Jump to content

checkbox help


dennismonsewicz

Recommended Posts

I have this code:

 

<?php
if($chkbx) {
	foreach($chkbx as $key=>$val) 
	{
		$newval = $val==1?1:0;
	  $data_name .= $key . "=" . $newval . ",";								
	}

	$trimmedvar = trim($data_name, ",");

$qry = "UPDATE has_had_projects SET project = '$project_name', $trimmedvar WHERE project_id = '$id'";
echo $qry;
} ?>		

 

This works as far as updating the checkboxes that have been checked but what I can't get working is setting all of the other values to 0 if they are unchecked... any help here?

Link to comment
https://forums.phpfreaks.com/topic/126935-checkbox-help/
Share on other sites

the $chkbx var is set like this

 

$chkbx = $_POST['checkboxes'];

 

The code that is spitting out the checkboxes is this:

 

checkbox_qry = mysql_query("SELECT * FROM has_had_projects WHERE project = '" . $results->project . "'")or die(mysql_error());
$field = mysql_num_fields($checkbox_qry);
while($row = mysql_fetch_assoc($checkbox_qry)) {													
for($i = 2; $i < $field; $i++) {
$names = mysql_field_name($checkbox_qry, $i);	
$chk = $row[$names]==1?'checked="checked"':'';
$numbers = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);																	
$title .= '<div><input type="checkbox" name="checkboxes[' . $names . ']" class="checkbox" id="' . $names . '" ' . $chk . ' value="1"  /> <label for="checkboxes[' . $names . ']">' . ucwords(str_replace($numbers, '', $names)) . '</label></div>';
}
echo $title;
}

Link to comment
https://forums.phpfreaks.com/topic/126935-checkbox-help/#findComment-656574
Share on other sites

every if statement has to have an else

 

Actually you don't need an else to an if statement. Here is a quick thing i found searching php if statement in firefox http://www.tizag.com/phpT/if.php

 

you can have an if statement which has no else.

if(something to check)
{
something to be done
}

This will be done no matter what the if statement says

Link to comment
https://forums.phpfreaks.com/topic/126935-checkbox-help/#findComment-656617
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.