PHP多个数组while循环使用if条件

时间:2017-03-01 18:55:39

标签: php mysql arrays mysqli

我是PHP的新手,iam尝试根据你在IF块中可以看到的条件为column4,column7,column9的单元格着色,下面是我尝试的代码,请帮助我理解如何实现这一点。我正在使用数组,因为我有超过80列,在下面的例子中,iam只显示10个用于解释。

<?php

$con=mysqli_connect("server","user","password","db");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM table1");

echo "<table id='table_id' class='mytable'>

<thead>
<tr>
<th class='heading'>Column1</th>
<th class='heading'>Column2</th>
<th class='heading'>Column3</th>
<th class='heading'>Column4</th>
<th class='heading'>Column5</th>
<th class='heading'>Column6</th>
<th class='heading'>Column7</th>
<th class='heading'>Column8</th>
<th class='heading'>Column9</th>
<th class='heading'>Column10</th>
</tr>
</thead>";
echo "<tbody>";

while ($row = mysqli_fetch_array($result))
{
$colorfields = array($row['Column4'],$row['Column7'],$row['Column9']);


if ($colorfields < 195 && $colorfields !='')
$classname = "red"; 
else if($colorfields >= 195 && $colorfields < 199.99)
$classname = "yellow";
else if($colorfields >= 199.99)
$classname = "green";
echo "<tr>";
echo "<td class='normal_cell'>" . $row['Column1']."</td>";
echo "<td class='normal_cell'>" . $row['Column2']."</td>";
echo "<td class='normal_cell'>" . $row['Column3']."</td>";
echo "<td class=".$classname.">". $row['Column4']."</td>";
echo "<td class='normal_cell'>" . $row['Column5']."</td>";
echo "<td class='normal_cell'>" . $row['Column6']."</td>";
echo "<td class=".$classname.">". $row['Column7']."</td>";
echo "<td class='normal_cell'>" . $row['Column8']."</td>";
echo "<td class=".$classname.">". $row['Column9']."</td>";
echo "<td class='normal_cell'>" . $row['Column10']."</td>";
echo "</tr>";
}

echo "</tbody>";

echo "</table>";

mysqli_close($con);
?> 

2 个答案:

答案 0 :(得分:2)

我会编写一个返回'red','yellow'或'green'的函数,具体取决于作为参数传入的值。

 function class_from_val($val) {
    $classname = '';
    if( $val < 195 && $val !='' ) {
       $classname = 'red';
    } else if( $val >= 195 && $val < 199.99 ) {
       $classname = 'yellow';
    } else if( $val >= 199.99 ) {
       $classname = 'green';
    } else {
      // ? val = ""
    }
    return $classname;
 }

然后我会调用我需要返回类名的函数

 echo "<td class='normal_cell'>"                        .$row['Column3']."</td>";
 echo "<td class='".class_from_val($row['Column4'])."'>".$row['Column4']."</td>";
 echo "<td class='normal_cell'>"                        .$row['Column5']."</td>";
 echo "<td class='normal_cell'>"                        .$row['Column6']."</td>";
 echo "<td class='".class_from_val($row['Column7'])."'>".$row['Column7']."</td>";

答案 1 :(得分:2)

遵循以下一般原则:

  • 缩进代码和空格
  • 仔细选择您的变量名称
  • 划分和规则(使用功能)

示例:

<div class="image-grid">

  <div class="image-grid__image">
    <img src="https://placehold.it/180x120/fff/000">
  </div>
  <div class="image-grid__image">
    <img src="https://placehold.it/180x120/fff/000">
  </div>
  <div class="image-grid__image">
    <img src="https://placehold.it/180x120/fff/000">
  </div>

</div>