3

How can I have an alternating color rows in my php loop?

$num = mysql_num_rows($qPhysician);

$i=0;

while($i < $num)

{

    echo "<tr>";
    echo "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";
    echo "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";
    echo "</tr>";

    $i++;

}

I have to omit the "<" and ">" for both "tr" and "td" because it wasn't allowed in this question. :)

thanks!

3
  • you've had your question's formatting fixed for you twice -- slow down and figure it out. Highlight the code portion and click the {} symbol in the editor, please. The <> are allowed in your sample if you mark it as code. Commented Jul 17, 2011 at 5:00
  • are you talking about alternate colors in the table or selecting data only from alternate indexes -- please clarify? Commented Jul 17, 2011 at 5:04
  • sorry about the confusion. Yes it is alternating color rows :) Commented Jul 17, 2011 at 5:07

5 Answers 5

10

What do you mean? Are you saying you want to echo it in a table that alternates rows?

$num = mysql_num_rows($qPhysician);
$i=0;
echo "<table>"
while($i < $num)

{
if ($i % 2 == 0){
echo "<tr class='style1'>";
}
else{
echo "<tr class='style2'>";
}
echo "<td>" . mysql_result($qPhysician,$i,"lastName") . "</td>";

echo "<td>" . mysql_result($qPhysician,$i,"firstName") . "</td>";

echo "</tr>";

$i++;

}
echo "</table>";
Sign up to request clarification or add additional context in comments.

Comments

2

Continuing with the example here:

$query = mysql_query("SELECT lastName, firstName FROM physicians");

$i = 0;
while( $arr = mysql_fetch_assoc( $query ) )
{
    // use modulus (%). It returns the remainder after division.
    // in this case, $i % 2 will be 1 when $i is odd, 0 when even.
    // this is the ternary operator. 
    // it means (if this)? do this: otherwise this        
    // (Remember 1 is true and 0 is false so odd rows will be the odd
    // class, even rows the even class)
    echo ($i % 2)?'<tr class="odd">':'<tr class="even">';
    // Now, use array indexing.
    echo "<td>" . $arr[ "lastName" ] . "</td>";
    echo "<td>" . $arr[ "firstName" ] . "</td>";
    echo "</tr>";
    $i++;
}

4 Comments

this works, BUT it only displays 2 rows. Currently, I have 3 rows in my DB.
What happens when you do a select * from physicians?
Because the mysql_fetch functions pretty much have to work or you have BIG problems.
this is my query: $qPhysician = mysql_query("SELECT * FROM physicians ORDER BY lastName ASC, firstName ASC");
0

inside your while you can use:

if ($i % 2 == 0)
   echo "even";
else
   echo "odd";

1 Comment

You're welcome. But I think it`s not about giving a fish but a fishing rod. ;)
0
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$dbname=""; // Database name
$tblname=""; // Table name
// Connect to server and select databse
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$sql="SELECT * FROM $tblname";
$result=mysql_query($sql);
// Define $color=1
$color="1";
echo '<h3 align = "center"> Details <hr /></h3>';
echo '<table width="400" border="1" align="center" cellpadding="2" cellspacing="0">';
while($rows=mysql_fetch_row($result)){
// If $color==1 table row color = #FFCCFF
if($color == 1){
echo "<tr bgcolor='#FFCCFF'><td>$rows[0]</td><td>$rows[1]</td><td>$rows[2]</td><td>$rows[3]</td></tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, table row color = #FFC600
else {
echo "<tr bgcolor='#FFC600'><td>$rows[0]</td><td>$rows[1]</td><td>$rows[2]</td><td>$rows[3]</td></tr>";
// Set $color back to 1
$color="1";
}
}
echo '</table>';
mysql_close();
?>

Comments

0

Here's a simple trick if you're looking to alternate a group of colors.

First add some css in the head of the document.

    <style>

    div.red {
        background-color: #E87876;
    }

    div.burnt {
        background-color: #E89576;
    }

    div.orange {
        background-color: #E8B176;
    }

    div.mustard {
        background-color: #E8CE76;
    }

    div.yellow {
        background-color: #E6E876;
    }

    div.green {
        background-color: #CAE876;
    }

    </style>

Then add the colors to your loop.

    $colors = array('red','burnt','orange','mustard','yellow','green');
    $rowCount=0;

        while($row = mysql_fetch_array($sql))  
          { 
          $something=$row['data'];  
          if($rowCount==6) // number of colors in array 
            {
            $rowCount=0;   // reset to first color
            }

        echo "<div class=\"".$colors[$rowCount]."\">".$something."</div>";      

        $rowCount++;

          }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.