Jump to content

Brainteaser for Php/MySql Guru - Please help.


Jezreel

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

Link to comment
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.
Link to comment
Share on other sites

  • 1 month later...
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.