the_mossman Posted September 7, 2009 Share Posted September 7, 2009 I have a db that stores id and size for checked checkboxes. (Only checked checkboxes i stored in this db) I want to generate checkboxes until $i<(a specified number). If the value of the checkbox that is about to be created is already stored in the db, then make the checkbox that is created CHECKED. IN SHORT: Create the checkboxes with the value that are not stored in the DB unchecked. If the checkbox value is stored in the DB, create the checkbox checked. $query = "SELECT * FROM db WHERE fileid=$fileid"; $result = mysql_query($query,$db_link); $rows = mysql_num_rows($result); $a = 4.5; //Value of the first checkbox generated echo "<form method =\"post\" action=\"sizes.php\">"; for ($i=0; $i<20; $i++){ $a = $a + 0.5; //size is increased by 0.5 for each checkbox for($x=0; $x<$rows; $x++) { $size = mysql_result($result,$x,"size"); //size is retreived from db where fileid=$fileid if ($a == $size) { //Here I want to say: if there is a value in db, display teh box as checked echo "<input name=\"sizes[]\" type=\"checkbox\" value=\"$a\" checked=\"checked\" />$a "; $a = $a + 0.5; } } echo "<input name=\"sizes[]\" type=\"checkbox\" value=\"$a\" />$a "; //every box that is not checked } echo "<input type=\"hidden\" name=\"fileid\" value=\"$fileid\">"; echo "<input type=\"submit\" name=\"submit\" value=\"Ok\" style=\"width: echo "</form>"; $submit = $_POST['submit']; if ($submit == "Ok"){ $mySizes = $_POST['sizes']; $fileid = $_POST['fileid']; $query = "DELETE FROM greppet_sizes WHERE fileid='$fileid'"; mysql_query($query,$db_link); if ($mySizes != null){ foreach ($mySizes as $value){ $query = "INSERT INTO greppet_sizes VALUES (null,$fileid,'$value')"; mysql_query($query,$db_link); } } } 100px;\" /> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.