3

如何在我的 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++;

}

我必须省略“tr”和“td”的“<”和“>”,因为在这个问题中不允许这样做。:)

谢谢!

4

5 回答 5

10

你什么意思?您是说要在交替行的表格中回显它吗?

$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>";
于 2011-07-17T05:04:00.427 回答
2

继续此处的示例:

$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++;
}
于 2011-07-17T05:04:40.947 回答
0

在你的里面你可以使用:

if ($i % 2 == 0)
   echo "even";
else
   echo "odd";
于 2011-07-17T05:01:24.517 回答
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();
?>
于 2016-04-05T10:31:42.750 回答
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++;

          }
于 2018-03-09T06:10:20.443 回答