Alternating color rows in PHP loop
How c开发者_开发问答an 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!
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>";
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++;
}
inside your while you can use:
if ($i % 2 == 0)
echo "even";
else
echo "odd";
<?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();
?>
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++;
}
精彩评论