When I add multiple products to the shopping cart, it duplicates the product data which was first inserted. The prepare statement in showCart() also echo's 'There's something wrong', while the data is still displayed, I suppose my code looks quite nasty. Excuses for that, I'm planning on cleaning it, when I get it to function
public function displayProduct()
{
if($product = $this->db->query("SELECT id, title, description, price FROM trips ORDER BY id"))
{
while ($row = $product->fetch_assoc())
{
$output[] = '<div class="reisbox">';
$output[] = '<div id="reis_insidebox1">';
$output[] = '<div class="reis_textbox">';
$output[] = '<h2>'.ucfirst($row['title']).'</h2>';
$output[] = '<article>';
$output[] = ucfirst($row['description']);
$output[] = '</article>';
$output[] = '</div>';
$output[] = '<div class="rightboxx">';
$output[] = '<div class="reis_price_box">';
$output[] = '<div class="reis_price_box_text">';
$output[] = '€'.$row['price'];
$output[] = '</div>';
$output[] = '<div class="more_box">';
$output[] = '<a href="index.php?page=reis"><p>Lees meer..</p></a>';
$output[] = '</div>';
$output[] = '</div>';
$output[] = '</div>';
$output[]='<br />';
$output[] = '<div id="add">';
$output[]='<a href="index.php?page=cart.php&action=add&id='.$row['id'].'">Add to cart</a>';
$output[] = '</div>';
$output[] = '<div class="review_box">';
$output[] = '<div class="review_text">Review</div>';
$output[] = '<div class="review_textbox"> Fantastische ontvangst met kleine attenties. Fantastisch ontbijt,. Goede bedden en ruime zitgelegenheid in de serre.</div>';
$output[] = '<div class="star_box"></div>';
$output[] = '<div class="review_linkbox">';
$output[] = '<a href="review1.php">Schrijf review</a>';
$output[] = '</div>';
$output[] = '</div>';
$output[] = '</div>';
}
echo implode($output);
}
public function showCart() {
$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
$output[]='<div id="contents">';
$output[] = '<form action="index.php?page=cart.php&action=update" method="post" id="cart">';
$output[]='<table id="table_cart">';
$output[]='<thead>';
$output[]='<tr>';
$output[]='<th scope="col"></th>';
$output[]='<th scope="col">Informatie</th>';
$output[]='<th scope="col">Prijs</th>';
$output[]='<th scope="col">Aantal</th>';
$output[]='<th scope="col">Prijs Totaal</th>';
$output[]='</tr>';
$output[]='</thead>';
foreach ($contents as $id=>$qty)
{
$sql = 'SELECT id, title, description, price FROM trips WHERE id = ?';
if($result = $this->db->prepare($sql))
{
$result->bind_param('i', $id);
$result->execute();
$result->bind_result($id, $title, $description, $price);
$result->fetch();
}
else
{
echo "something went wrong";
}
$output[]='<tr>';
$output[]='<td><a href="index.php?page=cart.php&action=delete&id='.$id.'" class="r"><p>Remove</p></a></td>';
$output[]='<td>'.$title.'</td>';
$output[]='<td>€'.$price.'</td>';
$output[]='<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
$output[]='<td>€'.($price * $qty).'</td>';
$total += $price * $qty;
$output[]='</tr>';
}
$output[] = '<div id="total">';
$output[] = '<p>Grand total: <strong>€'.$total.'</strong></p>';
$output[] = '<button type="submit">Update cart</button>';
$output[] = '</div">';
$output[] = '</table>';
$output[]='</form>';
$output[] = '</div">';
} else {
$output[] = '<p>You shopping cart is empty.</p>';
$output[] = '<p><a href="index.php?page=reizen.php">terug naar reizen</a></p>';
}
return implode('',$output);
}
First off, this is two separate bugs, and unless there is something else going on you aren't telling us the first script has nothing to do with either.
Bug 1: If your script echos "something went wrong"
during a call to showCart()
then you should be debugging your database connection and your statement prepare. Unless some of those column names or the row name is wrong, the error will most likely be in your connection. Try echoing the DB error info to see what is going on.
Bug 2: duplicated product data when showing cart:
To properly debug this we'd need to see how you add a product to the cart in the first place, but likely however you do so is interacting with this line negatively:
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
EDIT
Actually it probably isn't that line causing the second bug, it probably is the fact that you echo product info even if the DB call fails.