0

I am currently populating a table with rows from a database. One of the columns is a dropdown that needs to be populated with multiple values.

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.

How can I have the dropdowns keep all values as options in the dropdown (bowling green, michigan, southeast), but have the dropdown value 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);
$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) { ?>

              <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 } ?>

Example of what it looks like now. You can see that it has multiple options which is what I need, but, regardless of it's value in the database, is defaulting to Bowling Green:

enter image description here

enter image description here

2
  • 1
    You need to mark the appropriate <OPTION> with the SELECTED attribute. Commented Sep 24, 2018 at 15:02
  • 1
    That's because it is the first option in the dropdown. For it to select it based on the value from the database, you need to add the selected attribute to the option Commented Sep 24, 2018 at 15:02

2 Answers 2

2

You need to add the selected attribute to the correct option.

Something like this:

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

Where $row['Purchasing_Group'] is the column in the Table_OS_List table.

The important code here is <?php echo $row['Purchasing_Group'] == $dropdown['Purchasing_Group'] ? "selected" : ""; ?> - this is known as a Ternary, it could also be written in expanded form like this:

<?php
    if($row['Purchasing_Group'] == $dropdown['Purchasing_Group']) {
        echo "selected";
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect and is what I needed...thank you. Will accept this as the answer when I am able to
@Rataiczak24 Glad that I could help
0

You can add a selected attribute with a inline ternary operator just like so;

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

and after you fetching all the datas from the DB, always good to check if result or results existing.

1 Comment

Every row should have an existing result in OP's code, your code will add selected to every single option, you have to check if the option it's creating is the same as the row.

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.