当表格单元格等于“0”时更改它们的显示值

时间:2011-11-01 20:41:58

标签: php foreach while-loop

我有一张价格表。 我正在运行一个简单的while函数来显示HTML格式的价格表 我希望在html中显示时将所有0的价格更改为'call',而不更改实际的mysql表。

我可以用循环中的foreach函数以某种方式执行此操作吗?

while ($result1 = mysql_fetch_array($query1)) {
    foreach ($result1[] as $key => $value) {
        if ($key == '0') {$value='Call';}// ???? 
    echo "<tr>";
    echo "<td>";
    echo $result1['type'];
    echo "</td><td>";
    echo $result1['25'];
    echo "</td><td>";
    echo $result1['50'];
    echo "</td><td>";
    echo $result1['100'];
    echo "</td><td>";
    echo $result1['250'];
    echo "</td><td>";
    echo $result1['500']; 
    echo "</td><td>";
    echo $result1['plus'];
    echo "</td><td>";
    echo "</tr>";
    }

1 个答案:

答案 0 :(得分:1)

while ($result1 = mysql_fetch_array($query1)) {
    foreach ($result1 as &$value) {
        if($value == 0) $value = 'call';
    }
    ...

(同)

while ($result1 = mysql_fetch_array($query1)) {
    foreach ($result1 as $key => $value) {
        if($value == 0) $value[$key] = 'call';
    }
    ...