简体   繁体   English

PHP交替颜色

[英]PHP alternating colors

$dbc = mysql_connect('localhost','root','') or die (mysql_error());
  mysql_select_db('payroll') or die (mysql_error());
 $sql = "SELECT * FROM employee ORDER BY employee_id DESC";

  $result = mysql_query($sql);

  while($row = mysql_fetch_array($result)) {

  echo "
  <tr>
  <td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
  <td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
  </tr>
  ";

  }

I wanted to add an alternating color in the loop. 我想在循环中添加一种交替的颜色。 what code should I add? 我应该添加什么代码?

$rowCount = 0;
$colorOne = '#ffffff';
$colorTwo = '#f3f3f3';

while($row = mysql_fetch_array($result)){
        $rowColor = ($rowCount % 2) ? $colorOne : $colorTwo; 
        echo "<element bgcolor='$rowColor'></element>";
$rowCount++;
}

Edited after @Jayrox comment.

Use CSS classes instead of inline styles. 使用CSS类代替内联样式。 They're easier to manipulate. 它们更易于操纵。 Then style the .myclass_row_0 and .myclass_row_1 in your CSS for the alternate row color as well as .col1 and .col2 for the column styles. 然后风格的.myclass_row_0.myclass_row_1在你的CSS了备用行颜色以及.col1.col2为列样式。

$c=1; // or 0 (added after deceze's comment)
while($row = mysql_fetch_array($result)) {
  $c=1-$c; // magic!

  echo "
    <tr class=\"myclass_row_$c\">
      <td class=\"col1\">".$row['first_name']." ".$row['last_name']."</td>
      <td class=\"col2\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
    </tr>
  ";
}

Or using inline styles: 或使用内联样式:

$colors = array('#ffffff','#f3f3f3');
$c=1;
while($row = mysql_fetch_array($result)) {
  $c=1-$c;

  echo "
    <tr style=\"background-color:{$colors[$c]};\">
       <!-- tds here -->
    </tr>
  ";
}

Or using preset classes: 或使用预设类:

$styles = array('odd','even');
$c=1;
while($row = mysql_fetch_array($result)) {
  $c=1-$c;

  echo "
    <tr class=\"{$styles[$c]}\">
       <!-- ... -->
    </tr>
  ";
}

There are already a lot of solutions, but this one use only two lines of css. 已经有很多解决方案,但是这一解决方案仅使用两行CSS。

You can set specific css on even and odd rows: 您可以在偶数和奇数行上设置特定的CSS:

tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}

Usually, I will declare an index(int) value and increment for each time through the loop. 通常,我将声明index(int)值,并在每次循环中都递增。 Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row. 然后检查索引是否为mod 2 =1。如果是,则输出具有您要应用的样式的tr,以显示交替的行。

$color = 0;
while($row = mysql_fetch_array($result)) {

  if($color % 2 == 1){
    echo "<tr>";
  }else{
    echo "<tr class=\"altRow\">";
  }
  echo "
  <td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
  <td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
  </tr>
  ";
  $color++;
}

As it's bumped already, here is my 5 cents. 因为已经碰到了,这是我的5美分。

7 answers and not a single one using templates. 7个答案,而不是使用模板的答案。
We can write a thousand articles of the templates necessity, but such an examples will always win. 我们可以写成千上万的模板必要性文章,但是这样的例子将永远赢。

So, based on the OP's code and stagas' answer: 因此,基于OP的代码和stagas的答案:

business logic part: 业务逻辑部分:

$c      = 1;
$DATA   = array();
$sql    = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($result)) {
  $row['c'] = $c=1-$c;
  $DATA[] = $row;
}

and template part: 和模板部分:

<tr class="myclass_row_<?=$row['$c']?>">
  <td class="col1"><?=$row['first_name']?> <?=$row['last_name']?></td>
  <td class="col2">
    <input type="button" name="edit" value="Edit" class="selbtn">&nbsp;<input type="button" name="delete" value="Delete" class="selbtn">
  </td>
</tr>
$color = 1;
while($row = mysql_fetch_array($result)) {

if($color == 1){ 
  echo "
  <tr>
  <td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
  <td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
  </tr>
  ";
$color = 2;
}
else
{
  echo "
  <tr>
  <td style=\"padding-left: 20px; border-bottom: 1px solid #555; border-right: 1px solid #555;\">".$row['first_name']." ".$row['last_name']."</td>
  <td style=\"text-align: center; border-bottom: 1px solid #555; border-right: 1px solid #555;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
  </tr>
  ";
  }
$color = 1;
}
// here it is in less code

<?php
 $dbc = mysql_connect('localhost','root','') or die (mysql_error());
  mysql_select_db('payroll') or die (mysql_error());
 $sql = "SELECT * FROM employee ORDER BY employee_id DESC";

  $result = mysql_query($sql);
  $colors = array('FFF', '000000'); // valid hex colors
  $numOfColors = sizeOf($colors); $i = 0

  while($row = mysql_fetch_array($result)) {
  $i++;if($i>$numOfColors){$i=0;}
?>

  <tr>
  <td style="padding-left: 20px; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>;"> <?=$row['first_name']?> <?=$row['last_name']?> </td>
  <td style="text-align: center; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>;  padding-top: 2px ; padding-bottom: 3px ;"><input type="button" name="edit" value="Edit" class="selbtn">&nbsp;<input type="button" name="delete" value="Delete" class="selbtn"></td>
  </tr>

<?php

  }
?>

Just keep track whether it is an alternating row with a Boolean. 只需跟踪它是否是一个带有布尔值的交替行即可。 Initialize it to false before your loop, not it for each iteration, then you can set the row style based on its value. 在循环之前将其初始化为false ,而不是在每次迭代时将其初始化为false ,然后可以根据其值设置行样式。 Something like: 就像是:

...

$isAlternatingRow = false;

while($row = mysql_fetch_array($result)) {

    echo "
       <tr class=\"" . $isAlternatingRow ? "defaultRow" : "alternatingRow" . "\">
       <td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
       <td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999;  padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\">&nbsp;<input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
       </tr>
       ";

    $isAlternatingRow = !($isAlternatingRow);
}

Then just define styles for tr.defaultRow td and tr.alternatingRow td . 然后只需为tr.defaultRow tdtr.alternatingRow td定义样式。

$class="even"
while($row = mysql_fetch_array($result)) 
{
  if($class == "even")
  {
    echo "<tr class='$class'>";
    $class="odd"
  }
  else
  {
    echo "<tr class='$class'>";
    $class="even";
  }

  ...

}

Using predefined color outside the loop. 在循环外使用预定义的颜色。

$rowCount = 0;
$color = array('#ffffff','#f3f3f3');
while($row = mysql_fetch_array($result)){
    $i = ($rowCount % 2); 
    echo "<element bgcolor='".$color["$i"]."'></element>";
    $rowCount++;
}

I did it like this: 我这样做是这样的:

Remember to add a CSS class called "even", with styling of course. 记住要添加一个样式为CSS的名为“ even”的类。

<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
   if($i % 2 == 0)
   {
      echo "<tr class='even'>";
      echo "<td>" . $row['something'] . "</td>";
      echo "</tr>";
   }

   else
   {
      echo "<tr>";
      echo "<td>" . $row['something'] . "</td>";
      echo "</tr>";
   }
   $i++;
}
echo "</table>";

mysql_close($con);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM