Max Veteran Posted February 16, 2010 Veteran Share Posted February 16, 2010 Below is the output code for a site I run. This goes on the website and it presents a list of events for a band from a database. I would like every other event to be in bold when viewed. Being no PHP expert, I was wondering if someone could quickly modify the code to do such a thing. Thanks for any help :) <table> <tr> <td width="80"><strong>Date</strong></td> <td width="70"><strong>Day</strong></td> <td><strong>Location</strong></td> </tr> <?php mysql_connect('localhost', 'thehorse_horsey', '*********') or die(mysql_error()); mysql_select_db("thehorse_horseband") or die(mysql_error()); $sql = "SELECT * FROM `event` WHERE `event`.`deleted` = '0' ORDER BY `event`.`date` ASC"; $result = mysql_query($sql); if(mysql_num_rows($result) >= 1) { while($e = mysql_fetch_object($result)) { echo ' <tr> <td>'. date("d/m/Y", $e->date) .'</td> <td>'. $e->day .'</td> <td>'. $e->location .'</td> </tr> '; } } else { echo ' <tr> <td colspan="3" class="no-rows">No Events</td> </tr> '; } ?> </table> Link to comment Share on other sites More sharing options...
0 Antaris Veteran Posted February 16, 2010 Veteran Share Posted February 16, 2010 You can use the modulo operator (%) to determine if the row is odd or even. if(mysql_num_rows($result) >= 1) { $row = 'odd'; $i = 1; while($e = mysql_fetch_object($result)) { if (($i % 2) == 0) { $row = 'even'; } else { $row = 'odd'; } $i = $i + 1; echo '<tr class="'. $row .'"> <td>'. date("d/m/Y", $e->date) .'</td> <td>'. $e->day .'</td> <td>'. $e->location .'</td> </tr>'; } } else { echo '<tr> <td colspan="3" class="no-rows">No Events</td> </tr>'; } Link to comment Share on other sites More sharing options...
0 sweetsam Posted February 16, 2010 Share Posted February 16, 2010 <table> <tr> <td width="80"><strong>Date</strong></td> <td width="70"><strong>Day</strong></td> <td><strong>Location</strong></td> </tr> <?php $row = ''; mysql_connect('localhost', 'thehorse_horsey', '*********') or die(mysql_error()); mysql_select_db("thehorse_horseband") or die(mysql_error()); $sql = "SELECT * FROM `event` WHERE `event`.`deleted` = '0' ORDER BY `event`.`date` ASC"; $result = mysql_query($sql); if(mysql_num_rows($result) >= 1) { while($e = mysql_fetch_object($result)) { if ( $row == '') { $row = 'class="bold"'; } else { $row = ''; } echo ' <tr ' . $row . '> <td>'. date("d/m/Y", $e->date) .'</td> <td>'. $e->day .'</td> <td>'. $e->location .'</td> </tr> '; } } else { echo ' <tr> <td colspan="3" class="no-rows">No Events</td> </tr> '; } ?> </table> Link to comment Share on other sites More sharing options...
0 Kudos Veteran Posted February 16, 2010 Veteran Share Posted February 16, 2010 (edited) Guys, for loops. ... if(mysql_num_rows($result) >= 1) { for($i=0;$e = mysql_fetch_object($result);$i++) { echo ' <tr'.($i%2 == 0 ?: ' class="bold"').'> <td>'. date("d/m/Y", $e->date) .'</td> <td>'. $e->day .'</td> <td>'. $e->location .'</td> </tr> '; } ... The ternary operator needs a slight modification if you're not running 5.3 Link to comment Share on other sites More sharing options...
0 Mike Posted February 16, 2010 Share Posted February 16, 2010 Do you really need the number of rows check? If there are no rows then the for() loop won't get executed anyway. Link to comment Share on other sites More sharing options...
0 Kudos Veteran Posted February 16, 2010 Veteran Share Posted February 16, 2010 Do you really need the number of rows check? If there are no rows then the for() loop won't get executed anyway. The check is needed to otherwise output the "No Events" message. Link to comment Share on other sites More sharing options...
0 Mike Posted February 16, 2010 Share Posted February 16, 2010 Good point, didn't real all the code in the 1st post :p Link to comment Share on other sites More sharing options...
0 Max Veteran Posted February 18, 2010 Author Veteran Share Posted February 18, 2010 Thanks all :) Link to comment Share on other sites More sharing options...
Question
Max Veteran
Below is the output code for a site I run.
This goes on the website and it presents a list of events for a band from a database.
I would like every other event to be in bold when viewed. Being no PHP expert, I was wondering if someone could quickly modify the code to do such a thing.
Thanks for any help :)
<table> <tr> <td width="80"><strong>Date</strong></td> <td width="70"><strong>Day</strong></td> <td><strong>Location</strong></td> </tr> <?php mysql_connect('localhost', 'thehorse_horsey', '*********') or die(mysql_error()); mysql_select_db("thehorse_horseband") or die(mysql_error()); $sql = "SELECT * FROM `event` WHERE `event`.`deleted` = '0' ORDER BY `event`.`date` ASC"; $result = mysql_query($sql); if(mysql_num_rows($result) >= 1) { while($e = mysql_fetch_object($result)) { echo ' <tr> <td>'. date("d/m/Y", $e->date) .'</td> <td>'. $e->day .'</td> <td>'. $e->location .'</td> </tr> '; } } else { echo ' <tr> <td colspan="3" class="no-rows">No Events</td> </tr> '; } ?> </table>Link to comment
Share on other sites
7 answers to this question
Recommended Posts