无法获得购物车PHP / MySQL的总计

时间:2012-02-16 08:24:43

标签: php mysql sum

我试图抓住我的购物车的总数,但我似乎无法让它工作,我想知道它是否我的方式我的代码,我已经使用foreach循环从会话中获取id然后循环遍历结果然后当我尝试进行SUM查询时,答案只是将单个价格作为数组输出,并将它们添加到一起。将价格存储在二维阵列中会更好吗?

if(!isset($_SESSION['cart'])){//
$_SESSION['cart']=array();//creating session array to store items id
}
    <?php
                        echo'<table class="table">';
                            echo'<tr>';
                            echo'<th>Item</th>';
                            echo'<th>Code</th>';
                            echo'<th>Description</th>';
                            echo'<th>Cost(GBP)</th>';
                            echo'<th>Remove</th>';
                            echo'</tr>';

                            foreach ($_SESSION['cart'] as $value){
                                $z = mysql_query('SELECT * FROM product_item where id="'.$value.'"');
                                while($row = mysql_fetch_array($z)){
                                    echo'<tr>';
                                    echo'<td><img src="images/'.$row['path'].'.jpg" alt="'.$row['alt'].'" width="65" height="65"/></td>';
                                    echo'<td>'.$row['code'].'</td>';
                                    echo'<td>'.$row['description'].'</td>';
                                    echo'<td><p>&pound;'.$row['price'].'</p></td>';
                                    echo'<td><p><a title="remove from shopping cart" href="cart.php?remove='.$value.'">X</a></p></td>';
                                    echo'</tr>';                                
                                }


                                // this is where i want to get total cost
                                $y = mysql_query('SELECT SUM(price) as total_cost FROM product_item where id="'.$value.'"');
                                while($row = mysql_fetch_array($y)){
                                    echo $row['total_cost'];
                                }

                            }

                            echo'<tr>';
                            echo'<td></td>';
                            echo'<td></td>';
                            echo'<td></td>';
                            echo'<td><p>Total</p></td>';
                            echo'<td></td>';
                            echo'</tr>';
                            echo'</table>';
                        }
                    ?>
                    ?>

3 个答案:

答案 0 :(得分:2)

为什么不是外部变量?

if(!isset($_SESSION['cart'])){//
$_SESSION['cart']=array();//creating session array to store items id

$cartTotal = 0; //This is where you store the total
}
    <?php
                        echo'<table class="table">';
                            echo'<tr>';
                            echo'<th>Item</th>';
                            echo'<th>Code</th>';
                            echo'<th>Description</th>';
                            echo'<th>Cost(GBP)</th>';
                            echo'<th>Remove</th>';
                            echo'</tr>';

                            foreach ($_SESSION['cart'] as $value){
                                $z = mysql_query('SELECT * FROM product_item where id="'.$value.'"');
                                while($row = mysql_fetch_array($z)){
                                    $cartTotal += $row['price'];
                                    echo'<tr>';
                                    echo'<td><img src="images/'.$row['path'].'.jpg" alt="'.$row['alt'].'" width="65" height="65"/></td>';
                                    echo'<td>'.$row['code'].'</td>';
                                    echo'<td>'.$row['description'].'</td>';
                                    echo'<td><p>&pound;'.$row['price'].'</p></td>';
                                    echo'<td><p><a title="remove from shopping cart" href="cart.php?remove='.$value.'">X</a></p></td>';
                                    echo'</tr>';                                
                                }
                            }
                            echo $cartTotal; //after running through all the items, you can show the total

                            echo'<tr>';
                            echo'<td></td>';
                            echo'<td></td>';
                            echo'<td></td>';
                            echo'<td><p>Total</p></td>';
                            echo'<td></td>';
                            echo'</tr>';
                            echo'</table>';
                        }
                    ?>
                    ?>

答案 1 :(得分:0)

'SELECT SUM(price) as total_cost FROM product_item where id="'.$value.'"'

如果您的ID是唯一的,它将始终带来“价格”值。 此外,你的总和是在foreach。 如果你想要总价格,我建议你将价格值保存在php变量中,并在foreach中总结,那么你只需要在最后显示这个变量。

不推荐使用“select *”,您应该输入要检索的字段。

答案 2 :(得分:0)

您可以在迭代购物车中的商品时计算PHP中的总和。如果您真的想在SQL中执行此操作,则必须在foreach循环之外执行此操作,因为这样一次只能为您提供一个项目的价格。

使用implode(),您可以将购物车中的所有商品ID连接到一个列表中,以便在查询中使用:

$y = mysql_query("SELECT SUM(price) AS total_cost FROM product_item WHERE id IN (" . implode(", ", $_SESSION['cart']) . ")";
相关问题