pure_skill_2000 Posted April 19, 2008 Share Posted April 19, 2008 Does anyone know the code of a tutorial for populating a drop down list from records in a mysql database using php for example; Field Month With options; Jan Feb March April I want the drop down list to populate with the months in that field of the database. Thanks! Quote Link to comment Share on other sites More sharing options...
EKINdesigns Posted April 19, 2008 Share Posted April 19, 2008 <? $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. Quote Link to comment Share on other sites More sharing options...
pure_skill_2000 Posted April 19, 2008 Author Share Posted April 19, 2008 This seems to really dislike the while($row = mysql_fetch_assoc($result)) Quote Link to comment Share on other sites More sharing options...
southofsomewhere Posted April 19, 2008 Share Posted April 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.