I have a function that displays a users cart. Now I had it so that that it would loop through and display all of the result from the cart table in my database after a user added them. However, now it will only display one item at a time and I have no idea why.
Thanks!
Whatever is causing this seems to happen between this line:
$result = mysql_query("SELECT * FROM cart INNER JOIN " . $_SESSION['table'] . " on cart.itemId = " . $_SESSION['table'] . ".itemId WHERE cart.cookieId = '" . GetCartId() . "' ORDER BY " . $_SESSION['table'] . ".itemId ASC") or die(mysql_error());
and this line
while($row = mysql_fetch_array($result) or die(mysql_error()))
because if I put a while loop directly after the mysql query it will loop correctly, but if I put it anywhere after the while loop I am using it will not display more than one result.
<?php
function ShowCart() // Display the users cart
{
// Session var shiptype equals the variable passed in the url
$_SESSION['shiptype'] = $_GET['shiptype'];
switch($_SESSION['shiptype']) // Depending on the shiptype, keep that shiptype selected
{
case "Express":
{
$eselected = "SELECTED";
$pselected = "";
$cselected = "";
break;
}
case "Priority":
{
$eselected = "";
$pselected = "SELECTED";
$cselected = "";
break;
}
case "EMS":
{
$eselected = "SELECTED";
$pselected = "";
$cselected = "";
break;
}
case "AirMail":
{
$eselected = "";
$pselected = "SELECTED";
$cselected = "";
break;
}
default:
{
$eselected = "";
$pselected = "SELECTED";
$cselected = "";
}
}
// Our cart query
$result = mysql_query("SELECT * FROM cart INNER JOIN " . $_SESSION['table'] . " on cart.itemId = " . $_SESSION['table'] . ".itemId WHERE cart.cookieId = '" . GetCartId() . "' ORDER BY " . $_SESSION['table'] . ".itemId ASC") or die(mysql_error());
?>
<html>
<script language="JavaScript">
function getSelect(s) {
return s.options[s.selectedIndex].value
}
</script>
<body>
<form name="cartform" method="post" action="cart.php">
<select name="shipsel" OnChange="location=getSelect(this)">
<option <?php echo $cselected; ?> value="#"> Choose a shipping type</option>
<option <?php echo $eselected; ?> value="cart.php?shiptype=<?php echo $_SESSION['expressorems']; ?>"><?php echo $_SESSION['expressorems']; ?></option>
<option <?php echo $pselected; ?> value="cart.php?shiptype=<?php echo $_SESSION['prorair']; ?>"><?php echo $_SESSION['prorair']; ?></option>
</select>
</form>
<table>
<tr>
<th><font face="verdana" size="2" color="black">Quantity</font></th>
<th><font face="verdana" size="2" color="black">Name</font></th>
<th><font face="verdana" size="2" color="black">Price</font></th>
</tr>
</table>
</body>
</html>
<?php
while($row = mysql_fetch_array($result) or die(mysql_error()))
{
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["itemPrice"]);
$totalWeight += ($row["qty"] * $row["itemWeight"]);
if ($totalWeight < 16)
{
$emslookup=(ceil($totalWeight/8))* 8;
} else {
$emslookup=(ceil($totalWeight/16))* 16;
}
if ($totalWeight < 16)
{
$explookup=(ceil($totalWeight/8))* 8;
} else {
$explookup=(ceil($totalWeight/16))* 16;
}
if ($totalWeight < 8)
{
$airlookup=(ceil($totalWeight/1))* 1;
} else {
$airlookup=(ceil($totalWeight/4))* 4;
}
$lookupinsurance=(ceil($totalCost/50))* 50;
$priorlookup=(ceil($totalWeight/16))* 16;
if ($_GET['id'] == "831" AND $_GET['action'] == "add_item")
{
?> <html><script language="JavaScript">window.location="cart.php?action=add_item&id=157&qty=1";</script><?php
} else if ($_GET['id'] == "831" AND $_GET['action'] == "remove_item") {
?> <script language="JavaScript">window.location="cart.php?action=remove_item&id=157&qty=1";</script><?php
} else if ($_GET['id'] == "157" AND $_GET['action'] == "remove_item") {
?> <script language="JavaScript">window.location="cart.php?action=remove_item&id=831&qty=1";</script><?php
}
$iresult = mysql_query("SELECT * FROM us_priority_insurance WHERE coverage = $lookupinsurance") or die(mysql_error());
$emsresult = mysql_query("SELECT cost FROM foreign_ems WHERE weight = '$emslookup' AND emszone = '" . $_SESSION['emszone'] ."'") or die(mysql_error());
$airresult = mysql_query("SELECT cost FROM foreign_letterpost WHERE weight = '$airlookup' AND priority = '" . $_SESSION['airprior'] . "'") or die(mysql_error());
$expresult = mysql_query("SELECT cost FROM us_express WHERE weight = '$explookup'") or die(mysql_error());
$priorresult = mysql_query("SELECT cost from us_prioritymail WHERE weight = '$priorlookup'") or die(mysql_error());
if ($_SESSION['shiptype'] == "EMS")
{
$shipresult = $emsresult;
} elseif ($_SESSION['shiptype'] == "AirMail") {
$shipresult = $airresult;
} elseif ($_SESSION['shiptype'] == "Express") {
$shipresult = $expresult;
} elseif ($_SESSION['shiptype'] == "Priority") {
$shipresult = $priorresult;
}
?>
<script>
function UpdateQty(item)
{
stype = "<?php echo $_SESSION['shiptype']; ?>";
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty+'&shiptype='+stype;
}
</script>
<body>
<table>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
</tr>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["itemName"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["itemPrice"], 2, ".", ","); ?>
</font>
</td>
<td width="10%" height="25">
<font face="verdana" size="1" color="black">
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>&shiptype=<?php echo $_SESSION['shiptype']; ?>">Remove</a>
</font>
</td>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="40%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="product.php?country=<?php echo $_SESSION['country']; ?>"><< Keep Shopping</a>
</font>
</td>
</tr> <tr>
<td width="40%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Sub Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b><br>
<b>
Shipping: $
<?php
while($srow = @mysql_fetch_array($shipresult) or die(mysql_error()))
{
echo $srow[cost];
?>
<br>
Total: $
<?php
echo number_format($srow[cost] + $totalCost, 2, ".", ",");
} echo $st;
?>
<br>
Insurance: $
<?php
while($irow = mysql_fetch_array($iresult) or die(mysql_error()))
{
echo $irow[rate];
}
?>
</b>
</font>
</tr>
</td>
</table>
</body>
</html>
<?php
}
}
?>