Dynamic checkboxes/labels from Db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Dynamic checkboxes/labels from Db

Post by skyrise85 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm having trouble figuring out how to make a form that will create a dynamic number of checkboxes.  I want to pull all records from a table and dynamically create a corresponding number of checkboxes on the form page.  This will enable the user to add additional records to the table without ever manually coding another checkbox.  Here's what I have (this code is within a form and <td> tag:

Code: Select all

<?
mysql_connect($hostname,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM genre";
$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$desc=mysql_result($result,$i,"desc");
?>
<input type="checkbox" name="cBox[]" value="1"/><? echo $desc; ?> <br> 
<? 
$i++; 
} 
?>
I have three records in the table, and all this code outputs is a single checkbox w/o any label(s).

PLEASE HELP! :(


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Care to read the posting guidelines?
Post Reply