php - products duplicate in shoppingcart -
when add together multiple products shopping cart, duplicates product info first inserted. prepare statement in showcart() echo's 'there's wrong', while info still displayed, suppose code looks quite nasty. excuses that, i'm planning on cleaning it, when function
public function displayproduct() { if($product = $this->db->query("select id, title, description, cost trips order 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 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 $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 $id=>$qty) { $sql = 'select id, title, description, cost trips 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 empty.</p>'; $output[] = '<p><a href="index.php?page=reizen.php">terug naar reizen</a></p>'; } homecoming implode('',$output); }
first off, 2 separate bugs, , unless there else going on aren't telling first script has nil either.
bug 1: if script echos "something went wrong" during phone call to showcart() should debugging database connection , statement prepare. unless of column names or row name wrong, error in connection. seek echoing db error info see going on.
bug 2: duplicated product info when showing cart:
to debug we'd need see how add together product cart in first place, interacting line negatively:
foreach ($items $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } edit
actually isn't line causing sec bug, fact echo product info if db phone call fails.
php mysqli shopping-cart
No comments:
Post a Comment