Comparing MySQL results to each other, while putting them in a table
I have a table that looks like this:
(so you can picture it visual开发者_开发知识库ly)
13 22 12
10 8 12
html:
<table>
<tr>
<td>13</td>
<td>22</td>
<td>12</td>
<tr>
<td>10</td>
<td>8</td>
<td>12</td>
</tr>
</table>
it's being populated by mysql rows:
<?php
$i = 1;
$id = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `users` WHERE `username` = '".$_GET['user']."'"));
$query = mysql_query("SELECT * FROM `data` WHERE `user_id` = '".$id['id']."' ORDER BY `datetime` DESC");
while($row = mysql_fetch_assoc($query)) {
echo '<tr><td>'.date('m/d H:i', strtotime($row['datetime'])).'</td>';
array_shift($row);
array_shift($row);
array_shift($row);
$new = array_chunk($row, 3);
foreach($new as $value => $var) {
if($var[1] == null) { $var[1] = "--"; }
echo '<td id="'.$i++.'">'.$var[1].'</td>';
}
echo '</tr>';
}
?>
If I wanted to have the each row compare itself to the row above it, and then do something (such as change font color or <td>
background color), how would I do that? For example, the 13 and 22 would change to boldface or red or something, and the 12's not change.
you can keep track of the previous row by setting $prev = $row at the end of the loop, then compare against prev, $color = ($prev && $row['x'] > $prev['x']) ? 'green' : 'red';
for something more advanced you can get all the rows into an array first: $rows = array(); while ($row = mysql_fetch_assoc($result)){ $rows[] = $row; }
精彩评论