Solved

Getting check box values through query string and saving them into database.

Posted on 2008-10-13
1
652 Views
Last Modified: 2013-12-13
I am looking ofr some code revision please, I am trying to save/update data from some check boxes ( based on groups and sub groups. Everything is working perfect just have one little glitch that I can't figure out. when I chance selection (EDIT check boxes) how do I minipulate the change and save it. Origionally how I did was, I deleted everything with the Contact ID and re-saved fresh data but that messes things up.

Question would be how do I clear check boxes data when user checks un-checks data. The old data is saved in database. ( i have three tables. groups, sub-groups and a look up table group-subgrups with contact ids)
Any ideas/suggesions?

I have attached the screenshot just to get an idea how groups/sub-groups are being displayed.

I have attached the code for save ( that works perfectly )
and update ( that's where I have some issues )
 Thanks.
Save groups/subgroups
--------------------
//pass to record set
$rs = mysql_query($sql, $cn);
while($row = mysql_fetch_array($rs))
{
print "<p>";
$group_id= $row["group_id"] ;
	
//name='maingroup[]' value="new_group_is_here___3"
//the part before the "___" is the actual group name;
//the part after the "___" is the group id
$group_name = $row["group_name"];
$group_value = preg_replace('/\s/','_',$group_name) . "___" . $group_id;
//$group_name_array[] = $group_name;
 
// if had check box for the Main Groups
Print " <input type='hidden' name='maingroup[]' value='" . $group_value ."'/>" . "  " . "<strong>". $group_name . "</strong>: "."</ br>";
	
//sub group part - start
$sql2 = "Select * from tbl_sub_groups where group_id = '$group_id'";
 
//pass to record set
$rs2 = mysql_query($sql2, $cn);
 
// check the number of records and print the main group name or skip it if not children.
	while($row2 = mysql_fetch_array($rs2))
	{
	$id= $row2["sub_group_id"] ;
	$sgroup_name = $row2["sgroup_name"];
 
	print "</ br>";
	Print " <input type='checkbox' name='{$group_value}[]' 
               value='{$id}'/>" . "  " . $sgroup_name . "</ br>";
	}
	// sub group part ends.
 	print "</p>";                                   
}
 
	
$maingroup=$_REQUEST['maingroup'];
foreach($maingroup as $v){
 
	list($group_name,$group_id)=explode("___",$v);
	$group_id = preg_replace('/_/',' ', $group_id);
 
	$subgroup=$_REQUEST[$v];
 
	if($subgroup){
		foreach($subgroup as $v){
		
	   	 $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '$v')";
	   	 $rs3 = mysql_query($sql3, $cn);
		 }	
		}
		else{
			    $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '')";
				$rs3 = mysql_query($sql3, $cn);
		}								 
}
 
	
$maingroup=$_REQUEST['maingroup'];
foreach($maingroup as $v){
 
	list($group_name,$group_id)=explode("___",$v);
	$group_id = preg_replace('/_/',' ', $group_id);
 
	$subgroup=$_REQUEST[$v];
 
	if($subgroup){
		foreach($subgroup as $v){
		
	   	 $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '$v')";
	   	 $rs3 = mysql_query($sql3, $cn);
		 }	
		}
		else{
			    $sql3 = "INSERT INTO `tbl_people_groups` (`people_id`, `group_id`, `sub_group_id`) 
                                         VALUES ('$people_id', '$group_id', '')";
				$rs3 = mysql_query($sql3, $cn);
		}								 
}
 
 
Edit:
---------------
//pass to record set
$rs = mysql_query($sql, $cn);
while($row = mysql_fetch_array($rs))
{
	print "<p>";
  
	$group_id= $row["group_id"] ;
	
	//name='maingroup[]' value="new_group_is_here___3"
	//the part before the "___" is the actual group name;
	//the part after the "___" is the group id
	$group_name = $row["group_name"];
	$group_value = preg_replace('/\s/','_',$group_name) . "___" . $group_id;
	//$group_name_array[] = $group_name;
	
    if (in_array(trim($group_id), $selected_groups))
{
   $checkedString = "checked=\"checked\"";
}
else
{
   $checkedString = "";
}
 
 
print "<input type='hidden' name='maingroup[]' value='" . $group_value ."' $checkedString />" . "  " . "<strong>". $group_name . "</strong>: "."</ br>";
 
	//sub group part - start
	$sql2 = "Select * from tbl_sub_groups where group_id = '$group_id'";
 
	//pass to record set
	$rs2 = mysql_query($sql2, $cn);
	while($row2 = mysql_fetch_array($rs2))
	{
		$sgroup_id= $row2["sub_group_id"] ;
		$sgroup_name = $row2["sgroup_name"];
		$group_id = $row2["group_id"];
		 
		$sql4 =  "Select * from tbl_people_groups where people_id = '$id' and group_id = '$group_id'";
        $rs4 = mysql_query($sql4, $cn);
	//echo $sql4;
	    while($row4 = mysql_fetch_array($rs4))
	    {
		 $selected_sub_groups[]= $row4["sub_group_id"];
	    }
 
	
	 	   if (in_array(trim($sgroup_id), $selected_sub_groups))
			{
			//echo "test 1";
 			  $checkedString2 = "checked=\"checked\"";		
			  print "</ br>";
		      Print " <input type='checkbox' name='{$group_value}[]' value='{$sgroup_id}' checked='$checkedString2'/>" . "  " . $sgroup_name . "</ br>";
			}
			else
			{ 
			//echo "test 2";
  			 $checkedString2 = "";
	
			  print "</ br>";
		      Print " <input type='checkbox' name='{$group_value}[]' value='{$sgroup_id}' />" . "  " . $sgroup_name . "</ br>";
			  
			}
 
	}
	// sub group part ends.
	print "</p>";                                   
}
 
Save:
-------
foreach($_REQUEST['maingroup'] as $grp){
// echo "Group:" . $grp . "<br />";
$pizza  = "Media___12";
$pieces = explode("___", $pizza);
// $pieces[0]. "<br />"; // piece1
$group = $pieces[1];
 
 $sql3 = "DELETE  from tbl_people_groups where people_id = '$id'";
  $rs = mysql_query($sql3,$cn);
  
 
 //explode("___", $grp)
  foreach( $_REQUEST[$grp] as $subgrp )
  {
  
 $sql2 = "insert into tbl_people_groups(people_id, group_id, sub_group_id) 
	                            values('$id','$group','$subgrp')";
		
  $rs = mysql_query($sql2,$cn);
		
// echo "Subgroup:" . $subgrp;
  }
}

Open in new window

0
Comment
Question by:martyje
1 Comment
 

Accepted Solution

by:
martyje earned 0 total points
ID: 22705457
I got it... ty guys...
Just added a counter to track the ids and delete and refresh the data whatever.

$count = 0;
      
foreach($_REQUEST['maingroup'] as $grp){
// echo "Group:" . $grp . "<br />";
$pizza  = "Media___12";
$pieces = explode("___", $pizza);
// $pieces[0]. "<br />"; // piece1
$group = $pieces[1];

// $sub = $_REQUEST[$grp];

      if ($count == 0){
     echo $sql3 = "DELETE  from tbl_people_groups where people_id = '$id'";
     $rs = mysql_query($sql3,$cn);
     $count = $count + 1;
     }
   //explode("___", $grp)
  foreach( $_REQUEST[$grp] as $subgrp )
  {
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question