PDA

View Full Version : Shopping cart - Dropdown menu problem please help



paxdev
August 27th, 2006, 09:35 PM
Hi,
I'm new to PHP and trying to build a shopping cart. I'm displaying all the items in 1 page as several rows. Each product is of different sizes and prices.
So i have 2 tables, One with product name and the other table with product size and prices.

I want to display each product name and a dropdown with the weight and prices of for that size.
I researched the internet and did some coding.
I'm able to write each in a row with a dropdown of the sizes, but the problem is the dropdown is writing the values several times for the iteration and hence my dropdown has the same values 6 times the number of rows in the product table.



<?php
// This page will list all of the items
// from the items table. Each item will have
// a link to add it to the cart
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");
?>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td width="34%" height="25">
<font face="verdana" size="1" color="black">

<?php echo $row["itemName"]; ?> </font> </td>

<td width="17%" height="25">
<label><font face="verdana" size="1" color="black">

<?php
$sql = 'SELECT * FROM prodsize';
$res = mysql_query($sql) or die(mysql_error());
while ($rec = mysql_fetch_assoc($res)) $prices[] = $rec;
// die('<pre>'.print_r($countries));
echo '<SELECT name="dropdown">';
reset($prices);
foreach ($prices as $c)
{
if ($c['id'] == $_GET['id'])
echo "<OPTION value=\"{$c['prodid']}\" SELECTED>{$c['prodwt']}, {$c['prodprice']}</OPTION>\n";
else
echo "<OPTION value=\"{$c['prodid']}\">{$c['prodwt']}, {$c['prodprice']}</OPTION>\n";
}
echo '</SELECT>';




}

?>

AndyM103
August 28th, 2006, 06:50 PM
Try neatening up the script first!

paxdev
August 28th, 2006, 07:05 PM
<?php

include("db.php");

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$result = mysql_query("select * from items order by itemName asc");
?>
<?php
while($row = mysql_fetch_array($result))
{
echo $row["itemName"];
$sql = 'SELECT * FROM prodsize';
$res = mysql_query($sql) or die(mysql_error());
while ($rec = mysql_fetch_assoc($res)) $prices[] = $rec;
echo '<SELECT name="dropdown">';
foreach ($prices as $c)
{
if ($c['id'] == $_GET['id'])
echo "<OPTION value=\"{$c['prodid']}\" SELECTED>{$c['prodwt']}, {$c['prodprice']}</OPTION>\n";
else
echo "<OPTION value=\"{$c['prodid']}\">{$c['prodwt']}, {$c['prodprice']}</OPTION>\n";
}
echo '</SELECT>';


}

?>