1

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

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

The problem with your code is that pg_fetch_array as I understand it fetches just one row of the data set.
yeah i see. Thx :)
1

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>

Comments

0

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>

1 Comment

The problem with your code is that pg_fetch_array as I understand it fetches just one row of the data set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.