Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shopping Basket Problems

Status
Not open for further replies.

chrisgarvey

Technical User
Mar 27, 2003
64
0
0
GB
Hi.

I'm new to php and trying to create a shopping basket for the first time.

Firstly I am trying to create a php script to display the items in my sql database on screen before I move onto creating the actual on-line basket.

The script below display's the items as I wish however it produces several errors:

Use of undefined constant id - assumed 'id'
Use of undefined constant cat_title - assumed 'cat_title'
Use of undefined constant cat_desc - assumed 'cat_desc' Use of undefined constant cat_id - assumed 'cat_id'
Use of undefined constant item_title - assumed 'item_title'
Use of undefined constant item_price - assumed 'item_price'

Php code:
Code:
<?php
//connect to database
$conn = mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("indiana",$conn)  or die(mysql_error());

$display_block = "<h1>My Store - Item Detail</h1>";

//validate item
$get_item = "select c.cat_title, si.item_title, si.item_price, si.item_desc, si.item_image from store_items as si left join store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";
$get_item_res = mysql_query($get_item) or die (mysql_error());

if (mysql_num_rows($get_item_res) < 1) {
   //invalid item
   $display_block .= "<P><em>Invalid item selection.</em></p>";
} else {
   //valid item, get info
   $cat_title = strtoupper(stripslashes(mysql_result($get_item_res,0,'cat_title')));
   $item_title = stripslashes(mysql_result($get_item_res,0,'item_title'));
   $item_price = mysql_result($get_item_res,0,'item_price');
   $item_desc = stripslashes(mysql_result($get_item_res,0,'item_desc'));
   $item_image = mysql_result($get_item_res,0,'item_image');

   //make breadcrumb trail
   $display_block .= "<P><strong><em>You are viewing:</em><br><a href=\"seestore.php?cat_id=$cat_id\">$cat_title</a> &gt; $item_title</strong></p>
   <table cellpadding=3 cellspacing=3>
   <tr>
   <td valign=middle align=center><img src=\"$item_image\"></td>
   <td valign=middle><P><strong>Description:</strong><br>$item_desc</p>
   <P><strong>Price:</strong> \$$item_price</p>
   <form method=post action=\"addtocart.php\">";

   //get colors
   $get_colors = "select item_color from store_item_color where item_id = $item_id order by item_color";
   $get_colors_res = mysql_query($get_colors) or die(mysql_error());

   if (mysql_num_rows($get_colors_res) > 0) {
   	    $display_block .= "<P><strong>Available Colors:</strong>
  	    <select name=\"sel_item_color\">";
   	    while ($colors = mysql_fetch_array($get_colors_res)) {
     	    $item_color = $colors['item_color'];
     	    $display_block .= "<option value=\"$item_color\">$item_color</option>";
   	    }

  	    $display_block .= "</select>";
   }

   //get sizes
   $get_sizes = "select item_size from store_item_size where item_id = $item_id order by item_size";
   $get_sizes_res = mysql_query($get_sizes) or die(mysql_error());

   if (mysql_num_rows($get_sizes_res) > 0) {
  	    $display_block .= "<P><strong>Available Sizes:</strong>
  	    <select name=\"sel_item_size\">";
   	    while ($sizes = mysql_fetch_array($get_sizes_res)) {
    	    $item_size = $sizes['item_size'];
    	    $display_block .= "
    	    <option value=\"$item_size\">$item_size</option>";
   	    }

   	    $display_block .= "</select>";
   }

   $display_block .= "
   <P><strong>Select Quantity:</strong>
   <select name=\"sel_item_qty\">";

   for($i=1; $i<11; $i++) {
  	    $display_block .= "<option value=\"$i\">$i</option>";
   }

   $display_block .= "
   </select>
   <input type=\"hidden\" name=\"sel_item_id\" value=\"$_GET[item_id]\">
   <P><input type=\"submit\" name=\"submit\" value=\"Add to Cart\"></p>
   </form>
   </td>
   </tr>
   </table>";
}
?>
<HTML>
<HEAD>
<TITLE>My Store</TITLE>
</HEAD>
<BODY>
<? print $display_block; ?>
</BODY>
</HTML>
Does anyone have any idea of the problem?

Can anyone tell me where there are any good online resources as to how to create a on-line shopping basket?

Many Thanks,

Chris.
 
I've never used mysql_result. I would use something like mysql_fetch_object and replace your calls with something like $row=mysql_fetch_object($get_item_res);
$cat_title=$row->cat_title;
and then do your stripslashes etc (you could even do it in a function)
 
The PHP online manual entry on mysql_result() states:

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below).

and the specified list is:

Recommended high-performance alternatives : mysql_fetch_row(), mysql_fetch_array(), mysql_fetch_assoc() and mysql_fetch_object().





Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top