简体   繁体   English

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

[英]only display value if input value is greater than 0

So I have hit a wall. 所以我撞墙了。 I am trying to avoid displaying all the color options from a form in a "review order" page. 我试图避免在“审阅顺序”页面中显示表单中的所有颜色选项。 So they would enter desired quantity in the form for each color and submit to review.php page. 因此,他们将在表格中为每种颜色输入所需的数量,然后提交给review.php页面。 I'm just not sure if I need a PHP loop or a javascript loop or how to accomplish this. 我不确定是否需要PHP循环或javascript循环或如何完成此操作。 Any help is really appreciated! 任何帮助都非常感谢!

Form 形成

<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>

Then on the review page I have the following... And I would like to only display the values that are greater than 0. 然后在评论页面上,我有以下内容...而且我只想显示大于0的值。

PHP 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 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>

I think I might reserve the use of a loop to a case where the values were each being pushed to an array. 我想我可以保留循环的使用,以免将每个值都推到数组中。

That said, and working within this setup I might recommend something like this for the display: 也就是说,在此设置下工作时,我可能会为显示器推荐类似的内容:

<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>

An array approach example: 数组方法示例:

<?php

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

?>

I would like to only display the values that are greater than 0. 我只想显示大于0的值。

The solution using array_filter and implode functions: 使用array_filterimplode函数的解决方案:

PHP: 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: HTML:

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

I really want to thank you guys for the help. 我真的很感谢你们的帮助。 I got a fix in place. 我已修复。 I used the array_filter and foreach loop from both answers. 我从两个答案中使用了array_filter和foreach循环。 I forgot to mention in the OP that I needed the names displayed as well. 我忘了在OP中提到我也需要显示的名称。 However, after the tips here and more research, here is my fix. 但是,经过这里的技巧和更多研究之后,这是我的解决方法。

**note.. all the variables in the QTY sections will be $_POST from the form. **注意.. QTY部分中的所有变量将是来自表单的$ _POST。

FORM remains the same... 形式保持不变...

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>";
 }
};
?>

And this will output... 这将输出...

12 x 12 12 x 12
Transparent = 8 透明= 8
White = 2 白色= 2
Red = 1 红色= 1
Blue = 3 蓝色= 3

12 x 24 12 x 24
White = 1 白色= 1
Black = 3 黑色= 3
Blue = 9 蓝色= 9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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