Abdul Waheed Abdul Waheed - 29 days ago 7
Ajax Question

How can i add dropdowns based on number present in the database

For an example
In my database i have a column name floor and the value i have inserted is 4
i want to show in a form like if the value of the floor is 4, i want to show drop downs based on the value present in the database here it is 4

<select name="floor">
<option value=0>Floor</option>
<option value=1>First Floor</option>
<option value=2>Second Floor</option>
<option value=3>Third Floor</option>
<option value=4>Fourth Floor</option>
</select>


How can i achieve this one, is it possible..? please suggest edit if question not reach the standards.

Answer

If I understand correctly here is one very simple example since I don't know anything about DB and your other source:

// Connection data (server_address, database, name, password)
$hostdb = 'localhost';
$namedb = 'yourdbname';
$userdb = 'yourdbuser';
$passdb = 'yourdbpass';

try {
    // Connect and create the PDO object
    $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
    $conn->exec("SET CHARACTER SET utf8");      // Sets encoding UTF-8

    // Selects the rows in which "floor_id" is 2
    $sql = "SELECT *  FROM floor WHERE floor_id = 2";
    $result = $conn->query($sql);

    // Parse the result set
    echo '<select name="floor">';

    while($row = $result->fetch()) {
        echo '<option value="'.$row['floor_value'].'">'.$row['floor_name'].'</option>';
    }

    echo '</select>';
    $conn = null;        // Disconnect
}
catch(PDOException $e) {
    echo $e->getMessage();
}

This snippet will show on the page select dropdown which are with value 2. More information about PDO. Few examples for MySQLi_* and PDO

Comments