元のデータは前と同じ 2to3nen.txt です。3年の名簿をソートして出るようにします。
3年の選択ボタンが必要です。actionの先は自分自身で今回はselect3.phpです。その他は同じ。
<form action="select3.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>
PHPプログラムはこれ。nhr,hrに同時に値が入ることはありません。
<table> <?php $fp = fopen("2to3nen.txt","r"); $shr="2Z"; if( isset($_POST["hr"]) ){ $shr =$_POST["hr"]; 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"; } } } } $nhr="3Z"; 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 ($newhr === $nhr){ $key = sprintf("%2s%02d",$newhr,$newno); $val = $line; $gotoarray[$key]=$val; } } } ksort($gotoarray); foreach ($gotoarray as $key => $val) { list($hr,$no,$sei,$mei,$seib,$ysei,$ymei,$newhr,$newno) = explode("\t",$val); 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 select3.php を完成させなさい。index.htmlに「phpで選択3」という名前のリンクを作りましたか。