2012-04-10 8 views
0

私はPHPを初めて使い、基本的な機能をいくつか習得しています。私は自分のユーザーが自分自身を編集して再表示するためのテーブルを作成することができましたが、私は質問に出くわしました。 以下のスクリプトを使用して、さまざまな製品のスキルレベルを入力できます。私は彼らが "0"または空白を入力する各セルを強調表示できるようにしたいと思いました。ユーザーの入力は0〜5の間です(まだ入力していない場合は空白です)。MySQLクエリ出力に基づいてセルの背景色を設定する

これはすべて私のlocalhost上で行われているので、私はすべてのセキュリティ対策があまりないことを認めます。

私は多くの投稿を読んで自分自身でそれを理解しようとしましたが、根本的に間違ったことをしています。

これについてのご支援をいただければ幸いです。私はコーディング:)

をここで私を助けて人のために(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

この1つはどちらか動作しませんでした。あなたがここに示した通りにペーストしました。保存後にページが読み込まれません。 (私はまた "灰色"を "赤"に変更しました) – user1267673

+0

whats the error? – Dion

+0

は 'background-color'ですか? 'color'は、セルではなくテキストの色を制御します。 – octern

0

このように、あなたの作成した文書

のにこれを追加し、何かを試してみてください

は、これはあなたのPHPです:

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

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

残念ながらそれはうまくいきませんでした。私は$ row [hamke]行にそれを適用しましたが、その行には "0"がありますが、色は変わりませんでした。しかし、試してくれてありがとう! – user1267673

+0

あなたは何か間違ったことやあなたのデータを誤解しています。他の空白や特殊文字がない限り、 'empty(0)'と 'empty( '0')'はfalseと評価されます。上の更新されたポストを参照して、私は物事を単純化し、データにトリムを追加しました。 – Alex

0

[OK]を、ので、私はそれが最終的に働いて得ることができました。上記の2つの回答は、これを行うための正しいアプローチを理解するのに役立ちました。

もちろん、私のアプローチは最良の方法ではないかもしれませんが、私はそれをテストして、自分のニーズに合っています。将来サーチャーのために、ここで私がやったことだ:

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

} エコー "";

?>

関連する問題