foreach循环突出显示数据库中的单元格

时间:2017-05-01 12:40:36

标签: php html css mysql forms

我想知道使用foreach循环我的代码是否更容易。我的代码如此: 目的是读取MYSQL表中的值,如果anwser是" NEE"以红色显示背景颜色。我的代码有效,但很长..

$connection = mysql_connect('localhost', 'root', ''); //The Blank string is 
the password
mysql_select_db('heijsDB');

$query = "SELECT * FROM hygieneaanvoer"; //You don't need a ; like you do in 
SQL
$result = mysql_query($query);

//List the Columns for the Report 
if(! $result ) {
           die('Could display data: ' . mysql_error());
        }

echo "<table border='1' class='w3-panel'> 
<fieldset>hygiëne/GMP aduit Aanvoer</fieldset>
<tr> 
<th>Datum</th> 
<th>Controleur</th> 
<th>controleur</th>
<th>Revisie</th> 
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
</tr>"; 
while($row = mysql_fetch_array($result)) 
{ 
 echo "<tr>"; 
echo "<td>" . $row['datum'] . "</td>"; 
 echo "<td>" . $row['controleur'] . "</td>";
  echo "<td>" . $row['codering'] . "</td>"; 
 echo "<td>" . $row['revisie'] . "</td>";
 if($row['q1']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q1']."</td>";
else echo "<td>".$row['q1']."</td>";
    if($row['q2']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q2']."</td>";
else echo "<td>".$row['q2']."</td>";
if($row['q3']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q3']."</td>";
else echo "<td>".$row['q3']."</td>";
if($row['q4']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q4']."</td>";
else echo "<td>".$row['q4']."</td>";
if($row['q5']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q5']."</td>";
else echo "<td>".$row['q5']."</td>";
if($row['q6']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q6']."</td>";
else echo "<td>".$row['q6']."</td>";
if($row['q7']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q7']."</td>";
else echo "<td>".$row['q7']."</td>";
if($row['q8']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q8']."</td>";
else echo "<td>".$row['q8']."</td>";
if($row['q9']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q9']."</td>";
else echo "<td>".$row['q9']."</td>";
if($row['q10']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q10']."</td>";
else echo "<td>".$row['q10']."</td>";
if($row['q11']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q11']."</td>";
else echo "<td>".$row['q11']."</td>";
if($row['q12']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q12']."</td>";
else echo "<td>".$row['q12']."</td>";
if($row['q13']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q13']."</td>";
else echo "<td>".$row['q13']."</td>";
if($row['q14']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q14']."</td>";
else echo "<td>".$row['q14']."</td>";
if($row['q15']=='NEE') // [val1] can be 'approved'
     echo "<td style='background-color: #e21010;'>".$row['q15']."</td>";
else echo "<td>".$row['q15']."</td>";
 echo "</tr>"; 
 } 
echo "</table>";

   ?>

3 个答案:

答案 0 :(得分:0)

您可以做的只是创建一个用于设置所有行样式的变量:

while($row = mysql_fetch_array($result)) 
{ 
    $style_string = "";
    if($row['q1']=='NEE') {
        $style_string = "style='background-color: #e21010;'";
    }

    echo "<tr>"; 
    echo "<td>" . $row['datum'] . "</td>"; 
    echo "<td>" . $row['controleur'] . "</td>";
    echo "<td>" . $row['codering'] . "</td>"; 
    echo "<td>" . $row['revisie'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q1'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q2'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q3'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q4'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q5'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q6'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q7'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q8'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q9'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q10'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q11'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q12'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q13'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q14'] . "</td>";
    echo "<td " . $style_string . ">" . $row['q15']. "</td>";
    echo "</tr>"; 
 } 

(或者只设置<tr>

的背景颜色

祝你好运!

答案 1 :(得分:0)

您只需将该类添加到td,就像这样

echo "<td class='". $row['q15'] . "'>".$row['q15']."</td>";

像这样添加css

.NEE {background-color: #e21010;}

答案 2 :(得分:0)

这是解决方案     

$result = mysql_query("SELECT * FROM hygieneaanvoer")or die('Could display data: ' . mysql_error());;

echo "<table border='1' class='w3-panel'> 
<fieldset>hygiëne/GMP aduit Aanvoer</fieldset>
<tr> 
<th>Datum</th> 
<th>Controleur</th> 
<th>controleur</th>
<th>Revisie</th>";

for ($i=1;$i<=15;$i++){
    echo '<th>'.$i.'</th>';
} 
echo "</tr>"; 
while($row = mysql_fetch_array($result))  { 
    echo "<tr>"; 
    echo "<td>" . $row['datum'] . "</td>"; 
    echo "<td>" . $row['controleur'] . "</td>";
    echo "<td>" . $row['codering'] . "</td>"; 
    echo "<td>" . $row['revisie'] . "</td>";
    for ($j=1;$j<=15;$j++){
        echo "<td ".(($row['q'.$j] == 'NEE') ? "style='background-color: #e21010;'" : "" ).">".$row['q'.$j]."</td>";
    }
    echo "</tr>"; 
} 
echo "</table>";

?>