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