2012-04-10 30 views
0

我是PHP新手,嘗試學習足夠的基本功能。我已經能夠爲我的用戶創建一個表格來編輯自己,並重新顯示,但我遇到了一個問題。 使用下面的腳本,用戶可以輸入他們的各種產品的技能水平。我希望能夠突出顯示它們輸入「0」或空白的每個單元格。用戶的輸入將在0-5之間(如果還沒有填入,則輸入空白)。根據MySQL查詢輸出設置單元格的背景顏色

這一切都在我的本地主機上完成,所以我承認所有的安全措施都不在那裏。

我已經閱讀了很多文章,並試圖找出自己,但我正在做一些根本性錯誤,我相信。

任何這方面的援助將不勝感激。我已經知道買啤酒(通過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> 

回答

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

這一個也不能工作。我粘貼就像你在這裏指出的那樣。保存後頁面不會加載。 (我也改變了「灰色」到「紅色」) – user1267673 2012-04-10 22:17:32

+0

最新錯誤? – Dion 2012-04-10 22:31:21

+0

應該是「背景色」嗎? 'color'控制文本的顏色,而不是單元格。 – octern 2012-04-11 02:02:47

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

不幸的是,沒有奏效。我將它調整到了肯定有「0」的$ row [hamke]行,但它並沒有改變顏色。謝謝你嘗試! – user1267673 2012-04-10 22:15:05

+0

你正在做錯或誤解你的數據。除非你有其他的空格或特殊字符,否則'empty(0)'和'empty('0')'將評估爲false。看到上面更新的帖子,我簡化了一些事情,並在數據中添加了修剪。 – Alex 2012-04-11 01:02:45

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

} 回聲 「」;

?>

相關問題