2010-04-02 43 views
1

我正在做一個購物cart.i有一個變量$ total.when我添加第一個產品的價格200我得到$總= 200,當我添加第二個產品的價格100當我添加價格400的第三個產品時,我得到$ total = 200100,我得到$ total = 200100400。我想要得到$總數= 700。如何解決這個問題?查找總價值得到一個變量在PHP

session_start(); 
      $ar20=Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'price WHERE id="'.$_SESSION['y'].'" AND prodid="'.$row['id_product'].'" ORDER BY size DESC'); 

      for($i=0;$i < count($ar20);$i++) 
      { 
       $test=$test+$ar20[$i]['price']; 
      } 




      $row['price'] += $test ; 
$row['total'] = $row['price'] * intval($row['quantity']); 

如果3個產品則呼應$行[「總」]會得到這個100200300

+1

向我們顯示您的代碼。 – 2010-04-02 05:18:10

+2

在php'+'中做了一個隱式轉換爲數字值。這可能聽起來像一個愚蠢的問題,但你確定你在PHP中添加數字而不是在嵌入在PHP文檔中的Javascript代碼塊中? – leepowers 2010-04-02 05:21:52

+0

session_start(); $ ar20 = Db :: getInstance() - > ExecuteS('SELECT * FROM'._DB_PREFIX _。'WHERE id =''。$ _ SESSION ['y']。'「AND prodid =」'。$ row [' id_product']。'「ORDER BY size DESC'); \t \t \t 爲($ I = 0; $ I <計數($ AR20); $ I ++){ $ 測試= $測試+ $ AR20 [$ i]於[ '價格']; } \t \t \t \t \t \t echo $ test; \t \t \t \t \t \t \t \t \t \t \t \t $行[ '價格'] + = $測試; $ row ['total'] = $ row ['price'] * intval($ row ['quantity']); 如果3個產品然後回顯$行['total']會得到這個100200300 – Hmwd 2010-04-02 05:36:26

回答

1

使用+代替.操作

+0

我已經使用+ opearator – Hmwd 2010-04-02 05:15:49

+0

@Hmwd所以,你在做你的數學在JavaScript中,而不是在PHP – 2010-04-02 05:25:55

1

只是每次獲得的價格的價值,並將其添加在以前像明智的,下面的邏輯可以幫助ü 說$newprice是一個變量,其中u得到的價格..

$total = 0; 
$total +=$newprice; 
+0

我試着像你所顯示的,但沒有變化,我gt像$總= 200100400 – Hmwd 2010-04-02 05:16:55

0
session_start(); 

$records=Db::getInstance()->ExecuteS('SELECT * FROM '._DB_PREFIX_.'price WHERE id="'.$_SESSION['y'].'" AND prodid="'.$row['id_product'].'" ORDER BY size DESC'); 

$row = array(); 
$test = '0'; 

if (count($records) < 1) { 
    $row['price'] = '0'; 
    $row['total'] = '0'; 
} else { 
    foreach($records as $record) 
    { 
     $test = bcadd($test, $record['price']); 
    } 

    $row['price'] = bcadd($row['price'], $test); 
    $row['total'] = bcmul($row['price'], $row['quantity']); 
} 

PHP: bcadd

+0

檢查bcadd,bcmul規範爲規模。 – 2010-04-02 14:26:25

0

要這樣?

$total = '100';
$total += '300';
$total += '300';
echo $total;

PHP是寬容的一個點,特別是對JavaScript的球迷:它允許你使用+運算符,串聯,如果兩邊都是字符串。請確保你不圍繞他們的報價,單人或雙人,像這樣:

$total = 100;
$total += 300;
$total += 300;
echo $total;

+0

session_start(); $ ar20 = Db :: getInstance() - > ExecuteS('SELECT * FROM'._DB_PREFIX _。'WHERE id =''。$ _ SESSION ['y']。'「AND prodid =」'。$ row [' id_product']。'「ORDER BY size DESC'); \t \t \t 爲($ I = 0; $ I <計數($ AR20); $ I ++){ $ 測試= $測試+ $ AR20 [$ i]於[ '價格']; } \t \t \t \t \t \t echo $ test; \t \t \t \t \t \t \t \t \t \t \t \t $行[ '價格'] + = $測試; $ row ['total'] = $ row ['price'] * intval($ row ['quantity']); 如果3個產品然後回顯$行['total']會得到這個100200300 – Hmwd 2010-04-02 05:33:29

0

您的代碼是不明確的,因爲它沒有上下文,但這應該工作:

$test = (integer) $test; 

for($i=0; $i < count($ar20); $i++) 
{ 
    $test = $test + (integer) $ar20[$i]['price']; 
} 

$row['price'] = (integer) $row['price'] + (integer) $test; 

$row['total'] = $row['price'] * intval($row['quantity']); 
+0

我試過但結果是一樣的。我在foreach循環中使用了代碼 – Hmwd 2010-04-02 05:50:55

+0

老實說,我很難相信這種類型。 – 2010-04-02 05:52:32