仅在输入值大于0时显示值

时间:2017-02-09 22:24:55

标签: javascript php html loops

所以我撞墙了。我试图避免在“审核订单”页面中显示表单中的所有颜色选项。 因此,他们将在每种颜色的表单中输入所需的数量,并提交到review.php页面。 我只是不确定我是否需要PHP循环或javascript循环或如何完成此操作。 任何帮助真的很感激!

表格

<form action="review.php" method="post" name="form1" id="form1">
<p>12x12<br>
c000x12 <input type="text" name="c000x12"><br>
c010x12 <input type="text" name="c010x12"><br>
c020x12 <input type="text" name="c020x12"><br>
c019x12 <input type="text" name="c019x12"><br>
c021x12 <input type="text" name="c021x12"><br>
</p>
<p>12x24<br>
c000x24 <input type="text" name="c000x24"><br>
c010x24 <input type="text" name="c010x24"><br>
c020x24 <input type="text" name="c020x24"><br>
c019x24 <input type="text" name="c019x24"><br>
c021x24 <input type="text" name="c021x24">
</p>
<p><input type="submit" name="submit" id="submit" value="Submit"></p>

</form>

然后在评论页面上我有以下内容...... 我想只显示大于0的值。

PHP

<?PHP

// x12
$c000x12= $_POST["c000x12"];
$c010x12= $_POST["c010x12"];
$c020x12= $_POST["c020x12"];
$c019x12= $_POST["c019x12"];
$c021x12= $_POST["c021x12"];

// x24
$c000x24= $_POST["c000x24"];
$c010x24= $_POST["c010x24"];
$c020x24= $_POST["c020x24"];
$c019x24= $_POST["c019x24"];
$c021x24= $_POST["c021x24"];

?>

HTML

<p>12x12<br>
<? echo "000 Transparent=".$c000x12 ."<br/>".
"010 White=".$c010x12 ."<br/>".
"020 Golden yellow=".$c020x12 ."<br/>".
"019 Signal yellow=".$c019x12 ."<br/>".
"021 Yellow=".$c021x12 ."<br/>" ; ?>
</p>
<p>12x24<br>
<? echo "000 Transparent=".$c000x24 ."<br/>".
"010 White=".$c010x24 ."<br/>".
"020 Golden yellow=".$c020x24 ."<br/>".
"019 Signal yellow=".$c019x24 ."<br/>".
"021 Yellow=".$c021x24 ."<br/>" ; ?>
</p>

3 个答案:

答案 0 :(得分:0)

我想我可以保留使用循环来将值分别推送到数组。

那就是说,并且在这个设置中工作我可能会为显示器推荐这样的东西:

<p>12x12<br> 
<?php if( $c000x12 > 0 ){ echo $c000x12 ."<br>" }; ?>
<?php if( $c010x12 > 0 ){ echo $c010x12 ."<br>" }; ?>
<?php if( $c020x12 > 0 ){ echo $c020x12 ."<br>" }; ?>
<?php if( $c019x12 > 0 ){ echo $c019x12 ."<br>" }; ?>
<?php if( $c021x12 > 0 ){ echo $c021x12 ."<br>" }; ?>
</p>

<p>12x24<br> 
<?php if( $c000x24 > 0 ){ echo $c000x24 ."<br>" }; ?>
<?php if( $c010x24 > 0 ){ echo $c010x24 ."<br>" }; ?>
<?php if( $c020x24 > 0 ){ echo $c020x24 ."<br>" }; ?>
<?php if( $c019x24 > 0 ){ echo $c019x24 ."<br>" }; ?>
<?php if( $c021x24 > 0 ){ echo $c021x24 ."<br>" }; ?>
</p>

数组方法示例:

<?php

foreach ($_POST as $key => $value){
 if($value > 0){
  echo $value . "</br>";
 }
};

?>

答案 1 :(得分:0)

  

我想只显示大于0的值。

使用array_filterimplode函数的解决方案:

<强> PHP:

$c12x12 = array_filter([$c000x12, $c010x12, $c019x12, $c020x12, $c021x12], function ($v) {
    return (int) $v > 0;
});

$c12x24 = array_filter([$c000x24, $c010x24, $c020x24, $c019x24, $c021x24], function ($v) {
    return (int) $v > 0;
});

<强> HTML:

<p>12x12<br>
<?php echo implode('<br>', $c12x12); ?>
</p>
<p>12x24<br>
<?php echo implode('<br>', $c12x24); ?>
</p>

答案 2 :(得分:0)

我真的要感谢你们的帮助。我得到了修复。我从两个答案中使用了array_filter和foreach循环。 我忘记在OP中提到我也需要显示的名字。但是,经过这里的提示和更多的研究,这是我的修复。

**注意...... QTY部分中的所有变量都是来自表单的$ _POST。

FORM保持不变......

REVIEW

<?php
// COLORS
$n000= "Transparent";
$n010= "White";
$n020= "Black";
$n019= "Red";
$n021= "Blue";
//x12 QTY
$c000x12= "8";
$c010x12= "2";
$c020x12= "";
$c019x12= "1";
$c021x12= "3";
//x24 QTY
$c000x24= "0";
$c010x24= "1";
$c020x24= "3";
$c019x24= "0";
$c021x24= "9";



$x12=array($n000=>$c000x12,$n010=>$c010x12,$n020=>$c020x12,$n019=>$c019x12,$n021=>$c021x12);

echo "<b>12 x 12</b> <br>";

foreach($x12 as $clr=>$qty) {
 if($qty > 0){
  echo $clr . " = " . $qty;
  echo "<br>";
 }
};

  echo "<br>";

$x24=array($n000=>$c000x24,$n010=>$c010x24,$n020=>$c020x24,$n019=>$c019x24,$n021=>$c021x24);

echo "<b>12 x 24</b> <br>";

foreach($x24 as $clr=>$qty) {
 if($qty > 0){
  echo $clr . " = " . $qty;
  echo "<br>";
 }
};
?>

这将输出......

12 x 12
透明= 8
白= 2
红= 1蓝= 3

< b> 12 x 24
白色= 1
黑色= 3蓝色= 9