Jump to content

Retreive/Store if checkbox i checked


the_mossman

Recommended Posts

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;\" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.