donglinxi1467 2017-07-21 12:56
浏览 372
已采纳

嵌套while循环只运行一次

I'm running two while loops to display a table in html.

  • The first while loop is to display data from a selected databasetable as long as there is content in the database.
  • My second while loop displays a dropdown, where the content of another table should be displayed.

My goal is to show this dropdown each row. My problem is that the dropdown filled with data is just shown in the first tablerow. All the other rows just show an empty selectfield. Can someone help me what I did wrong?

My Code so far:

    $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>'."
"; }?> 
            </select>
        </td>
        </tr>
    <?php } ?>
    </table>
</form>
  • 写回答

3条回答 默认 最新

  • douweiduo7526 2017-07-21 13:05
    关注

    Why don't you try a foreach? Looks much better in this situation. Something like this:

    <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>'."
    "; }?> 
            </select>
        </td>
        </tr>
    <?php } ?>
    </table>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计