2017-03-01 63 views
-1

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

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

和你面对什么问题? –

+0

您的代码很奇怪,'$ colorfields'是一个数组,您不能将它与标量值(如字符串或数字)进行比较。这些神奇的价值是什么:195,199.99?我认为这个问题只能用css3选择器来解决。 –

+0

@Anant,我没有得到IF条件为真的单元格的预期颜色,而是在所有单元格上获得了绿色。 – davidb

回答

2

我会写一个返回的“红色”的功能,“黄色”或“绿色”,根据传递作为参数的值。

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

谢谢你,我正在尝试你的解决方案,即时获取空白页。 – davidb

+0

使用echo输出函数返回值。 echo class_from_val($ row ['Column4']) –

+0

@RavinderReddy:是的,可以使用'echo'。我只是想知道它的优点是什么,而不是将函数的返回连接到已经被回显的字符串中。 – spencer7593

2

遵循以下基本原则的功能:

  • 缩进代码和空间它
  • 小心你的选择变量名称
  • 划分和规则(使用函数)

例如:

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

@ Casimir et Hippolyte,谢谢。 – davidb