简体   繁体   English

嵌套的while循环仅运行一次

[英]Nested while loop is only running once

I'm running two while loops to display a table in html. 我正在运行两个while循环以html显示表格。

  • The first while loop is to display data from a selected databasetable as long as there is content in the database. 只要数据库中有内容,第一个while循环就是显示来自所选数据库表的数据。
  • My second while loop displays a dropdown, where the content of another table should be displayed. 我的第二个while循环显示一个下拉列表,其中应显示另一个表的内容。

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. 其他所有行仅显示一个空的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>'."\n"; }?> 
            </select>
        </td>
        </tr>
    <?php } ?>
    </table>
</form>

Why don't you try a foreach? 你为什么不尝试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>'."\n"; }?> 
        </select>
    </td>
    </tr>
<?php } ?>
</table>

The problem in your code is that you consume all the data in the first loop, after that it is gone. 您的代码中的问题是,在第一个循环中,所有数据都消失了之后,便将其消耗掉了。 Store the data in an array and iterate over the array. 将数据存储在数组中并遍历数组。

<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
      $mitarbeiter_array = array();
      while($row = pg_fetch_array($mitarbeiter)) {
        $mitarbeiter_array[] = $row;
      }
      unset($row);
    ?>

    <?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 foreach($mitarbeiter_array as $row){
                 echo '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>'."\n"; }?> 
            </select>
        </td>
        </tr>
    <?php } ?>
    </table>
</form>

i think it will be better way and will be fast 我认为这将是更好的方法,并且会很快

<form action=mitarbeiterauswahl.php method=post>

<?php 
$select_html = '<select name="mitarbeiter">'; 
$select_list_data = pg_fetch_array($mitarbeiter);  
?>
<?php foreach($select_list_data as $row){  ?>
<?php 
     $select_html .= '<option value="'. $row['id'] .'">('. $row['id'] .') '. $row['vorname'] .' '. $row['nachname'] .'</option>';
 ?> 

<?php } ?>
<?php $select_html .= '</select>'; ?>


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

 $fetch_results = pg_fetch_array($arbeitspaket);
 foreach($fetch_results as $results){ ?>          
    <tr>
    <td><?php echo $results['apid']; ?></td>
    <td><?php echo $results['arbeitspaketbezeichnung']; ?></td>
    <td><?php echo $results['mitarbeiterbedarf']; ?></td>
    <td>
        <?php echo $select_html; ?>
    </td>
    </tr>
<?php } ?>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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