Working on my final project for a PHP college course. I am working on a shopping cart at this point. I am hacking the script which came with my text book to make it work with my db.
In my db I have:
Table name = 'cart' in cart I have the following rows;
cartId
cookieId
isbn
qty
Table name = 'books' in books I have the following relevant rows;
isbn
title
price
I am able to click and add items to the cart. This function is supposed to display the cart (and I get a blank page). Pardon me, but I am posting the whole function just in case:
function ShowCart()
{
// Gets each item from the cart table and display them in
// a tabulated format, as well as a final total for the cart
global $hst, $usnm, $pwd, $dbs;
// Get a connection to the database
@ $db = new mysqli($hst, $usnm, $pwd, $dbs);
if (mysqli_connect_errno()) {
echo 'Connection to database failed:'.mysqli_connect_error();
exit();
}
$totalCost = 0;
[B]$query ="select * from cart inner join items on cart.isbn = books.isbn where cart.cookieId = '" . GetCookieId() . "' order by books.name asc";[/B] $result = $db->query($query);
?>
<html>
<head>
<title> Your Shopping Cart </title>
</head>
<body bgcolor="#ffffff">
<h1>Your Shopping Cart</h1>
<form name="frmCart" method="get">
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="15%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Qty</b>
</font>
</td>
<td width="55%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Product</b>
</font>
</td>
<td width="20%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Price Each</b>
</font>
</td>
<td width="10%" height="25" bgcolor="red">
<font face="verdana" size="1" color="white">
<b>Remove?</b>
</font>
</td>
</tr>
<?php
[B]while($row = mysqli_fetch_array($result, mysqli_ASSOC))[/B] {
// Increment the total cost of all items
$totalCost += ($row["qty"] * $row["price"]);
?>
<tr>
<td width="15%" height="25">
<font face="verdana" size="1" color="black">
<select name="<?php echo $row["isbn"]; ?>" onChange="UpdateQty(this)">
<?php
for($i = 1; $i <= 20; $i++)
{
echo "<option ";
if($row["qty"] == $i)
{
echo " SELECTED ";
}
echo ">" . $i . "</option>";
}
?>
</select>
</font>
</td>
<td width="55%" height="25">
<font face="verdana" size="1" color="black">
<?php echo $row["title"]; ?>
</font>
</td>
<td width="20%" height="25">
<font face="verdana" size="1" color="black">
$<?php echo number_format($row["price"], 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["isbn"]; ?>">Remove</a>
</font>
</td>
</tr>
<?php
}
// Display the total
?>
<tr>
<td width="100%" colspan="4">
<hr size="1" color="red" NOSHADE>
</td>
</tr>
<tr>
<td width="70%" colspan="2">
<font face="verdana" size="1" color="black">
<a href="products.php"><< Keep Shopping</a>
</font>
</td>
<td width="30%" colspan="2">
<font face="verdana" size="2" color="black">
<b>Total: $<?php echo number_format($totalCost, 2, ".", ","); ?></b>
</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
}
?>
</body>
</html>