【问题标题】:How do I put an If then statement into a mysql_fetch_array() statement in php如何将 If then 语句放入 php 中的 mysql_fetch_array() 语句中
【发布时间】:2013-03-25 21:00:14
【问题描述】:

我有一个数据库和表来获取 OID 结果并将它们插入到表中。这个表里面有多种类型的值,我需要最终表能够区分它们。我将结果按分组排序,并通过mysql_fetch_array() 将结果过滤到表格中。

这就是我在代码方面的优势。

$result = mysql_query("SELECT MAX(t1)AS t1, device, test, value FROM ACRC WHERE        test='Fan Speed' GROUP BY device, test ORDER BY device ASC, test ASC LIMIT 4");

echo "<table border='3' cellpadding='3'>
    <tr>
    <th>Timestamp</th>
    <th>Unit</th>
    <th>Test</th>
    <th>Result</th>
    </tr>
    <tbody>";

while($row = mysql_fetch_array($result))
    {
    echo"<tr>";
    echo"<td>". $row['t1']. "</td>";
    echo"<td>". $row['device']. "</td>";
    echo"<td>". $row['test']. "</td>";
    echo"<td>". $row['value']." %</td>";
    echo"</tr>";
    }


$result = mysql_query("SELECT MAX(t1)AS t1, device, test, value FROM ACRC WHERE test<>'Fan Speed' GROUP BY device, test ORDER BY  test ASC, device ASC LIMIT 8");


while($row = mysql_fetch_array($result))
    {
    echo"<tr>";
    echo"<td>". $row['t1']. "</td>";
    echo"<td>". $row['device']. "</td>";
    echo"<td>". $row['test']. "</td>";
    echo"<td>". $row['value']." F</td>";
    echo"</tr>";
    }


echo"</table>";

此设置当前有效,但我想使用 if/then/else 语句将其压缩为一个 mysql_fetch_array(),因为只要风扇速度在测试列中,该值就会在其表的末尾添加一个 %正方形,每当温度出现在测试列中时,我需要一个 F 出现在值正方形中。数据当前在我的数据库中存储为一个简单的整数。

 --------------------
|Fan Speed  | 55   % |
----------------------
|Temperature| 70   F |
 --------------------

【问题讨论】:

    标签: php mysql arrays html-table


    【解决方案1】:

    如果我没有正确理解,请告诉我!

    while($row = mysql_fetch_array($result))
        {
    if( $row['value']==70)
    {
        echo"<tr>";
    
        echo"<td>". $row['device']. "</td><td>F</td>";
    
        echo"</tr>";
        }else
    {
     echo"<tr>";
    
        echo"<td>". $row['device']. "</td><td>" % . "</td> 
    
        echo"</tr>";
    }
    }
    

    【讨论】:

    • 您的语法对我来说没有意义,因为 If 值是从静态数字中获取的。随着我的 crontab 更新数据库,这些值将每 5 分钟更改一次。
    【解决方案2】:

    只需摆脱您的 WHERE 条件,以便所有结果都出现在一个查询中。然后,正如您在问题标题中提到的那样,使用if() 条件,在其中回显结果以确定您是否输出%F 或下一个高于该值的值。

    while($row = mysql_fetch_array($result))
        {
    ?>
    <tr>
        <td><?php echo $row['t1']; ?></td>
        <td><?php echo $row['device']; ?></td>
        <td><?php echo $row['test']; ?></td>
        <td><?php echo $row['value']; ?> <?php echo('$row['test'] === 'Fan Speed' ? '%' : 'F'); ?></td>
    </tr>
    <?php
        }
    

    或者,如果您想花哨并保持代码简洁,您甚至可以在 SQL 查询中使用 case 语句来填充新字段,如下所示:

    SELECT
      MAX(t1)AS t1,
      device,
      test,
      value,
      (CASE test WHEN 'Fan Speed' THEN '%' ELSE 'F') AS symbol
    FROM ACRC
    ...
    

    代码可能是:

    while($row = mysql_fetch_array($result))
        {
    ?>
    <tr>
        <td><?php echo $row['t1']; ?></td>
        <td><?php echo $row['device']; ?></td>
        <td><?php echo $row['test']; ?></td>
        <td><?php echo $row['value'] . ' ' . $row['symbol']; ?></td>
    </tr>
    <?php
        }
    

    【讨论】:

    • 我似乎无法得到你工作正确的任何一个例子,也许它所有的
    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2021-03-20
    • 2017-07-07
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多