Shenzi1985 Posted December 12, 2011 Share Posted December 12, 2011 im fairly new to php so tend to do trial and error..... more error than trial tbh. im wondering if it is possible to get a drop down menu to fill from a mysql database and to arrange it alphabetically. i have created the menu just dont know how to arrange it better. also how can i use the items id in drop menu to load other info from that row on the database. hope you can help me. <FORM> <?php $result = mysql_query( "SELECT * FROM movie_info" ) ; echo "<select name= film onChange='submit()' >film name</option>"; while ($nt=mysql_fetch_array($result)){ ?> <?php echo "<option value='$nt[id]'>$nt[title] </option>"; } ?> </select> </FORM> Quote Link to comment https://forums.phpfreaks.com/topic/253016-drop-menu/ Share on other sites More sharing options...
KevinM1 Posted December 12, 2011 Share Posted December 12, 2011 Yes, it's definitely possible. Since we, here at PHP Freaks, like to teach best practices, this is going to be a fairly long message. You're doing some things that will only cause headaches for you down the road. Things that are better to correct now, when you're a beginner, rather than later. Don't mix PHP and HTML when you don't need to. One of the greatest 'problems' with PHP is that it allows one to embed their script code within their HTML code. The way to write good PHP scripts is to do all of your data processing first, then store the results in variables. You can then easily output them with a small amount of simple PHP. The same goes for JavaScript and CSS. Don't write JavaScript code or CSS directly in tags. CSS should be kept to external files. JS libraries should be referenced in the <head> while custom JS code for a particular page written in a <script> tag at the bottom of the page. I write mine after </body> but before </html>. By keeping code separate, you do the following: Make it easier to write. Make it easier to edit without running the risk of screwing something else up. Make it easy to swap out entire parts, if necessary. Make it easy to debug as your code won't be nested in code of other languages. Make it easy to zone in on the problem area, and send that relevant block of code to others (read: us) for help. --- Okay, now on to your problem, try something like: <?php $query = "SELECT * FROM movie_info ORDER BY title ASC"; $movies = mysql_query($query); ?> <!-- HTML code up to your form --> <form action="" method="post"> <select> <?php while($row = mysql_fetch_assoc($movies)) { echo "<option value='{$row['id']}'>{$row['title']}</option>"; } ?> </select> </form> Quote Link to comment https://forums.phpfreaks.com/topic/253016-drop-menu/#findComment-1297190 Share on other sites More sharing options...
Shenzi1985 Posted December 12, 2011 Author Share Posted December 12, 2011 thanks for the quick answer worked great. reason i wanted it like this is so when i select a film name it brings up all the info on that film from the database. so gonna have a play with it now. no doubt i be back soon Quote Link to comment https://forums.phpfreaks.com/topic/253016-drop-menu/#findComment-1297203 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.