元のデータは前と同じ 2to3nen.txt です。新HRのデータでも選択可能にします。。
ボタンを追加してあります。
3年の選択ボタンが必要です。actionの先は自分自身。今回はselect2.phpという名前にしました。
<form action="select2.php#koko" method="post" id="koko" <p> <input type="submit" name="hr" value="2A"> <input type="submit" name="hr" value="2B"> <input type="submit" name="hr" value="2C"> <input type="submit" name="hr" value="2D"> <input type="submit" name="hr" value="2E"> <input type="submit" name="hr" value="2F"> <input type="submit" name="hr" value="2G"> <input type="submit" name="hr" value="2H"> </p> <p> <input type="submit" name="nhr" value="3A"> <input type="submit" name="nhr" value="3B"> <input type="submit" name="nhr" value="3C"> <input type="submit" name="nhr" value="3D"> <input type="submit" name="nhr" value="3E"> <input type="submit" name="nhr" value="3F"> <input type="submit" name="nhr" value="3G"> <input type="submit" name="nhr" value="3H"> </p> </form>
formは一つでボタンのnameをnhrにしました。(今回はhrのままでもプログラムの仕方によっては可能ですが、全学年のデータが1ファイルになっている場合を考えてこうしています)
PHPプログラムはこれ。nhr,hrに同時に値が入ることはありません。
<table> <?php $fp = fopen("2to3nen.txt","r"); $shr="2Z"; $nhr="3Z"; if( isset($_POST["hr"]) ){ $shr =$_POST["hr"];} if( isset($_POST["nhr"]) ){ $nhr =$_POST["nhr"];} while(!feof($fp)){ $line = fgets($fp); if (trim($line) != null){ list($hr,$no,$sei,$mei,$seib,$ysei,$ymei,$newhr,$newno) = explode("\t",$line); if ($hr === $shr){ print "<tr>\n"; print "<td>$hr</td><td>$no</td><td>$sei</td><td>$mei</td>"; print "<td>$seib</td><td>$ysei</td><td>$ymei</td>\n"; print "<td>$newhr</td><td>$newno</td>\n"; print "</tr>\n"; } if ($newhr === $nhr){ print "<tr>\n"; print "<td>$newhr</td><td>$newno</td><td>$sei</td><td>$mei</td>"; print "<td>$seib</td><td>$ysei</td><td>$ymei</td>\n"; print "<td>$hr</td><td>$no</td>\n"; print "</tr>\n"; } } } fclose($fp); ?> </table>
上の説明は前回の課題2の解答になっています。
前回の課題2 select2.php を完成させなさい。index.htmlに「phpで選択2」という名前の リンクを作りましたか。
課題としては項目名は曖昧な書き方のまま共通でも構いません。しかしHRか新HRかで変えたくなった場合は if( isset($_POST["hr"]) ){ $shr =$_POST["hr"];} の辺りに書くのがいいでしょう
if( isset($_POST["hr"]) ){ $shr =$_POST["hr"]; print "<tr>\n"; print "<td>旧HR</td><td>旧No</td>... } if( isset($_POST["nhr"]) ){ $nhr =$_POST["nhr"]; print "<tr>\n"; print "<td>新HR</td><td>新No</td>... }
3年の名簿をソートして出るようにしなさい。
phpのページファイルは select3.php という名前で保存します。index.htmlに「phpで選択3」という名前の リンクを作ります。