PDA

View Full Version : Shopping cart: Displaying products on different pages


mancroft
07-07-2003, 12:04 PM
I am doing a shopping cart and have put the products into a mysql database.

The code below displays all the products on one page.

What I want to do is display product type A (3 items) on one page, product type B (4 items) on another page, product type C (2 items) on another page and so on.

What is the best way to go about this?

Thanks for your help.

CODE BELOW>>>>>>>>


include("db.php");

// Get a connection to the database
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$result = mysql_query("select * from items order by itemName asc");

?>
<html>
<head>
<title> Product List </title>
</head>
<body bgcolor="#ffffff">
<h1>Products</h1>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="30%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
&nbsp;&nbsp;<b>Product</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Price</b>
</font>
</td>
<td width="50%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Description</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Add</b>
</font>
</td>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="30%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo $row["itemPrice"]; ?>
</font>
</td>
<td width="50%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemDesc"]; ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>&qty=1">Add Item</a>
</font>
</td>
</tr>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<?php
}
?>
<tr>
<td width="100%" colspan="4">
<font face="verdana" size="1" color="black">
<a href="cart.php">Your Shopping Cart &gt;&gt;</a>
</font>
</td>
</tr>
</table>
</body>
</html>

raf
07-07-2003, 01:11 PM
In your MySQL table (items), you need a column like "producttype", where you need to store this A, B or C, or better, where you record 1,2,3 etc as a foreign key to a producttypes-table where these values are the primary key) (Because you can filter much faster on numerical variabels).
Then you just need to replace
$result = mysql_query("select * from items order by itemName asc");
by
$result = mysql_query("select * from items WHERE producttype=1 order by itemName asc");

Normally, you'll call this page with a value for producttype in the querystring (1,2,3), like
<a href="page.php?prodtype=1">Products from catagory A</a>
so you'd need

$result = mysql_query("select * from items WHERE producttype=" . $GET_['prodtype'] . " order by itemName asc");

mancroft
07-07-2003, 05:16 PM
Thanks raf, excellent answer.

raf
07-07-2003, 07:52 PM
You're welcome.