如果(x> 0)输出0如何输出什么?

时间:2012-10-23 03:21:08

标签: php if-statement foreach

我希望我没有重复这个话题我确信它就在这里我找不到它。

我有一系列尺寸的产品,这些尺寸的可用性存储在数据库中,例如0 =库存中没有,1个或更多=库存。

如果不可用,我希望尺寸不会显示在下拉列表中。当1和更多可用时,它们显示...我可以在1或更多时显示大小但在=“0”时显示“0”我理解但不知道如何不显示。想想那里的解释很复杂:S

$size_available = "";
$size_6w = $row["size_6w"];
$size_7w = $row["size_7w"];
$size_8w = $row["size_8w"];

$dropdown = "<select class='product-select' name='size' id='size'>";

if ($size_6w > 0) {
    $size_6w = "6w";
}
if ($size_7w > 0) {
    $size_7w = "7w";
}
if ($size_8w > 0)
    $size_8w = "8w";
}

$size=array("$size_6w","$size_7w","$size_8w");

foreach ($size as $size_available)
{
    $dropdown .= "<option value='$size_available'>$size_available</option>";
}

$dropdown .= "\r\n</select>";

输出是一个简单的回声:

echo $dropdown

我应该使用else语句吗?如果是这样,你会怎么建议我用它来输出什么?

由于

6 个答案:

答案 0 :(得分:4)

检查大小是否大于或等于1,然后才向用户显示该选项。

以下是我将如何实现这一点:

<?php

$sizes = array(
    '6w' => $row["size_6w"],
    '7w' => $row["size_7w"],
    '8w' => $row["size_8w"]
);

$dropdown = "<select class='product-select' name='size' id='size'>";

foreach ($sizes as $size => $available)
{
    if ($available >= 1)
        $dropdown .= "<option value='$size'>$size</option>\n";
}

$dropdown .= "\r\n</select>";

答案 1 :(得分:2)

这就是我要做的事。

$sizes = array();
if ($row["size_6w"] > 0) $sizes[] = '6w';
if ($row["size_7w"] > 0) $sizes[] = '7w';
if ($row["size_8w"] > 0) $sizes[] = '8w';

if (!empty($sizes)) {
    $options = "<select class='product-select' name='size' id='size'>\n";
    foreach ($sizes as $size)
        $options .= "<option value='$size'>$size</option>\n";
    $options .= "</select>\n";
} else $options = "<div id='size' class='noResult'>0</div>\n";

答案 2 :(得分:1)

您可以编写一个检查$row的特定值的循环,而不是使用大量条件:

// this attempts to simulate the database row data
$row = array(
        'size_6w' => 1,
        'size_7w' => 0,
        'size_8w' => 2,
        'x' => 123, // field thrown in to illustrate the workings
);

$sizes = array();
foreach ($row as $field => $value) {
        // column should start with 'size_' and the value should be more than 0
        if (0 != strncmp($field, 'size_', 5) || !$value) {
                continue;
        }
        $size = substr($field, 5);
        echo sprintf('<option value="%s">%s</option>', $size, $size);
}

答案 3 :(得分:0)

if ($size_6w > 0) {
    $size_6w = "6w";
    array_push($size, "6w");
    }
if ($size_7w > 0) {
    $size_7w = "7w";
    array_push($size, "7w");
    }
if ($size_8w > 0) {
    $size_8w = "8w";
     array_push($size, "8w");
    }

//$size=array("$size_6w","$size_7w","$size_8w");

答案 4 :(得分:0)

$size=array($size_6w,$size_7w,$size_8w);
if(!empty($size))
{
foreach ($size as $size_available)
        {
          if(!empty($size_ailvaable)
          {
            $dropdown .= "<option value='$size_available'>$size_available</option>";
          }
        }
}
}
$dropdown .= "\r\n</select>";

答案 5 :(得分:0)

如果我做对了,你可能想要改变你的代码:

$size = array(); //initialize size array
if ($size_6w > 0) {
    $size_6w = "6w";
    $size[] = $size_6w;
}
if ($size_7w > 0) {
    $size_7w = "7w";
    $size[] = $size_7w;
}
if ($size_8w > 0) {
    $size_8w = "8w";
    $size[] = $size_8w;
}

$size=array("$size_6w","$size_7w","$size_8w"); //remove this line in your code.

这样,如果$size数组的值大于0,它只会包含大小。