根据MySQL查询输出设置单元格的背景颜色

时间:2012-04-10 19:52:16

标签: php mysql

我是PHP的新手,并试图学习足够的功能来完成一些基本功能。我已经能够为我的用户创建一个表来编辑自己,并重新显示但我遇到了一个问题。 使用下面的脚本,用户可以输入各种产品的技能水平。我希望能够突出显示输入“0”或空白的每个单元格。用户的输入将介于0-5之间(如果尚未填写,则为空白。)

这一切都是在我的本地主机上完成的,所以我承认所有的安全措施都不存在。

我已经阅读了很多帖子,并试图自己解决这个问题,但我相信我做的事情根本就是错误的。

对此的任何帮助将不胜感激。我知道为那些帮我编码的人买啤酒(通过paypal):)

以下是我现有的打印出数据库结果的代码:

<?php
//This will connect to the database in order to begin this page
mysql_connect("localhost", "root", "time2start") or die (mysql_error());
//Now we will select the database we need to talk to
mysql_select_db("joomla_dev_15") or die (mysql_error());
$query = "SELECT * FROM enterprise_storage WHERE id=1";
$result = mysql_query($query) or die (mysql_error());
echo "<table border='1'>";
echo "$row";
echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th>       <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th>  <th>Luke Soares</th>  <th>Josh Wenger</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['model'];
echo "</td><td>"; 
echo $row['beeg'];
echo "</td><td>"; 
echo $row['hamke'];
echo "</td><td>"; 
echo $row['jaczyk'];
echo "</td><td>"; 
echo $row['jontow'];
echo "</td><td>"; 
echo $row['macdonald'];
echo "</td><td>"; 
echo $row['munozcano'];
echo "</td><td>"; 
echo $row['shaffer'];
echo "</td><td>"; 
echo $row['soares'];
echo "</td><td>"; 
echo $row['wenger'];
echo "</td></tr>"; 
} 
echo "</table>";
?>
<FORM>
<INPUT TYPE="BUTTON" VALUE="Return to the Home Page"       ONCLICK="window.location.href='http://localhost/~user/joomla15/custom/skilldisplay.php'"> 
</FORM>

3 个答案:

答案 0 :(得分:1)

也许

while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr>";
foreach($row as $content) {
    if($content == 0) {
        echo "<td style='background-color:gray;'>";
    }
    else {
        echo "<td style='background-color:green;'>";
    }
    echo $content . "</td>";
}
echo $row['wenger'];
echo "</td>"; 
}
echo "</tr></table>";

答案 1 :(得分:0)

尝试这样的事情

将此内容添加到生成的文档中

<style type="text/css">
    .red{ background-color: red; }
</style>

这是你的PHP:

<?php
// sanitize value
$value = trim($row['model']);
$class = (empty($value)) ? 'red' : '';

// display
echo "<td class=\"$class\">$value</td>"; 
...
?>

答案 2 :(得分:0)

好的,所以我设法让它最终运转起来。上面的两个回复帮助我找到了做到这一点的正确方法。

当然,我的方法可能不是最好的方法,但我已经测试过,它可以满足我的需求。对于任何未来的搜索者,这就是我所做的:

    <?php
    //This will connect to the database in order to begin this page
    mysql_connect("localhost", "root", "time2start") or die (mysql_error());
    //Now we will select the database we need to talk to
    mysql_select_db("joomla_dev_15") or die (mysql_error());
    $query = "SELECT * FROM enterprise_storage";
    $result = mysql_query($query) or die (mysql_error());
    echo "<table border='1'>";
    echo "$row";
    echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th> <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th><a href='http://localhost/~user/joomla15/custom/updateform.php'>Luke Soares</a></th> <th>Josh Wenger</th> </tr>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>"; 
echo $row['model'];
echo "</td>";
if ($row['beeg'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['beeg'] ;
}else{
    echo '<td>' .$row['beeg'];
}
echo "</td>"; 
if ($row['hamke'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['hamke'] ;
}else{
    echo '<td>' .$row['hamke'];
}
echo "</td>"; 
if ($row['jaczyk'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['jaczyk'] ;
}else{
    echo '<td>' .$row['jaczyk'];
}
echo "</td>"; 
if ($row['jontow'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['jontow'] ;
}else{
    echo '<td>' .$row['jontow'];
}
echo "</td>"; 
if ($row['macdonald'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['macdonald'] ;
}else{
    echo '<td>' .$row['macdonald'];
}
echo "</td>"; 
if ($row['munozcano'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['munozcano'] ;
}else{
    echo '<td>' .$row['munozcano'];
}
echo "</td>"; 
if ($row['shaffer'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['shaffer'] ;
}else{
    echo '<td>' .$row['shaffer'];
}
echo "</td>"; 
if ($row['soares'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['soares'] ;
}else{
    echo '<td>' .$row['soares'];
}
echo "</td>"; 
if ($row['wenger'] == '0'){
    echo '<td bgcolor="#FF0000">' . $row['wenger'] ;
}else{
    echo '<td>' .$row['wenger'];
}
echo "</td></tr>"; 

} echo“”;

&GT?;