fetch data from database display data in drop down menu

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saurabhbpl
    New Member
    • Nov 2006
    • 4

    fetch data from database display data in drop down menu

    hi,
    i have a html drop down menus and i want to conver it into dynamically means
    that menu show data from database.how can i fetch data from database for using menus and sub menus using php and mysql.
  • khalidbaloch
    New Member
    • Oct 2006
    • 61

    #2
    please provid you database table info and let us know what do u want to retrive ...

    Comment

    • dswethar
      New Member
      • Nov 2006
      • 65

      #3
      Hi,

      I am not from PHP. But assuming that you know how to connect to the database and retrieve information, I'll explain to you how the drop down box should be filled.

      ---------------------------
      Open Database connections
      retrieve data

      <select name="" id="">
      loop i to the end of file
      <option value="value of the option (most of the time the id)">option name</option>
      end loop
      </select>

      I am sorry I cannot give you the full syntax. But you should have an idea now to continue with your work.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        This sample will give you an idea onn how to populate a drop down using db values:
        [php]// you have already opened your db connection
        $res = mysql_query("SE LECT value FROM table ORDER BY value")
        or die("Invalid query: " . mysql_query());
        echo '<label>Select value:</label>';
        echo '<select id="val" name="val">';
        echo '<option value="">Select </option>';
        while ($row = mysql_fetch_ass oc($res)) {
        $va = $row['value'];
        echo "<option value='$va'>$va </option>";
        }
        echo '</select>';[/php]
        Ronald :cool:

        Comment

        • saurabhbpl
          New Member
          • Nov 2006
          • 4

          #5
          Originally posted by khalidbaloch
          please provid you database table info and let us know what do u want to retrive ...
          hi,
          thanks for your reply, i give you some detail of my database.
          i have a two table listings_detail s and sublisting_deta ils.
          there are 3 columns In listings_detail s table: listing_id(p.k. ),domain,listin g_title.
          And also 3 columns in sublisting_deta ils table: sublisting_id(p .k.),listing_id ,sublisting_tit le.
          Now i want to fetch the listing_title and sublisting_titl e and show into the dropdown menu.listing_ti tle should display in mainmenu and sublisting_titl e display in submenu.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Well, you have the samples, what more do you want?

            Ronald :cool:

            Comment

            • techsoul
              New Member
              • Jul 2010
              • 4

              #7
              hi dear ...i m a newbie
              here is d code tested on db oracle,iis server,php using sql as query lang.


              Code:
              $conn=oci_connect("xyz","1234","localhost/XE");
              
                if(!conn){
                echo "connection error";
                exit;
                }
                
              	session_start(); 
              	$USER_ID=$_SESSION['USER_ID'];
                  
              	$query="SELECT WEEK_ID FROM WEEKLY_REPORTS WHERE WR_EMPLOYEE_ID=$USER_ID ";
              	$editWR=oci_parse($conn,$query);
              	oci_execute($editWR);
              	echo '<table border=1>'.'<tr>'.'<td>'.'<div>';
              	echo '<label>'."Week ID:".'</label>';
                  echo'<SELECT name="weekid" id="weekid">';
              	echo '<OPTION VALUE="">'."Select".'</OPTION>';
              	while($row = oci_fetch_array($editWR,OCI_ASSOC))
              	{
              	$WID=$row ['WEEK_ID'];
              	echo'<OPTION VALUE='."$WID".'>'.$WID.'</OPTION>';
              	}
              	echo '</SELECT>'.'</div>'.'</td>'.'</tr>'.'</table>';
              i hope it must help ....
              check:i think mysql use '+' instead of '.' to concatenate html elements in echo field.
              Last edited by Niheel; Jul 8 '10, 03:27 PM. Reason: please use code tags to display code

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by techsoul
                hi dear ...i m a newbie
                here is d code tested on db oracle,iis server,php using sql as query lang.


                Code:
                $conn=oci_connect("xyz","1234","localhost/XE");
                
                  if(!conn){
                  echo "connection error";
                  exit;
                  }
                  
                	session_start(); 
                	$USER_ID=$_SESSION['USER_ID'];
                    
                	$query="SELECT WEEK_ID FROM WEEKLY_REPORTS WHERE WR_EMPLOYEE_ID=$USER_ID ";
                	$editWR=oci_parse($conn,$query);
                	oci_execute($editWR);
                	echo '<table border=1>'.'<tr>'.'<td>'.'<div>';
                	echo '<label>'."Week ID:".'</label>';
                    echo'<SELECT name="weekid" id="weekid">';
                	echo '<OPTION VALUE="">'."Select".'</OPTION>';
                	while($row = oci_fetch_array($editWR,OCI_ASSOC))
                	{
                	$WID=$row ['WEEK_ID'];
                	echo'<OPTION VALUE='.'$WID'.'>'.$WID.'</OPTION>';
                	}
                	echo '</SELECT>'.'</div>'.'</td>'.'</tr>'.'</table>';
                i hope it must help ....
                check:i think mysql use '+' instead of '.' to concatenate html elements in echo field.
                That code is just awful. Please don't offer help if you're going to post such awful examples.

                1. Why are you concatenating strings that simply do not need to be concatenated?
                2. Why switch, part way through a string, from single-quotes to double-quotes?
                3. Variables are not parsed within single-quoted strings, so line #21 will not work as you expect.

                Furthermore, please don't resurrect such old threads.

                Mark.

                Comment

                • techsoul
                  New Member
                  • Jul 2010
                  • 4

                  #9
                  first thing first...i am newbie
                  now
                  1.if i do not concatenate it shows "HTTP Error 500.0 - Internal Server Error
                  The page cannot be displayed because an internal server error has occurred."
                  if there is a way to avoid it that will be really helpful may be some setting in php.ini or something else
                  because the code is in php and the code which is concatenated is in basic html.I think this is the cause the error ....concatenati on sort out the gap between two
                  .then it works fine

                  2.double quotes are used for string which is echoed as written which are neither variable nor html elements

                  3.yes you are right line #21 is not working.What change do I need to make?let me try....your help will be appreciated.
                  I am not sure extent of correctness of these...I change values and try things and this program is working fine for me on base php,sql,oracle, iis.

                  if anything that is wrong here i will surely like to know the reasons...
                  thanks @Markus's Avatar
                  Markus
                  for pointing out my mistakes.This saved a lot of my time.

                  Comment

                  • techsoul
                    New Member
                    • Jul 2010
                    • 4

                    #10
                    line 21 corrected : '$WID'->"$WID".

                    Comment

                    • TheServant
                      Recognized Expert Top Contributor
                      • Feb 2008
                      • 1168

                      #11
                      What Markus means by "concatinat ion is not needed" is instead of:
                      Code:
                      echo '<table border=1>'.'<tr>'.'<td>'.'<div>';
                      // USE
                      echo '<table border=1><tr><td><div>';
                      Line 21 should be:
                      Code:
                      echo '<OPTION VALUE=\''.$WID.'\'>'.$WID.'</OPTION>';
                      // OR
                      echo "<OPTION VALUE='$WID'>$WID</OPTION>";
                      Notice the space after the echo. You are right about there being a difference between a single quote and a double, but if you are breaking the string to put a variable (option 1), you do not need to put the variable in double quotes. If you prefer to not break the string, you can have the whole string in double quotes and do not need to break it for a variable. Also, think about the HTML when you're writing a PHP string. Your <option> should look like:
                      Code:
                      <option value='myVal'>myVal</option>
                      // NOT
                      <option value=myVal>myVal</option>
                      So you need to escape some strings using '\'.

                      That's all for now.

                      Comment

                      • Arshiya Sultana
                        New Member
                        • Oct 2014
                        • 1

                        #12
                        Thank you so much it really very help me once again thank you so much

                        Comment

                        Working...