仅检索值大于0的列

时间:2012-12-02 16:09:35

标签: php mysql

我想创建一个只显示值大于0的列的显示。

我的代码显示字段和值:

echo "<table><tr>";
while ($property = mysql_fetch_field($result))
{
    if($property->name > '0')
    echo "<td>Field name: " . $property->name . "</td>";
}
echo "<tr/>";
while ($row = mysql_fetch_assoc($result)) 
{ 
    echo "<tr><td>".$row['a']."</td>";
    echo "<td>".$row['b']."</td>";
    echo "<td>".$row['c']."</td>";
    echo "<td>".$row['d']."</td></tr>";
}
echo "</table>";

输出如下:

Field name: a   Field name: b   Field name: c   Field name: d
1               2               3               0

我希望有如下显示:

Field name: a   Field name: b   Field name: c   
1               2               3               

由于值为0,因此未显示field name: d

我该怎么做?我想要你的一些例子吗?

感谢。

4 个答案:

答案 0 :(得分:0)

while ($row = mysql_fetch_assoc($result)) 
{ 
   echo "<tr>";
   if($row['a'] >0)    echo "<td>".$row['a']."</td>";  else echo"<td>-<td>";
   if($row['b'] >0)    echo "<td>".$row['b']."</td>";  else echo"<td>-<td>";
   if($row['c'] >0)    echo "<td>".$row['c']."</td>";  else echo"<td>-<td>";
   if($row['d'] >0)    echo "<td>".$row['d']."</td>";  else echo"<td>-<td>";
  echo "</tr>";
}

答案 1 :(得分:0)

您的代码已根据正确的预期结果进行了修改。

echo "<table><tr>";
while ($property = mysql_fetch_field($result))
{
    if($property->name > '0')
    echo "<td>Field name: " . $property->name . "</td>";
}
echo "<tr/>";

while ($row = mysql_fetch_assoc($result)) 
{ 
    echo "<tr>";
    if($row['a'] > 0) echo "<td>".$row['a']."</td>";
    if($row['b'] > 0) echo "<td>".$row['b']."</td>";
    if($row['c'] > 0) echo "<td>".$row['c']."</td>";
    if($row['d'] > 0) echo "<td>".$row['d']."</td>";
    echo "</tr>";
}
echo "</table>";

答案 2 :(得分:0)

$results = array();
$disp_col_a = false;
$disp_col_b = false;
$disp_col_c = false;
$disp_col_d = false;

while ($row = mysql_fetch_assoc($result)) 
{
    if ($row['a'] > 0){
        $disp_col_a = true;
    }
    if ($row['b'] > 0){
        $disp_col_b = true;
    }
    if ($row['c'] > 0){
        $disp_col_b = true;
    }
    if ($row['b'] > 0){
        $disp_col_b = true;
    }
    $results[] = $row;
}

echo "<table><tr>";
if ($disp_col_a){
    echo "<td>Field name: a</td>";
}
if ($disp_col_b){
    echo "<td>Field name: b</td>";
}
if ($disp_col_c){
    echo "<td>Field name: c</td>";
}
if ($disp_col_d){
    echo "<td>Field name: d</td>";
}
echo "<tr/>";
foreach ($results as $results_row) {
{ 
    echo "<tr>";
    if ($disp_col_a){
        echo "<td>";
        if ($results_row['a'] > 0){
            echo $results_row['a'];
        }
        echo "</td>";
    }
    if ($disp_col_b){
        echo "<td>";
        if ($results_row['b'] > 0){
            echo $results_row['b'];
        }
        echo "</td>";
    }
    if ($disp_col_c){
        echo "<td>";
        if ($results_row['c'] > 0){
            echo $results_row['c'];
        }
        echo "</td>";
    }
    if ($disp_col_d){
        echo "<td>";
        if ($results_row['d'] > 0){
            echo $results_row['d'];
        }
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";

答案 3 :(得分:-1)

不要挑剔,撇开乍看之下让mysql在这里做繁重的工作当然更有意义 - 为什么你要在HTML表中输出具有不同列数的一些行?除了永久性的维护问题之外,从视觉美学的角度来看,这将被视为“坏设计”。 :)

与此同时,我确实理解“足够好”的概念,所以我不会抱怨太多。