2016-05-14 41 views
3

我正嘗試創建一個餐館訂單摘要,客戶可以通過CheckBox和數量選擇他的訂單,並在最後獲得價格總和。 當選擇雞的工作完美的前例:您選擇了3只雞,並返回正確的價格,但是當我檢查另一個並選擇其他數量時,它是 給0選擇或給1數量,即使我把5例如。 只有雞在別人工作。HTML PHP將數量和價格添加到我的菜單項

查看我的代碼和輸出示例的屏幕截圖。 Test 2 should give: you have selected 2 Meat Total 6

這是我的代碼:

<body> 
<h3>Select what you want to eat</h3> 

<form action="PlaceOrder.php" method="get"/> 
<input type="checkbox" name="choice[]" value="1"/>Chicken,Price:8 
<input name="quantity[]" type="text" /><br /> 

<input type="checkbox" name="choice[]" value="2"/>Meat,Price:3 
<input name="quantity[]" type="text" /><br /> 

<input type="checkbox" name="choice[]" value="3"/>Souvlaki,Price:2.50 
<input name="quantity[]" type="text" /><br /> 

<input type="checkbox" name="choice[]" value="4"/>Pizza,Price:12 
<input name="quantity[]" type="text" /><br /> 

<input type="submit" value="Order"/> 
</form> 

和PHP:

<?php 
if(isset($_GET["choice"])){ 
$food=$_GET["choice"]; 
$quantity=$_GET["quantity"]; 
$c = count($food); 
$price = 0.0; 


for ($i=0; $i<$c ; $i++){ 
    if ($food[$i] == 1){ 
     $price = $price + 8 * $quantity[$i]; 
//here it's not working with quantity 
     echo "You have selected " .$quantity[$i]." Chicken <br>"; 
    } 

    if ($food[$i] == 2){ 
     $price = $price + 3 * $quantity[$i]; 
     echo "You have selected" .$quantity[$i]." Meat <br>"; 
    } 

    if ($food[$i] == 3){ 
     $price = $price + 2.5 * $quantity[$i]; 
     echo "You have selected " .$quantity[$i]."Souvlaki <br>"; 
    } 

    if ($food[$i] == 4){ 
     $price = $price + 12 * $quantity[$i]; 
     echo "You have selected" .$quantity[$i]." Pizza <br>"; 
    }  
    } 

    echo "Total: ".$price . "<br>"; 
} 
else { 
echo "Please select something!"; 
} 


?> 




</body> 

回答

2

的問題是,沒有被選中的複選框沒有得到發送到PHP,所以你的代碼不會像你在那裏那樣工作。您需要在表單中包含關於每個項目的更多信息,以便更方便地處理PHP端的數據,並且這樣在顯示項目名稱時不必重複自己。嘗試這樣的:

<?php 
$total = 0; 
$items = []; 
$info = 'Select something to order.'; 

// form submitted 
if(!empty($_POST['choice']) && is_array($_POST['choice'])) 
{ 
    // loop all item choices 
    foreach($_POST['choice'] as $item) 
    { 
     // filter item info 
     $name  = trim($item['name']); 
     $price = floatval($item['price']); 
     $quantity = intval($item['quantity']); 

     // only add if item was checked and quantity is more than 0 
     if(isset($item['checked']) && $quantity > 0) 
     { 
      $items[] = $quantity .' '. $name; 
      $total += $price * $quantity; 
     } 
    } 
    // update info if items were selected 
    if(count($items)) 
    { 
     $info = 'You selected ('.implode(', ', $items).'), total: '.$total; 
    } 
} 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Order Form</title> 
    <meta charset="utf-8" /> 
</head> 
<body> 

    <form id="order-form" action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> 
     <div class="form-item"> 
      <input type="checkbox" name="choice[0][checked]" /> 
      <span>Chicken, Price: 8</span> 
      <input type="number" name="choice[0][quantity]" value="1" /> 
      <input type="hidden" name="choice[0][price]" value="8" /> 
      <input type="hidden" name="choice[0][name]" value="Chicken" /> 
     </div> 
     <div class="form-item"> 
      <input type="checkbox" name="choice[1][checked]" /> 
      <span>Meat, Price: 3</span> 
      <input type="number" name="choice[1][quantity]" value="1" /> 
      <input type="hidden" name="choice[1][price]" value="3" /> 
      <input type="hidden" name="choice[1][name]" value="Meat" /> 
     </div> 
     <div class="form-item"> 
      <input type="checkbox" name="choice[2][checked]" /> 
      <span>Souvlaki, Price: 2.50</span> 
      <input type="number" name="choice[2][quantity]" value="1" /> 
      <input type="hidden" name="choice[2][price]" value="2.50" /> 
      <input type="hidden" name="choice[2][name]" value="Souvlaki" /> 
     </div> 
     <div class="form-item"> 
      <input type="checkbox" name="choice[3][checked]" /> 
      <span>Pizza, Price: 12</span> 
      <input type="number" name="choice[3][quantity]" value="1" /> 
      <input type="hidden" name="choice[3][price]" value="12" /> 
      <input type="hidden" name="choice[3][name]" value="Pizza" /> 
     </div> 
     <input type="submit" value="Order"/> 
    </form> 

    <hr /> 
    <p><?= $info ?></p> 

