Jump to content

Archived

This topic is now archived and is closed to further replies.

Jezreel

Brainteaser for Php/MySql Guru - Please help.

Recommended Posts

Ok.. I am a beginner at Php/MySql, however most of the time I can feel my way around in the dark. However, trying to do this sort of hurts my head.

I need to have a drop down list which retrieves the data for the drop down list from a MySql Database.

The data is in a table that will constantly be growing so obviously the items in the drop down list will grow as well. So the table is called "Songs" and the field is s_title. When the drop down list retreives the data I need it to place the songs in alphabetical order as well.

Any takers?

Thanks in advance,

Jezreel

Share this post


Link to post
Share on other sites
Well, a simple MySQL query can retrieve the fields in whatever order you desire, and I'm sure there are literally hundreds of tutorials on how to dump these results via PHP into a drop-down / combobox / whatever.

Share this post


Link to post
Share on other sites
Jezreel

I am a noobie too but here is the method I used to create something similar, I assume your songs table has id, s_title columns

This works for me so I hope this helps...but with some luck someone will come along and explain a more elegant method.
Kind regards
DD

<?
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// config connection to dbase[!--colorc--][/span][!--/colorc--]
$dbhost='localhost';
$dbusername='XXXX';
$dbuserpass='XXXX';
$dbname='XXXX';

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// connect to dbase[!--colorc--][/span][!--/colorc--]
$conn = mysql_connect ($dbhost, $dbusername, $dbuserpass);

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// check connection[!--colorc--][/span][!--/colorc--]
if (!mysql_select_db($dbname)) die(mysql_error());

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]//query setup[!--colorc--][/span][!--/colorc--]
$sql="SELECT id, s_title FROM Songs ORDER BY s_title ASC";
$result=mysql_query($sql);
?>

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// the line below identifies the dropdown[!--colorc--][/span][!--/colorc--]
<select id="ST_DD" name="ST_DD">

<?
$options="";
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]// loop to insert values into dropdown[!--colorc--][/span][!--/colorc--]
while ($row=mysql_fetch_array($result)) {

$id=$row["id"];
$title=$row["s_title"];
$options.="<OPTION VALUE=\"$id\">".$title.'</option>';
}
?>
[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]//Display the Dropdown[!--colorc--][/span][!--/colorc--]
<?php echo $options ?>
</select>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.