选择仅填充第一个表行的选项

时间:2017-05-13 20:28:15

标签: php

在我的程序中,我根据MySql数据库中的数据构建了一个表。

        <?php  while ($row = mysqli_fetch_array($result)) {
    extract($row);?>
    <td width="5">   <input name="record_id" hidden value="<?php echo $row['id'] ?>"></td>
    <td width="75"> <input name="timeneeded" readonly value="<?php echo $row['time_needed']?>"></td>
    <td width="75"> <input name="dateneeded" readonly value="<?php echo $row['date_needed']?>"></td>
    <td width="100"> <input name="firstname" readonly value="<?php echo $row['first_name']?>"></td>
    <td width="100"> <input name="lastname" readonly value="<?php echo $row['last_name']?>"></td>
    <td width="120"> <input name="pickup" readonly value="<?php echo $row['pickup_location']?>"></td>"
    <td width="100"> <input name="dropoff" readonly value="<?php echo $row['destination']?>"></td>"
    <td width='125' align='left'>
    <select size='1' name='assignDrivers' onChange=showDrivers(this.value)>
    <option>Select Driver</option>
    <?php while(list($username)=mysqli_fetch_row($result1)) {
        echo "<option value=\"".$username."\">".$username."</option>";
     }
     ?>
    </select> <input id="drivertouse" name='driver_name' size='15' style='font-weight: 700' value=<?php echo "\"$username\""; ?>> </td>

一切都很好,在第一行,它会提取驱动程序列表,但是它不会为后续行提取它们。它仅显示“选择驱动程序”

任何帮助将不胜感激, TIA

3 个答案:

答案 0 :(得分:0)

我认为您的问题出在list($username),您应该能够做到这一点:

while($username = mysqli_fetch_row($result1)) {
   echo "<option value=\"".$username."\">".$username."</option>";
}

$username变量将作为循环的一部分重置,因此您不需要使用list()

答案 1 :(得分:0)

<?php
    $the_drivers = ""; // Initialize the drivers variable

    while(list($username)=mysqli_fetch_row($result1)) {

        $the_drivers .= "<option value='" . $username . "'>" . $username . "</option>";
    }

    if(isset($the_drivers)){

        echo $the_drivers;
    }
?>

版本2 正如Spholt先前提到的那样,我强烈反对,其中的list()函数也存在问题。此版本2删除该列表()。我不知道$ result1是什么(我的假设是它是一个数据库查询)。也就是说,我不知道$ username的列名(我只是想象它是用户名。如果它没有替换$ username_row中的正确名称[&#39;用户名&#39;]。

<?php
    $the_drivers = ""; // Initialize the drivers variable

    while($username_row=mysqli_fetch_row($result1)) {

    $username = $username_row['username'];

        $the_drivers .= "<option value='" . $username . "'>" . $username . "</option>";
    }

    if(isset($the_drivers)){

        echo $the_drivers;
    }
?>

答案 2 :(得分:0)

试试这段代码,我认为result1查询本身存在问题

 <?php  while ($row = mysqli_fetch_array($result)) {
    extract($row);?>
    <td width="5">   <input name="record_id" hidden value="<?php echo $row['id'] ?>"></td>
    <td width="75"> <input name="timeneeded" readonly value="<?php echo $row['time_needed']?>"></td>
    <td width="75"> <input name="dateneeded" readonly value="<?php echo $row['date_needed']?>"></td>
    <td width="100"> <input name="firstname" readonly value="<?php echo $row['first_name']?>"></td>
    <td width="100"> <input name="lastname" readonly value="<?php echo $row['last_name']?>"></td>
    <td width="120"> <input name="pickup" readonly value="<?php echo $row['pickup_location']?>"></td>"
    <td width="100"> <input name="dropoff" readonly value="<?php echo $row['destination']?>"></td>"
    <td width='125' align='left'>
    <select size='1' name='assignDrivers' onChange=showDrivers(this.value)>
    <option>Select Driver</option>
while($username = mysqli_fetch_row($result1)) {
foreach($username as $user){
   echo "<option value=\"".$user."\">".$user."</option>";
}
}
?>
    </select> <input id="drivertouse" name='driver_name' size='15' style='font-weight: 700' value=<?php echo "\"$username\""; ?>> </td>
相关问题