Hello everyone!

I've been able to successfully implement this bit of php that alters the color of tables for each row, but I am having a difficult time implementing it on our current web app. I am thinking I have misplaced something because the output shows one grey row where all the rest are green. I am thinking the variable is not able to loop for some knuckle-headed reason.

Any directon provided is greatly appreciated.

<?php
for ($j = 0; $j < mysql_num_rows($result); $j++)
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"last_name");
$f2=mysql_result($result,$i,"first_name");
...blah blah blah

This is the first part where the artihmatic is established working in conjunction $j changes the $bg_color variable and i$ communicates with mysql to display the data.

$f16=mysql_result($result,$i,"texting");
$f17=mysql_result($result,$i,"id");

if ($j % 2)
        {
            $bg_color="#EEEEEE";
        }
        else
        {
            $bg_color="#E0E0E0";
        }
?>
<tr bgcolor="'.$bg_color.'">
<td width="15"> &nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td width="15">&nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>

Do I need to include a PHP tag in the <tr>?

This loop is only executing the single line after it for one thing

for ($j = 0; $j < mysql_num_rows($result); $j++)
$i=0;

There are no braces for the larger block.

I'll leave you to fit the rest of your code in, but here is the alternating row color part

<?php
    for ($j=0; $j<10; $j++){
        $bg_color= $j % 2 ? "#EEEEEE":"#E0E0E0";
    ?>
        <tr style="background-color:<?php echo $bg_color ?>;">
            <td width="15"> &nbsp<font face="Arial, Helvetica, sans-serif">1</font></td>
            <td width="15">&nbsp<font face="Arial, Helvetica, sans-serif">2</font></td>
        </tr>
    <?php 
    }
    ?>

I'll leave you to fit the rest of your code in, but here is the alternating row color part

<?php
    for ($j=0; $j<10; $j++){
        $bg_color= $j % 2 ? "#EEEEEE":"#E0E0E0";
    ?>
        <tr style="background-color:<?php echo $bg_color ?>;">
            <td width="15"> &nbsp<font face="Arial, Helvetica, sans-serif">1</font></td>
            <td width="15">&nbsp<font face="Arial, Helvetica, sans-serif">2</font></td>
        </tr>
    <?php 
    }
    ?>

Thank you for this!

Okay, i've applied this code and changed the value of 10 to mysql_num_rows($result). This part works fine and the script is producing the correct number of rows. The problem now is that the loop is displaying each row 15 times because the loop is occuring twice? Once with $i and once with $j?. I am not sure what logically is wrong here...

<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"last_name");
$f2=mysql_result($result,$i,"first_name");
...
$f17=mysql_result($result,$i,"id");
?>
<?php
    for ($j=0; $j<mysql_num_rows($result); $j++){
    $bg_color= $j % 2 ? "#EEEEEE":"#E0E0E0";
?>
<tr style="background-color:<?php echo $bg_color ?>;">
<td> &nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td>&nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
...
<td>&nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f17; ?></font></td>
</tr>
</tr>
<?php
}
?>


<?php
$i++;
}
?>

You have this second loop on $i around the whole thing

while ($i < $num) {

You have this second loop on $i around the whole thing

while ($i < $num) {

Hmm...I apologize. There's something i'm not getting..

<?php
    for ($j=0; $j<mysql_num_rows($result); $j++){
    $bg_color= $j % 2 ? "#EEEEEE":"#E0E0E0";
tr style="background-color:<?php echo $bg_color ?>;">
<td> &nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
...
<td>&nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f17; ?></font></td>
</tr>
</tr>
}
?>

Changed to:

<?php
    for ($j=0; $j<mysql_num_rows($result); $j++){
    $bg_color= $j % 2 ? "#EEEEEE":"#E0E0E0";
    }
?>
tr style="background-color:<?php echo $bg_color ?>;">
<td> &nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>

This change eliminated the problem of looping the number of times a row appears in the table by mysql_num_rows; however, the rows are not alternating colors.

I have not changed the $i loop around the entire code because it seems to be functioning correctly.

You only posted portions of the code, but it looked like you had two loops running through the result set where you only need one. It doesn't matter which one of them you remove really - just don't loop them twice.

It you actually look at and think about what the loop that you changed is doing, you'll see why your row colors don't alternate. Setting the $bg_color 'mysql_num_rows' times doesn't really do much if you aren't using that variable in the loop. If you are using $i as a counter, use that in the bg_color expression instead and take the $j loop out completely.

Thought I could give some input in how to look at the problem in a different way.

1. Don't use <font> tags. Ever. Really. Never ever. They are evil. Use classes and css instead.
2. Why use mysql_result and save each in a variable? mysql_fetch_assoc does what you want, and it's easier.

Take the following code:

$tableRowClasses = array("row1", "row2");
$tableClassCounter = 0;

$result = #Do the query
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

print("<table>");
while ($row = mysql_fetch_assoc($result)) {
    $tableClassCounter = ($tableClassCounter + 1) % 2;
    print("<tr class=\"".$tableRowClasses[$tableClassCounter]."\">");
    foreach($row as $value) {
        print("<td>".$value."</td>");
    }
    print("</td>");
}
print("</table>");

It's been a while since I've coded php, so this code is not testet. Might not work at all, but it's the general idea I wanted to show.

What you now do is create some css that looks something like:

td {
    font-family: Arial, Helvetica, sans-serif;
}
.row1 {
    background-color: #EEEEEE;
}

.row2 {
    background-color: #E0E0E0;
}

This should get you a nice table with each row having an alternate background color. The ordering of the columns depends on the ordering of the columns returned from the sql-query. If it's in the wrong order - reorder your select statement. And there's no font-tags being used! Just plain old pretty html :)

Please try

$f16=mysql_result($result,$i,"texting");
$f17=mysql_result($result,$i,"id");

if ($j % 2)
        {
            $bg_color="#EEEEEE";
        }
        else
        {
            $bg_color="#E0E0E0";
        }
?>
<tr bgcolor="<? echo $bg_color;?>">
<td width="15"> &nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td>
<td width="15">&nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>

$f16=mysql_result($result,$i,"texting"); $f17=mysql_result($result,$i,"id"); if ($j % 2) { $bg_color="#EEEEEE"; } else { $bg_color="#E0E0E0"; } ?> <tr bgcolor="'.$bg_color.'"> <td width="15"> &nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td> <td width="15">&nbsp<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.