答案 0 :(得分:1)
无需重新获取它。你已经在这里做过了:
$uname = array();
while ($rows = mysqli_fetch_array($uresult)) {
$uname[] = $rows['name'];
}
所以在另一个while块中,只需使用已经收集的名称:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'>" . $row['emp_no']. "</td>";
echo "<td align='center'>" . $row['emp_name']. "</td>";
echo "<td align='center'>" . $row['task'] . "</td>";
echo "
<td align='center'>
<select name='select1'>
";
foreach($uname as $names) {
echo '<option>' . $names . '</option>';
}
echo "
</select>
</td>";
echo "</tr>";
}
或者只是使用<options>
创建一个$uname
的HTML字符串,然后只在echo
内创建while
:
$uname = '';
while ($rows = mysqli_fetch_array($uresult)) {
$uname .= '<option>' . $rows['name'] . '</option>';
}
然后在while
区块:
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td align='center'>" . $row['emp_no']. "</td>";
echo "<td align='center'>" . $row['emp_name']. "</td>";
echo "<td align='center'>" . $row['task'] . "</td>";
echo "
<td align='center'>
<select name='select1'>
";
echo $uname; // collection of HTML string options
echo "
</select>
</td>";
echo "</tr>";
}
答案 1 :(得分:1)
错误在于以下行 你有字符串里面的PHP代码
echo "<td align='center'> <select name='select1'> <?php while ($rows=mysqli_fetch_array($uresult)) { ?><option><?php echo $rows['name'];?> </option><?php } ?> </select> </td>";
答案 2 :(得分:0)
试试这个,html中的embedding
php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td align='center'><?php echo $row['emp_no'] ?></td>
<td align='center'><?php echo $row['emp_name'] ?></td>
<td align='center'><?php echo $row['task'] ?></td>
<td align='center'> <select name='select1'>
<?php while ($rows=mysqli_fetch_array($uresult)) { ?>
<option><?php echo $rows['name'];?> </option><?php } ?>
</select> </td>
</tr>";
<?php
}