0

I am trying to populate a table with rows from a database. However, one of the columns is a dropdown that needs to be populated with more than one value, therefore needing to use fetchAll. However, I am having trouble since I am using a foreach construct inside another foreach construct.

I'm guessing this isn't possible, or am I just doing something wrong? How can I find a workaround so that I can populate the entire table while also populating the dropdown with more than just one value, but still having it default to the value that it is in the database?

<?php
$sql = "SELECT TOP 100 *
        FROM Table_OS_List
        ORDER BY [CURRENT_SKU] ASC";


$drops = "SELECT [Purchasing_Group]
        FROM Table_OS_List
        GROUP BY [Purchasing_Group]";
$drop = $dbh->query($drops);
?>

<?php
    /* Foreach loop that brings in information to populate table */
    foreach ($dbh->query($sql) as $rows) {
    ?>
    <tr class="row">    
        <td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
        <td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
        <td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>


        <td class="dropdown-select" id="purchgroup">
            <select id="selected_group" class="selected_group" disabled>
                <?php foreach($drop->fetchAll() as $dropdown) { ?>

                  <option class="choice" value="Purchasing Group"><?php echo $dropdown['Purchasing_Group'];?></option>

                <?php } ?>

            </select>
        </td>


        <td><input type="button" class="edit" name="edit" value="Edit"></td>
        <td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
    </tr>
 <?php
  }
 ?>

NOTE This code correctly populates the entire table, however the dropdown list only has the value that is in each row and is not populated with the other value options if I need to select something different:

<?php
    /* Foreach loop that brings in information to populate table */
    foreach ($dbh->query($sql) as $rows) {
    ?>
    <tr class="row">    
        <td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
        <td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
        <td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>


        <td class="dropdown-select" id="purchgroup">
            <select id="selected_group" class="selected_group" disabled>

                <option class="choice" value="Purchasing Group"><?php echo $rows['Purchasing_Group'];?></option>

            </select>
        </td>           

        <td><input type="button" class="edit" name="edit" value="Edit"></td>
        <td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
    </tr>
 <?php
  }
 ?>

EDIT:

Each dropdown is automatically defaulting to Bowling Green even if that row should not be Bowling Green. Most rows are either Southeast or Michigan but regardless of what it should be, it is defaulting to Bowling Green for some reason

enter image description here

enter image description here

3
  • My first guess, you are just doing it wrong, lets have a closer look Commented Sep 21, 2018 at 14:34
  • 1
    This needs to be changed as well <option class="choice" value="<?php echo $rows['Purchasing_Group'];?>"><?php echo $rows['Purchasing_Group'];?></option> Commented Sep 21, 2018 at 14:36
  • Create a new query for the drop downs instead of using the first row result from the initial query. Commented Sep 21, 2018 at 14:38

1 Answer 1

2

Yes, doing it wrong, you are consuming the second resultset on completion of the first iteration of the outer foreach so instead retrieve the dropdown contents into an array so you can reuse it many times

<?php
$sql = "SELECT TOP 100 *
        FROM Table_OS_List
        ORDER BY [CURRENT_SKU] ASC";


$drops = "SELECT [Purchasing_Group]
        FROM Table_OS_List
        GROUP BY [Purchasing_Group]";
$drop = $dbh->query($drops);
$allDrops = $drop->fetchAll();

?>

<?php
    /* Foreach loop that brings in information to populate table */
    foreach ($dbh->query($sql) as $rows) {
    ?>
    <tr class="row">    
        <td class="old_sku" id="old_sku"><?php echo intval ($rows['OLD_SKU'])?></td>
        <td class="current_sku" id="current_sku"><?php echo intval ($rows['CURRENT_SKU'])?></td>
        <td class="id" id="id" style="display: none;"><?php echo intval ($rows['ID'])?></td>


        <td class="dropdown-select" id="purchgroup">
            <select id="selected_group" class="selected_group" disabled>
                <?php 
                foreach($allDrops as $dropdown) { 
        //--------------^^^^^^^^^
        // Also changed the way you fill the option tag below
                ?>

                  <option class="choice" 
                          value="<?php echo $dropdown['Purchasing_Group'];?>">
                    <?php echo $dropdown['Purchasing_Group'];?>
                  </option>

                <?php } ?>

            </select>
        </td>


        <td><input type="button" class="edit" name="edit" value="Edit"></td>
        <td><input type="button" class="delete" name="delete" id="<?php echo intval ($rows['ID'])?>" value="Delete"></td>
    </tr>
 <?php
  }
 ?>
Sign up to request clarification or add additional context in comments.

16 Comments

I am pretty sure the real issue is that value="Purchasing Group" is hard-coded so regardless of the option selected, only one choice is actually available.
@MonkeyZeus Ooo let me check that
Yeah, OP's question is quite scatter-brained and a bit incoherent so it's an easy detail to miss.
@MonkeyZeus Except once you have done a FetchAll the resultset is fully consumed, OP would have to re-issue the query to re-populate the resultset
I had another foreach elsewhere in my code that was using the previous fetchAll before you changed it. I went ahead and fixed that and now it works so everything is good from your end. Thank you!
|

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.