Jump to content

drop menu


Shenzi1985

Recommended Posts

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>

Link to comment
Share on other sites

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>

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.