943,975 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 7253
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 19th, 2008
0

populating drop down menu from MySQL

Expand Post »
hi guys.

i hav this drop down menu, which i need to populate with data from one column of a particular table of MySQL dbase.

i hav read a lot on this, but have not been able to come up wid any code.

any and all help in this regard will be greatly appreciated.

thx a ton.
Reputation Points: 10
Solved Threads: 0
Light Poster
akshit is offline Offline
46 posts
since Jun 2008
Jul 19th, 2008
0

Re: populating drop down menu from MySQL

Use this code in your application:
php Syntax (Toggle Plain Text)
  1. <?
  2. $query="SELECT sub_cat_id, sub_cat_name FROM category";
  3.  
  4. /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
  5.  
  6. $result = mysql_query ($query);
  7. echo "<select name=category value=''></option>";
  8. // printing the list box select command
  9.  
  10. while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  11. echo "<option value=$nt[sub_cat_id]>$nt[sub_cat_name]</option>";
  12. /* Option values are added by looping through the array */
  13. }
  14. echo "</select>";// Closing of list box
  15. ?>
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Jul 19th, 2008
0

Re: populating drop down menu from MySQL

I you want to display the second dropdown from first dropdown,then see this thread:
http://www.daniweb.com/forums/thread77281.html
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Oct 25th, 2009
0

Could I ask for help please

Hi to all
I have a drop down list that is populated from my mysql database, and works fine.
How to eliminate duplicate entries. I have seen the answer a 100 times but cant get it to work.
Here is the snip of code that I have
php Syntax (Toggle Plain Text)
  1. $result = mysql_query ($query);
  2. echo "<select name=Location value=''>Location</option>";
  3. // printing the list box select command
  4.  
  5. while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
  6. echo "<option value=$nt[Town]>$nt[Location]</option>";
  7. /* Option values are added by looping through the array */
  8. }
  9. echo "</select>";// Closing of list box
  10.  
  11. mysql_free_result($result) ;
Last edited by peter_budo; Oct 25th, 2009 at 7:05 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Oct 26th, 2009
0
Re: populating drop down menu from MySQL
0. $query="SELECT DISTINCT * FROM Garant GROUP BY Location";

Thanks for all that helped and apologies that I upset the moderators
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Nov 22nd, 2011
-1
Re: populating drop down menu from MySQL
Hello,
I have the drop down box as follows:
<label for="country">Country</label>
<select name="countryID"><?php echo $HTML['country_options_escape'];?></select>

how can I display data from mysql, I tried the following but did not work:
<label for="country">Country</label>
<select name="countryID"><?php $query="select country from countries where active ='YES'";
$result = mysql_query($query, $GLOBALS['DB']);

while($nt=mysql_fetch_array($result)){ //Array or records stored in $nt

$HTML['country_options_escape']=$nt[country];

//<option value=$nt[country]></option>;

}

echo $HTML['country_options_escape'];?></select>

Thanks
rse
Reputation Points: 10
Solved Threads: 0
Light Poster
rse is offline Offline
28 posts
since Nov 2011
Nov 23rd, 2011
0
Re: populating drop down menu from MySQL
@rse

fix this first, and then repost your codes. Unless, you define it somewhere it would make sense, but right now, I don't see it making any sense at all. Can you please elaborate or shades some light on where this line of codes coming from??

PHP Syntax (Toggle Plain Text)
  1. $HTML['country_options_escape']=$nt[country];

The only time I remember writing such arrangement was for smarty array, but not like yours.

this is valid
//while begins here
$something['it_is'] = (string) $so['this_is'];
$something['am_i'] = (string) $so['am_i'];
$assignItHere[] = $something;

//end of while loop

return $assignItHere;
Reputation Points: 39
Solved Threads: 10
Junior Poster in Training
veedeoo is offline Offline
88 posts
since Oct 2011
Nov 23rd, 2011
0
Re: populating drop down menu from MySQL
@ rse,

