Solved

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

Posted on 2008-10-13
1
650 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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 …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

773 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