Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now