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 »

I'm having trouble finding/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 <form> and <td> tags):

Code: Select all

<?php
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"/><?php echo $desc; ?> <br>
<?php
$i++;
}
?>
I have three records in the table, and all this code outputs is a single checkbox w/o any label(s).

PLEASE HELP!
Mohamed
Forum Newbie
Posts: 21
Joined: Fri Jan 19, 2007 6:59 pm
Location: Seattle

Re: Dynamic checkboxes/labels from Db

Post by Mohamed »

skyrise85 wrote:I'm having trouble finding/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 <form> and <td> tags):

Code: Select all

<?php
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"/><?php echo $desc; ?> <br>
<?php
$i++;
}
?>
I have three records in the table, and all this code outputs is a single checkbox w/o any label(s).

PLEASE HELP!
it looks like that mysql_numrows returns one row.
try this code, it may help you

Code: Select all

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

$query="SELECT * FROM genre";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
	"<input type="checkbox" name="cBox[]" value="1"/>{$row['desc']}<br>";
}
?>
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post by skyrise85 »

So I tried the following:

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);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
        "<input type=\"checkbox\" name=\"cBox[]\" value=\"1\"/>{$row['desc']}<br>";
 
?>
And this is what I got:

Code: Select all

" $row['desc']; }; ?>
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 »

You may want to prepend an 'echo' to the beginning of the statement
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post by skyrise85 »

I tried that. It didn't make a difference. I also tried this:

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");

echo "<input type=\"checkbox\" name=\"cBox[]\" value=\"1\"/>$row['desc']<br>" ; 

}
?>
and basically got the same thing:

Code: Select all

$row['desc']
" ; } ?>
shwetarani
Forum Newbie
Posts: 3
Joined: Fri Jan 19, 2007 6:11 am

Post by shwetarani »

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]


you should try this code:

Code: Select all

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

$query="SELECT * FROM genre"; 
$result=mysql_query($query); 
while ($row = mysql_fetch_array($result)){ 
       echo "<input type='checkbox' name='cBox[]' value='1'> .$row['desc']. <br>"; 
} 
?>

where desc = field name


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]
skyrise85
Forum Newbie
Posts: 15
Joined: Fri Jan 19, 2007 5:50 pm

Post by skyrise85 »

GOT IT TO WORK. THANKS FOR THE HELP EVERYONE. HERE'S WHAT I DID:

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);
//print_r($result);
//print_r($row);
$num=mysql_numrows($result);

$i=0; 

while ($i < $num ) { 
$row = mysql_fetch_assoc($result);
//$desc = mysql_result($result,$i,"desc");
echo "</blockquote><input type=\"checkbox\" name=\"cBox[]\" value=\"1\"/>" ; 
echo "$row[desc]<br>";

$i++;
}
mysql_close();
?>
Here's what the output kind of looks like:


[]Frog
[]Duck
[]Mask
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 »

Array keys need to be quoted though.. PHP is trying to find a constant by that name, not finding it, issuing a notice, and then finally looking for a key by that name in the array. You should concatenate the variable like so:

Code: Select all

echo "</blockquote><input type=\"checkbox\" name=\"cBox[]\" value=\"1\"/>" . $row['desc'] . "<br>"; 
Strange that you originally got that kind of output without an error or warning -- did you turn errors off?
Post Reply