php在行中打印不同的颜色

时间:2012-12-15 03:23:37

标签: php

我尝试打印出来自表格的所有用户电子邮件, 但我想用红色打印奇数 偶数打印蓝色。 如何在while循环中执行此操作?

$connect=mysql_connect("localhost", "root", "");
$database=mysql_select_db("phplogin", $connect);

$SQL=mysql_query("SELECT * FROM users");
$num=mysql_num_rows($SQL);
$start=0;

while($start<$num){
    $result_id=mysql_result($SQL, $start, "id");
    $result_email=mysql_result($SQL, $start, "email");

    echo "<b style='color:red'>" . $result_id . $result_email . "</b><br/>";
    echo "<b style='color:blue'>" . $result_id . $result_email . "</b><br/>";

    $start++;
    }

1 个答案:

答案 0 :(得分:4)

$connect=mysql_connect("localhost", "root", "");
$database=mysql_select_db("phplogin", $connect);

$SQL=mysql_query("SELECT * FROM users");
$num=mysql_num_rows($SQL);
$start=0;

while($start<$num){
    $result_id=mysql_result($SQL, $start, "id");
    $result_email=mysql_result($SQL, $start, "email");
    if ($start&1) {
      echo "<b style='color:red'>" . $result_id . $result_email . "</b><br/>";
    }
    else {
      echo "<b style='color:blue'>" . $result_id . $result_email . "</b><br/>";
    }
    $start++;
    }