MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 25th, 2004, 11:26 PM
cbunting cbunting is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 19 cbunting User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Lightbulb [Examples} Populating Dropboxes, Checkboxes and Radio Boxes using a MySQL Table.

## 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');

Reply With Quote
  #2  
Old January 25th, 2004, 11:27 PM
cbunting cbunting is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 19 cbunting User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>";
}
?>

Reply With Quote
  #3  
Old January 25th, 2004, 11:28 PM
cbunting cbunting is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 19 cbunting User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>";
}
?>

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > [Examples} Populating Dropboxes, Checkboxes and Radio Boxes using a MySQL Table.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway