2011-07-17 91 views
2

如何在我的php循环中交替颜色行?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”,因为它并没有在这个问题上允许的。 :)

谢谢!

+0

你有问题的格式为你解决了两次 - 慢下来,弄明白。突出显示代码部分,然后单击编辑器中的“{}”符号。如果您将标记为代码,则可以在您的示例中使用<<>。 – Erik

+0

您是在谈论表格中的替代颜色还是仅从备用索引中选择数据 - 请澄清? – treecoder

+0

对此感到抱歉。是的,它是交替颜色行:) –

回答

8

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

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

这工作得很好!谢谢! –

0

你里面,而你可以使用:

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

谢谢!凯文王能够阐述更多。 –

+0

不客气。但我认为这不是关于给鱼,而是钓鱼竿。 ;) – armandomiani

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++; 
} 
+0

这个工作,但它只显示2行。目前,我的数据库中有3行。 –

+0

当你从医生那里选择*时会发生什么? – cwallenpoole

+0

因为mysql_fetch函数几乎不得不工作,或者你有* BIG *问题。 – cwallenpoole

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(); 
?> 
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++; 

      }