Does anyone here know how to populate a dropdown list in PHP from a MySQL table? where the list would be all the records from a particular column? I am using windows XP pro.
Thanks for your help..
Does anyone here know how to populate a dropdown list in PHP from a MySQL table? where the list would be all the records from a particular column? I am using windows XP pro.
Thanks for your help..
Creates a simple drop down list from a SQL query. Column 1 of the query should be the reference, column 2 should be the descriptive text / list entry. You still have to create the <select name=""> tag within your HTML.
The default value is used to preselect an entry. The value of default is a value that will be in column 1 of the query results.
The value for blank is inserted at the top of the drop down list if required, either as a default value or for informative or instructive text.
Example:PHP Code:
<?php
function db_createlist($linkID,$default,$query,$blank)
{
if($blank)
{
print("<option select value=\"0\">$blank</option>");
}
$resultID = pg_exec($linkID,$query);
$num = pg_numrows($resultID);
for ($i=0;$i<$num;$i++)
{
$row = pg_fetch_row($resultID,$i);
if($row[0]==$default)$dtext = "selected";
else $dtext = "";
print("<option $dtext value=\"$row[0]\">$row[1]</option>");
}
}
?>
PHP Code:
<select name="select">
<?php
// default is 0, no entry will be selected.
db_createlist($linkID,0,
"select id,description from list","Please select one ...");
?>
</select>
Hi
I hope i can explain this well!!
I have a dynamic drop down list (populated using a MYSQL query and PHP). When the user selects an item from the list and submits it runs another SQL query to pull items from another table where id equals that of the selected drop down item.
PHP CODECode:<form method="POST" action="view_project.php"> <input type="hidden" name="sector" value="sector_list"> <select name="sector_list" class="inputstandard"> <option value="default">Select Sector</option> <option value="1" name="Housing & Residential">Housing & Residential</option><option value="2" name="Retail">Retail</option><option value="3" name="Public Sector">Public Sector</option><option value="4" name="Hotel & Leisure">Hotel & Leisure</option><option value="5" name="Commercial Development">Commercial Development</option> </select> <input type="submit" value="Go!"> </form>
PHP Code:
<form method="POST" action="view_project.php">
<input type="hidden" name="sector" value="sector_list">
<select name="sector_list" class="inputstandard">
<option value="default">Select Sector</option>
<?
$result = mysql_query('SELECT * FROM team ORDER BY T_ID ASC')
or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row['T_ID'] . '" name="' . $row['T_Team']. '">' . $row['T_Team']. '</option>';
}
?>
</select>
<input type="submit" value="Go!">
</form>
Bookmarks