在循环中显示来自php中其他表的数据

时间:2017-07-07 20:24:27

标签: php mysql while-loop

我正在尝试在另一个表格中显示select标签中的选项  首先,while循环继续,这是我到目前为止所尝试的!!

<?php
     $query="SELECT * FROM `customerdata` WHERE takenby='$_SESSION[username]'";
     $query_two="SELECT * FROM `vendordriver` WHERE vendoremail='$_SESSION[username]'";
     $run_two=$db->query($query_two);
     $run=$db->query($query);
     while ($row=$run->fetch_assoc()) {
       echo " <tr><td>$row[bookingid]</td>
       <td>$row[drivername]</td>
       <td>$row[cabtype]</td>
       <td>$row[carnumber]</td><td><select>";
  while ($row_two=$run_two->fetch_assoc()) {
    echo "<option>$row_two[drivername]</option>";
  }
echo" </select></td>
       <td><input type='submit' class='btn btn-success' value='SEND '>
       </td>";
     }
    ?>

任何建议请!!!

2 个答案:

答案 0 :(得分:0)

您可以尝试此代码

<?php
$table_1 = null;
$username = $_SESSION['username'];
$query="SELECT * FROM customerdata WHERE takenby='$username'";
$run=$db->query($query);
if ($result->num_rows > 0) {
    while ($row=$run->fetch_assoc()) {
    $table_1 = "<tr><td>$row[bookingid]</td>";
    $table_1 .= "<td>$row[drivername]</td>";
    $table_1 .= "<td>$row[cabtype]</td>";
    $table_1 .= "<td>$row[carnumber]</td><td><select>";

echo $table_1;
$query_two="SELECT * FROM vendordriver WHERE vendoremail='$username'";
$run_two=$db->query($query_two);
        while ($row_two=$run_two->fetch_assoc()) {
            echo "<option>$row_two[drivername]</option>";
        }

        echo" </select></td>
        <td><input type='submit' class='btn btn-success' value='SEND '>
        </td>";
    }
}
?>

答案 1 :(得分:0)

将第二个查询的结果定义为数组变量,然后在第一个查询循环中使用该数组回显驱动程序名称,在第一个查询之前执行第二个查询

$query="SELECT * FROM `customerdata` WHERE     takenby='$_SESSION[username]'";
$query_two="SELECT * FROM `vendordriver` WHERE vendoremail='$_SESSION[username]'";
$if(!isset($array_option)){$array_option=array();}
$run_two=$db->query($query_two);
$run=$db->query($query);
while ($row_two=$run_two->fetch_assoc()) {
array_push($array_option,$row_two[driver_name];
}

然后在第一次查询的循环中执行此操作

while ($row=$run->fetch_assoc()) {
echo " <tr><td>$row[bookingid]</td>
<td>$row[drivername]</td>
<td>$row[cabtype]</td>
<td>$row[carnumber]</td><td><select>";
for($i=0;$i<count($array_option);$i++){
echo "<option>$array_option[$i]</option>"
}

然后是其余的代码

echo" </select></td><td><input type='submit' class='btn btn-success' value='SEND '></td>";
}
?>