smti Posted May 17, 2008 Share Posted May 17, 2008 Hello, I have a small problem; I have a table which displays records pulled from a database. This table has different colors (denoted by the class tag -- see code below). Anyway, I want to be able to pull the records from the database and alternate the table colors properly. At the moment, when I grab the data, it displays the records in alternating colors, but the data is the same. I'm not quite sure what the problem is. Here is my code: <b>(A screen shot is also available).</b> echo "<tbody> <tr> <td>$row[mrn]</td> <td>$row[auditdate]</td> <td>$row[auditorid]</td> <td>$row[codername]</td>"; //Grab ID and put in variable (REQUIRED!) $id=$row[id]; echo "<td><img src='images/icons/view.png' alt='View Record'/> <img src='images/icons/pencil.png' /> <a href='../../../functions/delete.php?id=$id'><img src='images/icons/trash.png' /></a></td> </tr> <tr class='alt'> <td>$row[mrn]</td> <td>$row[auditdate]</td> <td>$row[auditorid]</td> <td>$row[codername]</td> <td><img src='images/icons/view.png' alt='View Record'/> <img src='images/icons/pencil.png' /> <a href='../../../ functions/delete.php?id=$id'><img src='images/icons/trash.png' /></a></td> </tr>"; } ?> Please also see the attached image. Any help would be greatly appreciated! Thanks, smti [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/106011-solved-data-loop/ Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Show us the query, and are you using a while() statement around the code you posted? I assume so. Quote Link to comment https://forums.phpfreaks.com/topic/106011-solved-data-loop/#findComment-543314 Share on other sites More sharing options...
smti Posted May 17, 2008 Author Share Posted May 17, 2008 Hello. As you requested, the query looks like this: $result = mysql_query("SELECT * FROM cases_bloomsburg") or die(mysql_error()); // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table Quote Link to comment https://forums.phpfreaks.com/topic/106011-solved-data-loop/#findComment-543346 Share on other sites More sharing options...
DarkWater Posted May 17, 2008 Share Posted May 17, 2008 Run the query in the MySQL client and make sure the output from MySQL is appropriate. Quote Link to comment https://forums.phpfreaks.com/topic/106011-solved-data-loop/#findComment-543353 Share on other sites More sharing options...
Barand Posted May 17, 2008 Share Posted May 17, 2008 try <?php $result = mysql_query("SELECT * FROM cases_bloomsburg") or die(mysql_error()); $count=0; echo '<tbody>'; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table $classStr = $count++ % 2 ? '' : "class='alt'"; echo "<tr $classStr> <td>{$row['mrn']}</td> <td>{$row['auditdate']}</td> <td>{$row['auditorid']}</td> <td>{$row['codername']}</td>"; //Grab ID and put in variable (REQUIRED!) $id=$row['id']; echo "<td><img src='images/icons/view.png' alt='View Record'/> <img src='images/icons/pencil.png' /> <a href='../../../functions/delete.php?id=$id'><img src='images/icons/trash.png' /></a></td> </tr>"; } echo '</tbody>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/106011-solved-data-loop/#findComment-543419 Share on other sites More sharing options...
smti Posted May 17, 2008 Author Share Posted May 17, 2008 Works like a charm! Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/106011-solved-data-loop/#findComment-543496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.