douqian3712 2013-02-12 14:19
浏览 12

产品在shoppingcart中重复

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[] = '&euro;'.$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>&euro;'.$price.'</td>';
                $output[]='<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';
                $output[]='<td>&euro;'.($price * $qty).'</td>';
                $total += $price * $qty;
                $output[]='</tr>';


            }
            $output[] = '<div id="total">';
            $output[] = '<p>Grand total: <strong>&euro;'.$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);
    }
  • 写回答

1条回答 默认 最新

  • duanpan7011 2013-02-12 16:56
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条