0

我想创建一个只显示值大于 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               

因为field name: d值为 0,所以不显示。

我怎样才能做到这一点?我可以从你那里得到一些例子吗?

谢谢。

4

4 回答 4

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>";
于 2012-12-02T16:16:43.037 回答
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>";
于 2012-12-02T22:09:21.083 回答
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>";
}
于 2012-12-02T16:11:51.467 回答
-1

不要挑剔,撇开乍一看,让 mysql 在这里做繁重的工作肯定更有意义 - 为什么要在具有不同列数的 HTML 表中输出一些行数?除了永久维护的头痛之外,从视觉美学的角度来看,这会被一些人认为是糟糕的设计。:)

同时,我也理解“Good Enough Is”的概念,所以我不会过多抱怨。

于 2012-12-02T16:36:48.947 回答