Php 创建动态下拉列表
我想创建动态选择列表。例如:我的db中有5名学生。我的目标是创建5个选择,并在每个选择中选择数据库中的所有学生。因此,如果用户在数据库中插入第6个学生,页面应显示6个选择,并且在每个选择中显示6个学生的姓名 我试过使用这段代码,但它只创建了1个select,其中包含4个学生(数据库中的第一个学生丢失了) 守则:Php 创建动态下拉列表,php,mysql,Php,Mysql,我想创建动态选择列表。例如:我的db中有5名学生。我的目标是创建5个选择,并在每个选择中选择数据库中的所有学生。因此,如果用户在数据库中插入第6个学生,页面应显示6个选择,并且在每个选择中显示6个学生的姓名 我试过使用这段代码,但它只创建了1个select,其中包含4个学生(数据库中的第一个学生丢失了) 守则: $con=mysqli_connect("localhost","root","","novi-studomat"); $exe="SELECT * FROM kolegij WHERE
$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++;
}
$con=mysqli_connect(“localhost”、“root”、“novi studomat”);
$exe=“从kolegij中选择*,其中id_student='$id_student';
$i=1;
$execute=mysqli\u查询($con,$exe);
而($result=mysqli\u fetch\u数组($execute))
{
回声';
回声“选择学生
”;
而($res=mysqli\u fetch\u数组($execute))
{
回显'.$res[“name”].
;
}
回声';
$i++;
}
$con=mysqli_connect(“localhost”、“root”、“novi studomat”);
$exe=“从kolegij中选择*,其中id_student='$id_student';
$i=1;
$execute=mysqli\u查询($con,$exe);
$select='';
对于($j=0;$j ”;
而($result=mysqli\u fetch\u数组($execute)){
$select.=''.$res[“name”].';
$i++;
}
$select.='';
}
echo$select;
更新
一会儿之内
$select = '<select name=student' . $j . '>';
$select .= '<option value=' . $res["id_student"].'>' . $res["name"] .
$select='';
$select.=''$res[“name”]。
把这行改成另一行
$select = '<select name="student' . $j . '">';
$select .= '<option value="' . $res["id_student"]."'>' . $res["name"] .
$select='';
$select.='问题解决了,我只需要在for循环中插入vars$exe和$execute,所以每次迭代后$result都会刷新,否则它只会在第一次选择中打印选项
代码:
$con=mysqli_connect(“localhost”、“root”、“novi studomat”);
$exe=“从kolegij中选择*,其中id_student='$id_student';
$execute=mysqli\u查询($con,$exe);
$select='';
对于($j=0;$j ”;
而($result=mysqli\u fetch\u数组($execute))
{
$select.=''.$res[“name”].';
}
$select.='';
}
echo$select;
正如andrewsi所说,您有两个while,但您需要删除内部while而不是外部while。您最好运行一个while循环来遍历数据库中的结果,并使用它为
生成SQL。然后运行第二个循环来生成
s,它将创建正确数量的选择,但每个选择中只包含一个学生。确实会。有一个+1。它创建了适当数量的选择,但只有第一个选择包含所有5名学生,其余选择只包含值-1???有什么想法吗?试试看,但留下引号
$select = '<select name="student' . $j . '">';
$select .= '<option value="' . $res["id_student"]."'>' . $res["name"] .
$con=mysqli_connect("localhost","root","","novi-studomat");
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$execute=mysqli_query($con,$exe);
$select = '';
for($j = 0; $j < mysqli_num_rows($execute); $j++)
{
$exe="SELECT * FROM kolegij WHERE id_student='$id_student'";
$execute=mysqli_query($con,$exe);
$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>';
}
$select .= '</select>';
}
echo $select;