Category: Software
Tags: Amateur Radio MySQL PHP Raspberry Pi
When making a web site, there are many time when a drop down list is called for. In HTML the options are are all hard coded as below:-
<select naee="test">
<option>first option</option>
<option>Second option</option>
<option>Third option</option>
</select>
However if that group is to be used on several pages then any changes require the update to each page. There is another option.
Put all the options in a database (mysql, sqlite, anyother) and then only the database needs to be changed and all will be updated.
The data base table "dropdowntable" with fileds id as an int with auto increment and list as varchar 20
So using php here is an example
Assuming the database is logged in.
a function can be set up
<?php
function dropDownList()
{
$sql = "select * from dropdowntable";
$result =$mysqli -> query($sql);
while ($row = $result -> fetch_row())
{
echo "<option>$row[1]</option>
}
}
?>
and the HTML would be :-
<select name="mylist">
<?php dropDownList(); ?>
</select>
The table names and columns can be changed to suite your requirements. The line $row[1] the number 1 refers to column 1 in the database (starts at 0) as 0 is the id number.