PHP循环中的交替颜色行

时间:2011-07-17 04:59:38

标签: php while-loop rows alternating

如何在php循环中使用交替的颜色行?

$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++;

}

我必须省略“&lt;”和“&gt;”对于“tr”和“td”,因为在这个问题中不允许这样做。 :)

谢谢!

5 个答案:

答案 0 :(得分:9)

你是什​​么意思?你是说你想在一个交替行的表中回显它吗?

$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>";

答案 1 :(得分:2)

继续示例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++;
}

答案 2 :(得分:0)

在您可以使用的时间内:

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

答案 3 :(得分: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();
?>

答案 4 :(得分:0)

如果您想要替换一组颜色,这是一个简单的技巧。

首先在文档的头部添加一些css。

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

然后将颜色添加到循环中。

    $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++;

          }