1

動的選択リストを作成したい。例: データベースに 5 人の学生がいます。私の目標は、5つの選択を作成し、すべての選択でデータベースにあるすべての学生を選択することです。したがって、ユーザーがデータベースに 6 番目の学生を挿入すると、ページには 6 つの選択が表示され、すべての選択で 6 人の学生の名前が表示されます。

このコードを試してみましたが、4 人の生徒を含む 1 つの選択しか作成されません (データベースの最初の 1 つが欠落しています)。

コード:

$con=mysqli_connect("localhost","root","","novi-studomat");
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$i=1;
$execute=mysqli_query($con,$exe);
while($result=mysqli_fetch_array($execute))
{
echo '<select name=student'.$i.'>';
echo '<option value="-1" >Choose student</option> <br/>';
while($res=mysqli_fetch_array($execute))
{
    echo '<option value='.$res["id_student"].'>'.$res["name"].'</option> <br/>';
}
echo '</select>';
$i++;
}
4

2 に答える 2

2
$con=mysqli_connect("localhost","root","","novi-studomat");
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";

$i=1;
$execute=mysqli_query($con,$exe);
$select = '';
for($j = 0; $j < mysqli_num_rows($execute); $j++) {
    $select .= '<select name=student' . $j . '>';
    $select .= '<option value="-1" >Choose student</option> <br/>';
    while($result=mysqli_fetch_array($execute)) {
        $select .= '<option value=' . $res["id_student"].'>' .   $res["name"] . '</option>';
        $i++;
    }
    $select .= '</select>';
}
echo $select;

アップデート

その間

$select = '<select name=student' . $j . '>';
$select .= '<option value=' . $res["id_student"].'>' .   $res["name"] . 

この行をこの他の行に変更します

$select = '<select name="student' . $j . '">';
$select .= '<option value="' . $res["id_student"]."'>' .   $res["name"] . 

値と選択名の二重引用符がありませんでした

于 2013-04-30T19:05:34.110 に答える