2014-01-24 148 views
0

現在我有這段代碼運行:計算總價PHP

$count = 0; 
    foreach ($products as $key => $product){ 
     $number = isset($_POST[$key])?$_POST[$key]:''; 
      if (!$number){ 
      $count++; 
      echo ""; 
      } else { 
     echo "<tr>"; 
      echo "<td>"; 
       echo $product; 
      echo "</td>"; 
      echo "<td>"; 
       echo $number; 
      echo "</td>"; 
      echo "<td>"; 
       $price = $prices[$count]* $number; 
       echo "&#8364 ", $price; 
      echo "</td>"; 
     echo "</tr>"; 

     $count++; 
     } 
    } 

但現在我加入了第3排(我的第一行只是<th>「s)表示,一個看起來像這樣的下面:

echo "<tr>"; 
    echo "<td>"; 
     echo "Totaal"; 
    echo "</td>"; 
    echo "<td>"; 
     echo $totalproducts; 
    echo "</td>"; 
    echo "<td>"; 
     echo $totalprice; 
    echo "</td>"; 
echo "</tr>"; 

正如你所看到的,我在這一行放置了2美元......在這裏,你可以猜到需要訂購的產品總量和這些產品的總價格。

我的問題是,我真的不知道如何讓它總結所有$價格,並將其放在$ totalprice中,並總結所有$數字並將其放在$ totalnumber中。我知道這是一個基本的問題,但它已經很久以前,因爲我實際上使用PHP。而我堅持這一點。

我很感謝這個事情的任何幫助,提前致謝!

+0

我希望你的意思是... $ price = $ price + $ prices [$ count] * $ number; – user1844933

+0

@ user1844933嗯,不,我不這樣做,爲什麼? –

回答

0

我希望你想要這個

第一設置變量$total_products$total_price

$total_products = 0; // set to 0 initially 
$total_price = 0; // set to 0 initially 

    $count = 0; 
     foreach ($products as $key => $product){ 
      $number = isset($_POST[$key])?$_POST[$key]:''; 
       if (!$number){ 
       $count++; 
       echo ""; 
       } else { 
      echo "<tr>"; 
       echo "<td>"; 
        echo $product; 
       echo "</td>"; 
       echo "<td>"; 
        echo $number; 
       echo "</td>"; 
       echo "<td>"; 
        $price = $prices[$count]* $number; 
        echo "&#8364 ", $price; 
       echo "</td>"; 
      echo "</tr>"; 

      $total_products += 1; // update total_products +1 
      $total_price += $price; // update total_price +$price 
      $count++; 
      } 
     } 

並把它們放在這裏就像

echo "<tr>"; 
    echo "<td>"; 
     echo "Totaal"; 
    echo "</td>"; 
    echo "<td>"; 
     echo $total_products; 
    echo "</td>"; 
    echo "<td>"; 
     echo $total_price; 
    echo "</td>"; 
echo "</tr>"; 
+0

這正是我只需要'$ total_products + = 1;'需要'$ total_products + = $ number;'因爲我的產品和我的價格系統一樣,用戶可以例如從這個產品訂購3個產品類型和5從另一種類型,使8,但非常感謝您的幫助和努力! –

1

嘗試......

$totalprice=0; 
$count = 0; 
    foreach ($products as $key => $product){ 
     $number = isset($_POST[$key])?$_POST[$key]:''; 
      if (!$number){ 
      $count++; 
      echo ""; 
      } else { 
     echo "<tr>"; 
      echo "<td>"; 
       echo $product; 
      echo "</td>"; 
      echo "<td>"; 
       echo $number; 
      echo "</td>"; 
      echo "<td>"; 
       $price = $prices[$count]* $number; 
       $totalprice = $totalprice+ $price; 
       echo "&#8364 ", $price; 
      echo "</td>"; 
     echo "</tr>"; 

     $count++; 
     } 
    } 

echo "<tr>"; 
    echo "<td>"; 
     echo "Totaal"; 
    echo "</td>"; 
    echo "<td>"; 
     echo $total_products; 
    echo "</td>"; 
    echo "<td>"; 
     echo $totalprice; 
    echo "</td>"; 
echo "</tr>";