• 0

[PHP] Making every other line bold


Question

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

  • 0

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

  • 0
<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

  • 0

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

  • 0

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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.