0

I have a Edit form in php and everything work great. The only issue i have is when i click edit it returns all the data except in the Select drop down. it does not have the chosen category it always shows the first value in the list. But i then can click on the drop down and choose a new category and it works.

//Query the category table
$resultSet = $con->query("SELECT * FROM schedule_category");

<select id="schedule_category" name="schedule_category" class="custom-select">
              <?php
               while($rows = mysqli_fetch_assoc($resultSet))
                {
            ?>
<option value = "<?php echo($rows['schedule_category'])?>">
         <?php echo($rows['schedule_category']) ?>
        </option>
        <?php
                }
                 ?>
               </select>

I would like to have it show the correct select option record not the first one in the drop down list. Here is an image of what happens https://imgur.com/a/XVXQ2Sa
1
  • Please print_r($rows) after while loop and check result. Commented Sep 3, 2019 at 10:50

1 Answer 1

1

You'll need to have your code compare each to the selected value, and add the appropriate keyword:

$previous_selection = // whatever it is, from your data
  while($rows = mysqli_fetch_assoc($resultSet))
  {
  $thisone = $previous_selection == $rows['schedule_category'] ? " selected " : "";
  echo '<option value = "';
  echo ($rows['schedule_category']) . '"' . $thisone . '>';
  echo($rows['schedule_category']) . '</option>';
}

What you're doing here is comparing your previously-selected value to each row, when it matches, the variable $thisone is set to "selected", otherwise it's empty. You then add that to each option line after the value and before the close-tag for the option, and it will add "selected" when the value matches.

Also I personally don't like switching in and out of PHP for no good reason, makes it really difficult to read, hence I echo the various bits of HTML here.

ETA - actually that could be simplified further, if your selection value is the same as the text displayed in the list, there's no need to actually specify the value in the option tag. That is only required when the value is different to the display, for example if you want the user to see your category names, but you want to submit the category ID.

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

3 Comments

Hi All i still cannot work it out click hete to see what happens imgur.com/a/XVXQ2Sa
An image doesn't help - you need to show the code that generates it. If you view the source of that page, is it setting any value to be selected?
Sorry, I can't see it. Where did you put the PHP code so I can try to help?

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.