</body> 
</html> 
1

食品陣列和數量數組的長度並不總是相同的。

示例:

食品陣列(的var_dump)

array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } 

量陣列還具有空值(的var_dump)

array(4) { [0]=> string(0) "" [1]=> string(1) "4" [2]=> string(1) "5" [3]=> string(0) "" } 

這爲我工作:

if ($food[$i] == 1){ 
    $price = $price + 8 * $quantity[0]; 
    echo "You have selected " .$quantity[0]." Chicken <br>"; 
    continue; 
} 

if ($food[$i] == 2){ 
    $price = $price + 3 * $quantity[1]; 
    echo "You have selected" .$quantity[1]." Meat <br>"; 
} 

if ($food[$i] == 3){ 
    $price = $price + 2.5 * $quantity[2]; 
    echo "You have selected " .$quantity[2]."Souvlaki <br>"; 
} 

if ($food[$i] == 4){ 
    $price = $price + 12 * $quantity[3]; 
    echo "You have selected" .$quantity[3]." Pizza <br>"; 
} 

希望它能起作用如果我錯了,請糾正我!

+0

請問你能更具體一些,並給出更多的例子說明?如果可能的話附上評論謝謝。 – Johnny

+0

在錯誤情況下使用var dump檢查食物和數量數組的數組長度 –

2

而不是

$price = $price + 3 * $quantity[$i]; 

這是

$price = $price + 8 * $quantity[$food[$i]-1]; 

順便說一句,你需要在「食物」,以數字更改值,它是當你從形式得到它的字符串。

if(isset($_GET["choice"])){ 
$food=$_GET["choice"]; 
$quantity=$_GET["quantity"]; 
// begin type-cast 
// without the '&',this code won't modify a array element directly 
// We could iterate the array normally(without '&') and push the value to a new array, 
// But that may change the order of the $quantity, I am not sure, so I'd better reference the element directly. 
foreach ($food as &$value) { 
//(int) change it to a number. 
    $value = (int)$value; 
} 
foreach ($quantity as &$value) { 
    $value = (int)$value; 
} 
// type-cast done 
$c = count($food); 
$price = 0.0; 


for ($i=0; $i<$c ; $i++){ 
if ($food[$i] == 1){ 
    $price = $price + 8 * $quantity[$food[$i]-1]; 
    echo "You have selected " .$quantity[$food[$i]-1]." Chicken <br>"; 
} 

if ($food[$i] == 2){ 
    $price = $price + 3 * $quantity[$food[$i]-1]; 
    echo "You have selected " .$quantity[$food[$i]-1]." Meat <br>"; 
} 

if ($food[$i] == 3){ 
    $price = $price + 2.5 * $quantity[$food[$i]-1]; 
    echo "You have selected " .$quantity[$food[$i]-1]." Souvlaki <br>"; 
} 

if ($food[$i] == 4){ 
    $price = $price + 12 * $quantity[$food[$i]-1]; 
    echo "You have selected " .$quantity[$food[$i]-1]." Pizza <br>"; 
}  
} 

    echo "Total: ".$price . "<br>"; 
} 
else { 
echo "Please select something!"; 
} 


?> 

HTML //您在這裏有一個錯字 雞,價格:8

<input type="checkbox" name="choice[]" value="2"/>Meat,Price:3 
<input name="quantity[]" type="text" /><br /> 

<input type="checkbox" name="choice[]" value="3"/>Souvlaki,Price:2.50 
<input name="quantity[]" type="text" /><br /> 

<input type="checkbox" name="choice[]" value="4"/>Pizza,Price:12 
<input name="quantity[]" type="text" /><br /> 

<input type="submit" value="Order"/>