Jump to content

Drop down lists


pure_skill_2000

Recommended Posts

<?

$conn = mysql_connect('localhost','username','password');

$db = mysql_select_db('database_name',$conn);

 

$query = "SELECT * FROM months";

$result = mysql_query($query);

 

while($row = mysql_fetch_assoc($result))

{

?>

<select value="<?=$row['id'];?>"><?=$row['month'];?></select>

<?

}

?>

 

With the little information I have, try that.

Link to comment
https://forums.phpfreaks.com/topic/101890-drop-down-lists/#findComment-521440
Share on other sites

I would highly suggest having a "monthid" to organize your months via a numerical value (e.g. Jan=1, Feb=2, etc)

 

echo "<select name=months>";

$db->query('SELECT Month FROM table_name ORDER BY monthid ASC');

foreach($db->rows as $month)
{

echo "<option value = " . $month.monthid . ">" . $month.Month . "</option>";

}

echo "</select>";

 

Where $db->rows is your mysql result of all of the month names in the table

$month = the variable assigned to each row, and the period after it gets that column.

 

You'll need to replace table_name with the name of the table these months are in

You'll want to ORDER BY ASC so that the month's are ordered from 1 to 12, and that way you'll get them in order.

 

$month.monthid returns the monthid of the current month it's looping through (so 1 for january).. and this'll be your VALUE.

Then $month.Month returns the actual month name, which will be visible to the user.  (assuming you named that column Month)

 

If you're not using an ADO..

 


echo "<select name=months>";

$sql = mysql_query("SELECT Month FROM table_name ORDER BY monthid ASC");
while($row = mysql_fetch_array($sql))
{

echo "<option value = " . $row['monthid'] . ">" . $row['Month'] . "</option>";

}

echo "</select>";

 

.. may be an easier way to look at things.

 

Link to comment
https://forums.phpfreaks.com/topic/101890-drop-down-lists/#findComment-521447
Share on other sites

Archived

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

×
×
  • 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.