带if条件的PHP多数组while循环

带if条件的PHP多数组while循环,php,mysql,arrays,mysqli,Php,Mysql,Arrays,Mysqli,我是PHP新手,我正在尝试根据IF块中可以看到的条件对第4列、第7列、第9列的单元格着色,下面是我尝试的代码,请帮助我理解如何实现这一点。我之所以使用数组,是因为我有80多列,在下面的示例中,iam仅显示了10列来解释 <?php $con=mysqli_connect("server","user","password","db"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_

我是PHP新手,我正在尝试根据IF块中可以看到的条件对第4列、第7列、第9列的单元格着色,下面是我尝试的代码,请帮助我理解如何实现这一点。我之所以使用数组,是因为我有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);
?> 

我将编写一个函数,根据作为参数传入的值返回“红色”、“黄色”或“绿色”

 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;
 }
function class_from_val($val){
$classname='';
如果($val<195&$val!=''){
$classname='red';
}如果($val>=195&$val<199.99),则为其他情况{
$classname='yellow';
}否则,如果($val>=199.99){
$classname='green';
}否则{
//?val=“”
}
返回$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>";
echo“$row['Column3']”;
回显“$row['Column4']”;
回显“$row['Column5']”;
回显“$row['Column6']”;
回显“$row['Column7']”;

遵循以下一般原则:

  • 缩进代码并将其隔开
  • 仔细选择变量名
  • 分而治之(使用函数)
例如:

function getClassName($col, $field) {
    if ( !in_array($col, [ 'Column4', 'Column7', 'Column9' ]) || empty($field) )
        return 'normal_cell';

    if ( $field >= 199.99 )
        return 'green';

    if ( $field >= 195 )
        return 'yellow';

    return 'red';
}

$cellFormat = '<td class="%s">%s</td>';

while ($row = mysqli_fetch_array($result)) {
    echo '<tr>';

    foreach ($row as $col => $field) {
        printf($cellFormat, getClassName($col, $field), $field);
    }

    echo '</tr>';
}
函数getClassName($col,$field){
if(!in_数组($coln,['Column4','Column7','Column9'])| |空($field))
返回“正常_单元”;
如果($field>=199.99)
返回“绿色”;
如果($field>=195)
返回“黄色”;
返回“红色”;
}
$cellFormat='%s';
while($row=mysqli\u fetch\u数组($result)){
回声';
foreach($col=>$field的行){
printf($cellFormat,getClassName($col,$field),$field);
}
回声';
}

您面临什么问题?您的代码很奇怪,
$colorfields
是一个数组,您无法将其与字符串或数字等标量值进行比较。这些神奇的价值是什么:195199.99?我认为只有使用css3选择器才能解决这个问题。@Anant,无论IF条件为真,我都没有得到单元格的预期颜色,相反,我在所有单元格上都得到了绿色。@CasimiritHippolyte,我对PHP完全陌生,刚刚开始学习。@davidb检查下面的答案谢谢,我正在尝试您的解决方案,我得到的是空白页。使用echo输出函数返回值。echo class_from_val($row['Column4'])@RavinderReddy:是的,可以使用
echo
。我只是想知道这样做的好处是什么,而不是仅仅将函数的返回连接到一个已经被回显的字符串中。@davidb:函数中的return语句中缺少分号(我的错)。我建议您启用PHP错误报告<代码>错误报告(E_全部)还考虑将类名设置为零长度字符串以外的其他内容。在一个
else
中,当没有一个if测试的计算结果为TRUE时,或者将$classname初始化为函数顶部的默认值。(例如,当所有条件都不成立时,
class=
的值是多少?@spencer7593,谢谢,我现在尝试使用分号,我得到的是准确的颜色,但数字在单元格中不可见,单元格颜色正确。