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.

Recommended Answers

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']"/>
}
Jump to Post

All 2 Replies

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.

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']"/>
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.