How can I pre-select an option in a select menu based on a value stored in the database?

ChriChri

I have a number of dropdown lists, which update a database, however once updated the first object on the list is displayed, rather than the actual object selected. What is the best way to fix this?

Code sample: HTML code sample

<select name="sleeps">
       <option value="0">Sleeps</option>
       <option value='1'>1</option>
       <option value='2'>2</option>
       <option value='3'>3</option>
       <option value='4'>4</option>
</select>

This is generated using:

 <?php 
                                for ($x = 1; $x <=10; $x++) {
                                    echo "<option value='$x'>$x</option>";
                                }
?>

Desired result

 <select name="sleeps">
       <option value="0">Sleeps</option>
       <option value='1'>1</option>
       <option value='2'>2</option>
       <option value='3'>3</option>
       <option value='4' selected>4</option>
</select>
ChriChri

Figured it out :)

Note - $echo and $title are there to allow changes in order to allow flexibility.

function select_dropdown($sql, $echo, $title) {
include 'connect.php';
$id = $_GET['id'];
$select = $conn->prepare($sql);
$select->bind_param('s', $id);
$select->execute();
$select->store_result();
if ($select->num_rows > 0) {
    $meta = $select->result_metadata();
echo "<select name='$title'>
                        <option value='0'>$title</option>\n";
    while ($field = $meta->fetch_field()) {
        $params[] = &$row[$field->name];
    }
    call_user_func_array(array($select, 'bind_result'), $params);
    while ($select->fetch()) {
                 for ($x = 1; $x <=10; $x++) {
             if ($row[$echo] == $x) {
                 echo "<option value='$row[$echo]' selected>$row[$echo]</option>";
             } else {
             echo "<option value='$x'>$x</option>";
             }
                            }

    }
    $select->close();
    echo "</select>";
}

}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I pre-select a dropdown option using previously submitted data from database, MySQLI

From Dev

How can I fill up select option menu from database with more fields?

From Dev

How to select an option based on value

From Dev

How can i select data stored in a database with PHP

From Dev

How will i input a value on database depending on the Select Option of HTML

From Dev

How can I set a select option value without using jQuery?

From Dev

How can I hide an option of a select by it's text value?

From Dev

How can I get value when click option in the select on vue?

From Dev

use jQuery to select option from menu based on its text not value

From Dev

How can I create a simple <select> in AngularJS with an option pre-selected?

From Dev

how can i select item from option menu if i have one item

From Java

How can i echo value from database INTO html <select> tag?

From Dev

How can i store multiple select box value in database in codeigniter?

From Dev

How do I pre-select rows in a DataTable based on the value in a column?

From Dev

How can I customize a select dropdown option

From Dev

How I can hide div on option select

From Dev

How can I select and option with jquery?

From Dev

How can I update the other select2 selectbox value based on the value of a select2 selectbox?

From Dev

Django: Can't pre-select from option menu Upon POST

From Dev

How can I pre-select a value from a dropdown list with meteor and autoform?

From Dev

How to pre select value with "Select2"

From Dev

Pre-select the dropdown select option according to value received

From Dev

Where is the select option current value stored?

From Dev

How do I pre-select an option in my Rails f.select item?

From Dev

In Javascript how do I add a new option into Html Select so inserted based on alphabetical value of text

From Dev

How can pre-load the value in Angular material select?

From Dev

How can I change jquery mobile select menu button background color based to selection?

From Dev

Default value to select option menu is not recognized

From Dev

How can I obtain a value from a stored procedure (SELECT COUNT) in c#

Related Related

  1. 1

    How can I pre-select a dropdown option using previously submitted data from database, MySQLI

  2. 2

    How can I fill up select option menu from database with more fields?

  3. 3

    How to select an option based on value

  4. 4

    How can i select data stored in a database with PHP

  5. 5

    How will i input a value on database depending on the Select Option of HTML

  6. 6

    How can I set a select option value without using jQuery?

  7. 7

    How can I hide an option of a select by it's text value?

  8. 8

    How can I get value when click option in the select on vue?

  9. 9

    use jQuery to select option from menu based on its text not value

  10. 10

    How can I create a simple <select> in AngularJS with an option pre-selected?

  11. 11

    how can i select item from option menu if i have one item

  12. 12

    How can i echo value from database INTO html <select> tag?

  13. 13

    How can i store multiple select box value in database in codeigniter?

  14. 14

    How do I pre-select rows in a DataTable based on the value in a column?

  15. 15

    How can I customize a select dropdown option

  16. 16

    How I can hide div on option select

  17. 17

    How can I select and option with jquery?

  18. 18

    How can I update the other select2 selectbox value based on the value of a select2 selectbox?

  19. 19

    Django: Can't pre-select from option menu Upon POST

  20. 20

    How can I pre-select a value from a dropdown list with meteor and autoform?

  21. 21

    How to pre select value with "Select2"

  22. 22

    Pre-select the dropdown select option according to value received

  23. 23

    Where is the select option current value stored?

  24. 24

    How do I pre-select an option in my Rails f.select item?

  25. 25

    In Javascript how do I add a new option into Html Select so inserted based on alphabetical value of text

  26. 26

    How can pre-load the value in Angular material select?

  27. 27

    How can I change jquery mobile select menu button background color based to selection?

  28. 28

    Default value to select option menu is not recognized

  29. 29

    How can I obtain a value from a stored procedure (SELECT COUNT) in c#

HotTag

Archive