954,598 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to create right amount of check boxes based on data held in database

How could i create x amount of checkboxes based on how many items are in a database and populate each check box with a php variable containing the data from the database.

thanks.

gunnerone
Light Poster
40 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

I have had a go and come up with the code below:

$sql1 = mysql_query("SELECT equipmentid, description FROM equipment") or die(mysql_error());
$row = mysql_fetch_assoc($sql1);

<?php for($i = "0"; i < strlen($row); $i++){ echo'<input type="checkbox" name="equipment" value="$row"/>';}?>


But i cant seem to get it to work.

gunnerone
Light Poster
40 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

The for loop is incorrect but you are better off using a while loop as much easier for what you require:

$sql1 = mysql_query("SELECT equipmentid, description FROM equipment") or die(mysql_error());
while ($row = mysql_fetch_assoc($sql1)) {
echo '<input type="checkbox" name="equipment" value="$row['equipmentid']"/>
}
simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: