471,853 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,853 software developers and data experts.

fetch data from database display data in drop down menu

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.
Nov 17 '06 #1
11 38661
please provid you database table info and let us know what do u want to retrive ...
Nov 17 '06 #2

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

I am sorry I cannot give you the full syntax. But you should have an idea now to continue with your work.
Nov 17 '06 #3
4,258 Expert 4TB
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("SELECT 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_assoc($res)) {
$va = $row['value'];
echo "<option value='$va'>$va</option>";
echo '</select>';[/php]
Ronald :cool:
Nov 17 '06 #4
please provid you database table info and let us know what do u want to retrive ...
thanks for your reply, i give you some detail of my database.
i have a two table listings_details and sublisting_details.
there are 3 columns In listings_details table: listing_id(p.k.),domain,listing_title.
And also 3 columns in sublisting_details table: sublisting_id(p.k.),listing_id,sublisting_title.
Now i want to fetch the listing_title and sublisting_title and show into the dropdown menu.listing_title should display in mainmenu and sublisting_title display in submenu.
Nov 18 '06 #5
4,258 Expert 4TB
Well, you have the samples, what more do you want?

Ronald :cool:
Nov 18 '06 #6
hi dear ...i m a newbie
here is d code tested on db oracle,iis server,php using sql as query lang.

Expand|Select|Wrap|Line Numbers
  1. $conn=oci_connect("xyz","1234","localhost/XE");
  3.   if(!conn){
  4.   echo "connection error";
  5.   exit;
  6.   }
  8.     session_start(); 
  9.     $USER_ID=$_SESSION['USER_ID'];
  12.     $editWR=oci_parse($conn,$query);
  13.     oci_execute($editWR);
  14.     echo '<table border=1>'.'<tr>'.'<td>'.'<div>';
  15.     echo '<label>'."Week ID:".'</label>';
  16.     echo'<SELECT name="weekid" id="weekid">';
  17.     echo '<OPTION VALUE="">'."Select".'</OPTION>';
  18.     while($row = oci_fetch_array($editWR,OCI_ASSOC))
  19.     {
  20.     $WID=$row ['WEEK_ID'];
  21.     echo'<OPTION VALUE='."$WID".'>'.$WID.'</OPTION>';
  22.     }
  23.     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.
Jul 8 '10 #7
6,050 Expert 4TB
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.

Jul 8 '10 #8
first thing first...i am newbie
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 ....concatenation 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
for pointing out my mistakes.This saved a lot of my time.
Jul 8 '10 #9
line 21 corrected : '$WID'->"$WID".
Jul 9 '10 #10
1,168 Expert 1GB
What Markus means by "concatination is not needed" is instead of:
Expand|Select|Wrap|Line Numbers
  1. echo '<table border=1>'.'<tr>'.'<td>'.'<div>';
  2. // USE
  3. echo '<table border=1><tr><td><div>';
Line 21 should be:
Expand|Select|Wrap|Line Numbers
  1. echo '<OPTION VALUE=\''.$WID.'\'>'.$WID.'</OPTION>';
  2. // OR
  3. 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:
Expand|Select|Wrap|Line Numbers
  1. <option value='myVal'>myVal</option>
  2. // NOT
  3. <option value=myVal>myVal</option>
So you need to escape some strings using '\'.

That's all for now.
Jul 9 '10 #11
Thank you so much it really very help me once again thank you so much
Oct 27 '14 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by hemanth.singamsetty | last post: by
4 posts views Thread by TycoonUK | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.