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

标签 php html css mysql forms

我想知道使用 foreach 循环是否可以使我的代码更简单。到目前为止我的代码: 目的是读取 MYSQL 表中的值,如果答案是“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>";

   ?>

最佳答案

这里是解决方案

$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>";

?>

关于php - foreach 循环突出显示数据库中的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43719245/

相关文章:

html - 移动设备上的背景图像与 PC 浏览器中的移动预览不同

html - 防止 float 元素在窗口缩放时掉落

javascript - 使用 Rollup 解决 woff 导入问题

html - 标题中图像右侧的居中文本

php - Cakephp 显示其中 1=1

php - 在 Linux 上使用 CURL (PHP) 的 SSL 连接,相同的代码在 Windows 上失败

php - 如果列不匹配,则从多个表中选择连接返回 null

javascript - 通过条形码扫描器扫描后避免自动提交表单

javascript - 焦点元素隐藏在固定菜单后面

php - 计算网站的超链接