###前提・実現したいこと```ここに言語を入力
コード
PHPにてテーブル一覧を作成しています。 ですが、PHPでループさせて一覧表示をする時に、表示後に特定の文字だった場合(未確定)のデータを抽出した時に、テーブル内のそのセルだけ背景色を変えたいと思うのですが、上手くいきません。 ###発生している問題・エラーメッセージ
特にエラーは出ておりませんが、色が変わりません。
自分なりに調べては見たのですが、Javascriptを利用しないと上手く行かないのでしょうか?
test.html <!DOCTYPE html> <html> <head> 省略 </head> <body> <table> <tr> <th>名前</th><th>誕生日</th><th>年齢</th> </tr> <tr> <#LIST#>//PHPにて置き換えしています </tr> </body> </html>
<?php $query='select * from check_info';//ここの名前カラムに、未確定というデータが入っています。 $line=""; $line_not_decide=""; if ($result = mysqli_query($link, $query)) { $i=0; while ($row = mysqli_fetch_row($result)) { $name = $row[0]; $birthday = $row[1]; $age = $row[2]; $line .="<tr><td>".$name[$i]."</td><td>".$birthday[$i]."</td><td>".$age[$i]."</td></tr>\n"; $i++; $line_not_decide .="<tr><td style='background-color:red;'>".$name[$i]."</td><td>".$birthday[$i]."</td><td>".$age[$i]."</td></tr>\n"; $i++; } mysqli_free_result($result); } mysqli_close($link); $fp=fopen('./test.html','r'); while(!feof($fp)) { $test_line=fgets($fp); if($name[$i] == "未確定"){ $lines=str_replace("<#LIST#>",$line_not_decide,$test_line); }else{ $lines=str_replace("<#LIST#>",$line,$test_line); } echo $lines; } fclose($fp); exit(); ?>
該当箇所修正コード <?php $i = $lines = ''; while ($row = mysqli_fetch_row($result)) { $name = $row[0]; $birthday = $row[1]; $age = $row[2]; if($name[$i] == "未確定"){ $lines .= "<tr><td style='background-color:red;'>".$name[$i]."</td><td>".$birthday[$i]."</td><td>".$age[$i]."</td></tr>\n"; } elseif ($name[$i] == "未確定" && $age[$i] == "30"){ $lines .= "<tr><td style='background-color:red;'>".$name[$i]."</td><td>".$birthday[$i]."</td><td style='background-color:red;'>".$age[$i]."</td></tr>\n"; } else { $lines .= "<tr><td>".$name[$i]."</td><td>".$birthday[$i]."</td><td>".$age[$i]."</td></tr>\n"; } $i++; } ?>
###試したこと
追記 :年齢もelseifで指定して、複数のカラムを選択して抽出しようとしたら、最初の名前のみ背景色が赤くなりますが、例えば年齢カラムの背景色が赤くならない。
PHPのif文を利用して、もし引っ張ってきたデータが「未確定」という文字だった場合に、セルの背景色を赤にしたいので、文を二つに分けて、表示させようとしました。
ですが、PHPでループした時に、未確定があったら$line_not_decide、なければ$lineで表示を変えていこうとしましたが、上手く反映されません。
分けてPHPで表示しようとするからいけないのかと思ったりもしていますが、どなたかご教授いただけないでしょうか?
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
MAC OS、
回答1件
あなたの回答
tips
プレビュー