▼  Site Navigation Main Articles News Search  ▼  Anime + Manga Anime Reviews Anime Characters Gallery Screenshots Manga Reviews  ▼  Misc Links to Webcomics Bible Quotes About Older Musings
site version 7.3
PHP and MySQL –– drop down menu
written by: admin

Date Written: 1/12/08 Last Updated: 1/15/10

This code will select a field, like title, from all the rows in a database table and list them as a drop down menu.  You will see that a form is generated and a name and value is assigned to each title.  The name is sel_reviewID, the value is the row# (the ID), and the title is the title that was pulled from the table.  It is the value that is passed to the next page, which in this case is the ID.  $_SERVER[PHP_SELF] is used here to tell the code that the submitted code will re–execute the page with the values that were passed to it with the values being whatever file was selected from the drop down menu.

     $display_block = "<h1>Select an Entry</h1>";

     //get parts of records
     $get_list = "select ID,title from table";
     $get_list_res = mysql_query($get_list) or die(mysql_error());

     if (mysql_num_rows($get_list_res) < 1) {
         //no records
         $display_block .= "<p><em>Sorry, no records to select!</em></p>";

     } else {
         //has records, so get results and print in a form        
         $display_block .= "
         <form method=\"POST\" action=\"$_SERVER[PHP_SELF]\">
         <P><strong>Select a Record to View:</strong><br>
         <select name=\"sel_reviewID\">
         <option value=\"\">-- Select One --</option>";

         while ($recs = mysql_fetch_array($get_list_res)) {
             $ID = $recs['ID'];
             $title1 = $recs['title'];
             $display_block .= "<option value=\"$ID\">
             $r="there are $nrows files in this table.";
        $display_block .= "
        <p><input type=\"submit\" name=\"submit\"  value=\"View Selected Entry\"></p>
        </FORM> $r";

print $display_block;

TAGS: mysql, php
copyright 2005–2024