简体   繁体   中英

Select Option only filling in first table row

In my program I build a table from data from a MySql Database.

        <?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>

Everything pulls fine, and on the first row, it pulls the list of drivers, however it doesn't pull them for subsequent rows. It only shows the 'Select Drivers'

Any help would be appreciated, TIA

I think your problem is at list($username) , you should just be able to do this:

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

The $username variable will be reset as part of the loop so you shouldn't need to use list() .

<?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;
    }
?>

As Spholt suggested earlier, which I believe strongly, the list() function in there is also a problem. 正如Spholt先前建议的那样(我坚信这一点),其中的list()函数也存在问题。 This version 2 removes that list(). I don't know what is $result1(my assumption is that it is a database query). That said, I don't know the column name of $username(I have just imagined it to be username. If it is not replace the correct name in $username_row['username'].

<?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;
    }
?>

try this code , I think there is a problem with result1 query itself

 <?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM