为什么我的下拉列表会在表格中单独分隔列。示例我希望在下拉列表中显示3个选项。这些是我数据库中的数据。
例如: company a, company b
将显示在下拉列表中。
现在,下拉列表中只有一个选项(company a)
可用,(company b)
显示在表格的下一行而不是一起显示在单个下拉列表中。
<?
$result = mysqli_query($con,"SELECT admin_no, name, GPA, gender, job_details.job_title, job_details.no_of_vacancy FROM student_details, job_details ORDER BY `GPA` DESC ");
$result2 = mysqli_query($con, "SELECT job_title FROM job_details;");
$row2 = mysqli_fetch_assoc($result2);
while($row = mysqli_fetch_assoc($result))
{
while ($row2 = mysqli_fetch_array($result2))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'><option value='". $row2['job_title'] ."'> ". $row2['job_title'] ."</option></td>";
echo "</tr>";
}
}
echo "</table>";
?>
</form>
答案 0 :(得分:1)
试试这个:
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>";
while ($row2 = mysqli_fetch_array($result2))
{
echo "<option value='". $row2['job_title'] ."'> ". $row2['job_title'] ."</option>";
}
echo "</select></td>";
echo "</tr>";
}
答案 1 :(得分:1)
试试这个,你错过了关闭</select>
标签,我重写了代码,即在表格生成部分上方移动<option>
标签生成。这样可以避免不必要的循环。
<?php
$result = mysqli_query($con,"SELECT admin_no, name, GPA, gender, job_details.job_title, job_details.no_of_vacancy FROM student_details, job_details ORDER BY `GPA` DESC ");
$result2 = mysqli_query($con, "SELECT job_title FROM job_details");
$row2 = mysqli_fetch_assoc($result2);
/*options sections start*/
$options= '';
while ($row2 = mysqli_fetch_array($result2))
{
$options .='<option value="'. $row2['job_title'] .'"> '. $row2['job_title'] .'</option>';
}
/*options sections end*/
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td bgColor=white>" . $row['admin_no'] . "</td>";
echo "<td bgColor=white>" . $row['name'] . "</td>";
echo "<td bgColor=white>" . $row['GPA'] . "</td>";
echo "<td bgColor=white>" . $row['gender'] . "</td>";
echo "<td><select name='ddl' onclick='if(this.value != '') { myform.submit(); }'>".$options."</select></td>";
echo "</tr>";
}
echo "</table>";
?>