Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP循环中的交替颜色行_Php_While Loop_Rows_Alternating - Fatal编程技术网

PHP循环中的交替颜色行

PHP循环中的交替颜色行,php,while-loop,rows,alternating,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,"firstNam

如何在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的,因为在这个问题中是不允许的:


谢谢

在您的电脑中,您可以使用:

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

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

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

继续这个例子:


如果你想改变一组颜色,这里有一个简单的技巧

首先在文档的头部添加一些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++;

          }

你已经两次修正了你的问题格式——放慢速度,把它弄清楚。请突出显示代码部分并单击编辑器中的{}符号。如果您将样本标记为代码,则允许在样本中使用。您是在表中谈论替代颜色,还是仅从替代索引中选择数据?请澄清?很抱歉造成混淆。是的,它是交替颜色行:这是可行的,但它只显示2行。目前,我的数据库中有3行。当您从医生处选择*时会发生什么情况?因为mysql_fetch函数几乎必须工作,或者您有很大的问题。这是我的查询:$QPhysican=mysql_querySELECT*从医生处按姓氏ASC、姓氏ASC排序;不客气。但我认为这不是给鱼而是给鱼竿;
    <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++;

          }