|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
## PLEASE NOTE: I am still learning php and mysql
so this may not be the most accurate or the best way to do this but it works. This code allows you to stick this into any form and allow you to populate a dropdown box with all categories and thier values. Since I also use "ORDER BY and DESC, the drop down contents are sorted by the category names alphabeticly. I am posting this simply because most if the tutorials that I found do not work if you have a seperate category table. Therefore, they won't work when you need to associate the category ID with the Category itself. The code below simply allows you to replace any existing or new select area to have it populated from the db. <SELECT> <? include("../includes/db.php"); MYSQL_CONNECT(HOST,USER,PASS) OR DIE("Unable to connect to database"); @mysql_select_db(DB) or die( "Unable to select database"); $query=("select * from categories order by category, category desc"); $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); while($row=mysql_fetch_array($result)){ echo "<OPTION VALUE=".$row['cid'].">".$row['category']."</OPTION>"; } ?> </SELECT> ################################### This is what the db.php looks like: ################################### <?php define("HOST", "localhost"); define("USER", "your_username"); define("PASS", "your_password"); define("DB", "your_mysql_db_name"); ?> ################################### Here is the table structure I have for my Categories: ################################### CREATE TABLE `categories` ( `cid` int(11) NOT NULL auto_increment, `category` varchar(55) NOT NULL default '', PRIMARY KEY (`cid`) ) TYPE=MyISAM AUTO_INCREMENT=15 ; ################################### Here is some example data if you want to try this out: ################################### INSERT INTO `categories` VALUES (1, 'Appraisers'); INSERT INTO `categories` VALUES (2, 'Auctions'); INSERT INTO `categories` VALUES (3, 'Education'); INSERT INTO `categories` VALUES (4, 'Sale By Owner'); INSERT INTO `categories` VALUES (5, 'Inspections'); INSERT INTO `categories` VALUES (6, 'Land'); INSERT INTO `categories` VALUES (7, 'Mortgages'); INSERT INTO `categories` VALUES (8, 'Commercial'); INSERT INTO `categories` VALUES (9, 'Referral'); INSERT INTO `categories` VALUES (10, 'Apartments'); INSERT INTO `categories` VALUES (11, 'Relocation'); INSERT INTO `categories` VALUES (12, 'Rentals'); INSERT INTO `categories` VALUES (13, 'Residential'); INSERT INTO `categories` VALUES (14, 'Agents'); |
|
#2
|
|||
|
|||
|
Using the example above, You can also populate check boxes the same way with a few modifications.
<? include("includes/db.php"); MYSQL_CONNECT(HOST,USER,PASS) OR DIE("Unable to connect to database"); @mysql_select_db(DB) or die( "Unable to select database"); //$query=("select * from categories"); $query=("select * from categories order by category, category desc"); $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); while($row=mysql_fetch_array($result)){ $category = @$row["category"]; $cid = @$row["cid"]; echo "<input type=\"checkbox\" name=\"$category\" value=\"$cid\"> $category:"; echo "<br>"; } ?> |
|
#3
|
|||
|
|||
|
And for this one using radio boxes, it appears to work although I'm not sure on this.
<? include("includes/db.php"); MYSQL_CONNECT(HOST,USER,PASS) OR DIE("Unable to connect to database"); @mysql_select_db(DB) or die( "Unable to select database"); //$query=("select * from categories"); $query=("select * from categories order by category, category desc"); $result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); while($row=mysql_fetch_array($result)){ $category = @$row["category"]; $cid = @$row["cid"]; echo " <input type=\"radio\" name=\"category[]\" value=\"$cid\"> $category:"; echo "<br>"; } ?> |
![]() |
| Viewing: Dev Articles Community Forums > Databases > MySQL Development > [Examples} Populating Dropboxes, Checkboxes and Radio Boxes using a MySQL Table. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|