2012-02-16 58 views
0

我試圖抓住我的購物車的總計,但我似乎無法讓它工作,我想知道如果它的方式我已經接近我的代碼,我有使用foreach循環從會話數組中獲取id,然後循環遍歷結果,但是當我嘗試執行SUM查詢時,答案僅輸出單個價格作爲數組,以便將它們添加到一起。將價格存儲在二維數組中會更好嗎?不能得到一個購物車的總計PHP/MySQL

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>'; 
         } 
        ?> 
        ?> 
+0

你嘗試直接在SQL查詢?它可能是查詢 – Joseph 2012-02-16 08:29:44

回答

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>'; 
         } 
        ?> 
        ?> 
+0

謝謝你的工作,感謝上帝,像你這樣的人:) – Greyhamne 2012-02-16 08:35:53

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

如果你的ID是唯一的,它永遠把 '價格' 值。 此外,你的總和是在foreach。 如果你想要總價格,我建議你保持價格值在PHP變量和總結在foreach,那麼你只需要在最後顯示這個變量。

並且不推薦使用「select *」,您應該放置要檢索的字段。

+0

感謝您的幫助,是的,我會開始刪除*我的查詢,因爲我聽到那懶惰 – Greyhamne 2012-02-16 08:36:32

0

您可以通過遍歷購物車中的物品來計算PHP中的總和。如果你真的想用SQL來做,你必須在foreach循環之外完成,因爲它一次只能給你一個項目的價格。

使用implode()您可以連接在車中的所有項目ID到一個列表中查詢中使用:

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