简体   繁体   中英

Need help creating an if function

I am currently making a self-processing form with 5 products, where the user can enter a quantity for any of the 5 products. I saved the quantity that they entered into

$_POST[qty_entered] 

because I have the following for function to print the product display:

// Print the product display
for ($i = 0; $i < sizeof ($product_display); $i++){
print "<tr><td>" .$product_display[$i]['image'] . "</td>"; 
print "<td>" . $product_display[$i]['model'] . "</td>"; 
print "<td> $" . $product_display[$i]['price'] . "</td>" ; 

// Allow user inqut for quantity

print "<td> <input type = 'text' name = 'qty_entered[]' value ='0' size = '3' maxlength = '3'>  </td> </tr>";} ?>
    </table>

            <br><input type = 'submit' name = 'submit_button' value = 'Submit Order'><br><br>

After the user inputs a quantity, and press the submit button, I must check that they entered valid quantities (which is a positive integer) and then print the invoice if the user inputs a valid quantity. If they did not input a valid quantity, I need to print an error message and not display the invoice. The following code is what I have now, and I am having problems making it display correctly. One thing that I believe is wrong is that I somehow need to refer to the multiple quantities that are saved in $_POST['qty_entered'] however I cannot figure out how to do that without using [0][1][2][3] and it gives me an error if I use multiple ones. [$i] also does not work.

// Make sure quantity entered is a positive whole number and print table row
       $errors = FALSE;
if (array_key_exists ('submit_button', $_POST) && is_numeric($_POST['qty_entered'][0]) && $_POST['qty_entered'][0] > 0) {
print "<h2>Invoice</h2> <br> <table style='border-collapse: collapse; text-align: center'
                  height='319' border='1' bordercolor='#111111'
                  cellpadding='0' cellspacing='0' width='514'>

        <!-- create header use <th> -->
        <tr>
            <th>Phone</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Extended Price</th>
        </tr>";
}
else {
    $errors = TRUE;
    Print "Please enter a valid quantity.";
}

Please help me create a proper if-function to check if the user inputted valid quantities after they press the submit button. If the quantities entered is valid, print the invoice, if not, print the error message.

Thank you!

Use foreach:

// Start out assuming all products have quantities
$allProductsHaveQuantities = true;
foreach($_POST['qty_entered'] as $idx => $qty)
{
  // Look for any quantities that are zero
  if($qty == 0)
  {
    // One is enough, so set the flag and stop the loop
    $allProductsHaveQuantities = false;
    break;
  }
}

// Now check the result of the flag
if(!$allProductsHaveQuantities)
{
  ...your error message here...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM