Page 1 of 1

Dynamic checkboxes/labels from Db

Posted: Sun Jan 21, 2007 2:07 pm
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!

Re: Dynamic checkboxes/labels from Db

Posted: Sun Jan 21, 2007 3:10 pm
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>";
}
?>

Posted: Sun Jan 21, 2007 9:24 pm
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']; }; ?>

Posted: Sun Jan 21, 2007 9:48 pm
by aaronhall
You may want to prepend an 'echo' to the beginning of the statement

Posted: Sun Jan 21, 2007 10:08 pm
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']
" ; } ?>

Posted: Mon Jan 22, 2007 4:09 am
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]

Posted: Tue Jan 23, 2007 2:00 pm
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

Posted: Tue Jan 23, 2007 2:44 pm
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?