Jonathon Harper
Jonathon Harper

Reputation: 13

How do I put an If then statement into a mysql_fetch_array() statement in php

I have one database and table that grabs OID results and inserts them into a table. This table has multiple types of values inside it that I need the final table to be able to differentiate between. I have the results sorted with a group by and I have the results filtered into a table through mysql_fetch_array().

Heres what I have code wise.

$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>";

This set up currently works but I want to compress it into one mysql_fetch_array() using an if/then/else statement in that whenever fan speed is in the test column, the value has a % added to the end of it's table square and whenever a temperature shows up in the test column, I need a F to appear in the value square. The data is currently stored as a simple integer in my database.

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

Upvotes: 0

Views: 662

Answers (2)

zod
zod

Reputation: 12417

if i dont understand correctly let me know !

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>";
}
}

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71384

Just get rid of your WHERE condition such that all results appears in one query. Then, as you mention in your question title, use an if() condition where you echo out the results to determine if you output a %, F, or whatever next top the value.

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
    }

Or, if you want to get fancy and keep your code clean you could even use cases statements in your SQL query to populate a new field like this:

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

And the code could be:

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
    }

Upvotes: 1

Related Questions