Try using this simple example. Maybe, it will help you locate the problems on your codes.

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4. ## we can do it the simpler way, and if it is working properly, we can add more into
  5. ## it to make it fancier. For now it is always nice to start in simpler form.
  6.  
  7. ## define your database connection credentials
  8. mysql_connect( $db_host, $db_user, $db_password ) or die(mysql_error());
  9. mysql_select_db( $db_database ) or die(mysql_error());
  10.  
  11. ## make sure that 'YES' can be found as shown on in the country rows under active column
  12. $countries = mysql_query("SELECT * country FROM countries WHERE active='YES'")
  13. or die(mysql_error());
  14.  
  15. ## lets loop the crap out of the toid
  16. while($country = mysql_fetch_array( $countries ))
  17. {
  18.  
  19. ## I am assuming here that your countries table has the following columns
  20. ## id | name | active
  21. ## 100 | Bundagle | YES
  22. ## 200 | Somewhere | YES
  23. $country_id = $country['id'];
  24. $country_name = $country['name'];
  25. ## Now, let's assign the country inside the option form tags.
  26. ## the country named bundagle and somewhere should be now inside the option tags.
  27. $country_block .= '<OPTION value="'.$country_id.'">'.$country_name.'</OPTION>';
  28.  
  29. } ## end while loop
  30.  
  31. ## now lets show the result from the table countries.
  32. ?>
  33. <!-- notice the country_block is outside the crap, of which will give you more freedom to modify the html tags without so much clutter-->
  34.  
  35. <label for="country">Country</label>
  36. <select name="countryID"><?php echo $country_block; ?></select>
Last edited by veedeoo; Nov 23rd, 2011 at 3:57 am.
Reputation Points: 39
Solved Threads: 10
Junior Poster in Training
veedeoo is offline Offline
88 posts
since Oct 2011
Nov 23rd, 2011
0
Re: populating drop down menu from MySQL
wonderful thanks
rse
Reputation Points: 10
Solved Threads: 0
Light Poster
rse is offline Offline
28 posts
since Nov 2011
13 Days Ago
0
Re: populating drop down menu from MySQL
Ok guys I need some help... I am having a similar issue let me tell you a little bit of what I am wanting to do.

I have a MySQL database with my data in it that I want on my web page.

What I need is a table with all of the data that is in that database listed and above it I want drop downs people can select from to filter the table to display just what they are looking for. With each drop down options coming from my data base and depending on what the first drop down had selected...

What I have so far on the MAIN page is this.
PHP Syntax (Toggle Plain Text)
  1. <?
  2.  
  3. $sql="SELECT id, bar_name FROM barlist";
  4. $result=mysql_query($sql);
  5.  
  6. $options="";
  7.  
  8. while ($row=mysql_fetch_array($result)) {
  9.  
  10. $id=$row["id"];
  11. $thing=$row["bar_name"];
  12. $options.="<OPTION VALUE=\"$id\">$bar_name</option>";
  13. }
  14. ?>
  15.  
  16. <SELECT NAME=id>
  17. <OPTION VALUE=0>Choose
  18. <? echo $options?>
  19. </SELECT>

and on the data base connection page I have this
PHP Syntax (Toggle Plain Text)
  1. <head>
  2. <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
  3. <title>Untitled 1</title>
  4. </head>
  5.  
  6. <body>
  7. <?php
  8. $q=$_GET["q"];
  9.  
  10. $con = mysql_connect('DATABASE', 'USER', 'PASSWORD');
  11. if (!$con)
  12. {
  13. die('Could not connect: ' . mysql_error());
  14. }
  15.  
  16. mysql_select_db("DATABASE", $con);
  17.  
  18. $sql="SELECT * FROM bar WHERE id = '".$q."'";
  19.  
  20. $result = mysql_query($sql);
  21.  
  22. echo "<table border='1'>
  23. <tr>
  24. <th>Bar Name</th>
  25. <th>Address</th>
  26. <th>Time they open</th>
  27. <th>Time they close</th>
  28. </tr>";
  29.  
  30. while($row = mysql_fetch_array($result))
  31. {
  32. echo "<tr>";
  33. echo "<td>" . $row['bar_name'] . "</td>";
  34. echo "<td>" . $row['address'] . "</td>";
  35. echo "<td>" . $row['bar_open'] . "</td>";
  36. echo "<td>" . $row['bar_close'] . "</td>";
  37. echo "</tr>";
  38. }
  39. echo "</table>";
  40.  
  41. mysql_close($con);
  42. ?>
  43. </body>
  44.  
  45. </html>

The above code gives me the following errors on the main page

Warning: mysql_query() [function.mysql-query]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/40/8248640/html/murfreesboro.php on line 96

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/content/40/8248640/html/murfreesboro.php on line 96

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/40/8248640/html/murfreesboro.php on line 100


I know a lot of reading I am sorry if you guys could help me out I would greatly appreciate it!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jgilliam is offline Offline
2 posts
since Feb 2012
Message:
Previous Thread in PHP Forum Timeline: Class and Function Confusion
Next Thread in PHP Forum Timeline: php rearange issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC