Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display values except those that are empty or zero

Tags:

php

So I have a form with about 60 choices for colors where the customer enters the quantity of each color desired. The following page is a "review" of the order. So I have defined all the variables so that I can display the quantities. What I would like to do is not display the colors that have a value of "" or "0". Is this something that can be done?

Here is the head code...

<?PHP
$packSize = $_POST["packSize"];
$cost = $_POST[""];
//COLORS
$c000 = $_POST["c000"];
$c010 = $_POST["c010"];
$c020 = $_POST["c020"];
$c019 = $_POST["c019"];
$c021 = $_POST["c021"];
$c022 = $_POST["c022"];
$c025 = $_POST["c025"];
$c026 = $_POST["c026"];
// ect...
?>

In the body...

  <p>Color choices:<br>
<?php
echo "000 = ".$c000."<br/>".
"010 = ".$c010 ."<br/>".
"020 = ".$c020 ."<br/>".
"019 = ".$c019 ."<br/>".
"021 = ".$c021 ."<br/>".
"022 = ".$c022 ."<br/>".
"025 = ".$c025 ."<br/>".
"026 = ".$c026 ."<br/>".
// etc...
?>
like image 892
James Crellin Avatar asked Dec 09 '25 14:12

James Crellin


1 Answers

Instead of declaring 60 variable, declare an array. Suppose you have color range from c0 to c100 then declare an array color = array();. Also use empty($var) function to check empty value.

A short form of your code:

<?PHP
$packSize = $_POST["packSize"];
$cost = $_POST[""];
//COLORS 
$color = array();
for($i=0; $i<100; $i++){
   if($i<10 && !empty($_POST["c00".$i]))
          $color['c00'.$i] = $_POST["c00".$i];
   else if($i<100 && !empty($_POST["c0".$i]))
          $color['c0'.$i] = $_POST["c0".$i];
}

?>

// print

<p>Color choices:<br>
<?php
for($i=0; $i<100; $i++){
   if($i <10 && !empty($color['c00'.$i]){
       echo 'c00'.$i.'='$color['c00'.$i]."<br>";
   }
   else if($i <100 && !empty($color['c0'.$i]){
       echo 'c0'.$i.'='$color['c0'.$i]."<br>";
   }
}

// etc...
?>
like image 61
MD Ruhul Amin Avatar answered Dec 11 '25 02:12

MD Ruhul Amin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!