--- Mike Brandonisio <[EMAIL PROTECTED]> wrote:
> Here is one way. Some else might a more compact version.
> 
> $row = 0;
> foreach( $result as $value ) {
>    if ( is_int($row/2) ) {
>       echo "<i>italic text</i>";
>    }
>    $row++;
> }

Obviously, the above is more like pseudo-code since $value is not used inside
the loop and the text isn't printed as plain text.

$row = 0;
foreach ($result as $value)
{
 print ($row++ %2) ? "<i>$value</i><br />\n" : "$value<br />\n";
}

By way of explanation, the % operator looks at the remainder of an integer
division.  Take any number, divide it by 2 and look at the remainder.  It will
be 1 or 0.  PHP interprets these numbers as true or false.

The "(condition) ? value_if_true : value_if_false" structure is a compact way
of working an if...else branch.  Both values must be supplied, even if one is
"".

The ++ operator after $row increments the variable after it is used in the
modulus (%) operation.

Setting $row to zero is optional but will prevent warnings.

James Keeline

Reply via email to