gpt4 book ai didi

php - 嵌套的 while 循环只运行一次

转载 作者:行者123 更新时间:2023-11-28 05:16:23 25 4
gpt4 key购买 nike

我正在运行两个 while 循环以在 html 中显示表格。

  • 第一个 while 循环是显示选定数据库表中的数据,只要数据库中有内容。
  • 我的第二个 while 循环显示一个下拉列表,其中应显示另一个表的内容。

我的目标是在每一行显示这个下拉菜单。我的问题是充满数据的下拉列表只显示在第一个表格行中。所有其他行只显示一个空的选择字段。有人可以帮助我做错什么吗?

到目前为止我的代码:

    $link=pg_connect($conn_str); 
if (!$link) {die('Verbindung schlug fehl.');}

$arbeitspaket = pg_query($link, "SELECT * FROM arbeitspaket WHERE id='$_SESSION[user_id]'");

$sql = "SELECT * FROM anwender ORDER BY nachname ASC";
$mitarbeiter = pg_query($link, $sql); ?>

<form action=mitarbeiterauswahl.php method=post>

<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>

<?php while($results=pg_fetch_array($arbeitspaket)){?>
<tr>
<td><?php echo $results['apid']; ?></td>
<td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $results['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php while($row = pg_fetch_array($mitarbeiter)){
echo '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>
</form>

最佳答案

你为什么不试试 foreach?在这种情况下看起来好多了。像这样的东西:

<table border=1>
<tr>
<th>Arbeitspaket-ID</th>
<th>Arbeitspaketbezeichnung</th>
<th>Mitarbeiterbedarf</th>
<th>Mitarbeiterzuordnung</th>
</tr>
<?php $results= pg_fetch_array($arbeitspaket);
$arbeiters = pg_fetch_array($mitarbeiter);
foreach($results as $result){?>
<tr>
<td><?php echo $result['apid']; ?></td>
<td><?php echo $result['arbeitspaketbezeichnung']; ?></td>
<td><?php echo $result['mitarbeiterbedarf']; ?></td>
<td>
<select name="mitarbeiter">
<?php foreach($arbeiters as $arbeiter) {
echo '<option value="'. $arbeiter) ['id'] .'">('. $arbeiter) ['id'] .') '. $arbeiter) ['vorname'] .' '. $arbeiter) ['nachname'] .'</option>'."\n"; }?>
</select>
</td>
</tr>
<?php } ?>
</table>

关于php - 嵌套的 while 循环只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45238294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com