[SOLVED] updating/modify checkboxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rave
Forum Newbie
Posts: 7
Joined: Thu Jul 10, 2003 6:32 am

[SOLVED] updating/modify checkboxes

Post by rave »

I have two tables.
One with user selected checkbox result.
The other with the entire category.
I need an update page so users can change their checkbox selected.

I have been having a hard time trying to display my user selected checkboxes from my all categories.

My tables

Code: Select all

CREATE TABLE `tbl_subjectcat` (
  `SubjectCatID` int(11) NOT NULL auto_increment,
  `Subject` varchar(25) NOT NULL default '',
  `Leveltype` varchar(10) NOT NULL default '',
  PRIMARY KEY  (`SubjectCatID`)
) TYPE=MyISAM COMMENT='store subjects for pri, sec JC and others modified ver 1.1' AUTO_INCREMENT=62 ;

---data for table `tbl_subjectcat`
INSERT INTO `tbl_subjectcat` VALUES (1, 'English', 'LP');
INSERT INTO `tbl_subjectcat` VALUES (2, 'Math', 'LP');
INSERT INTO `tbl_subjectcat` VALUES (3, 'Science', 'LP');
INSERT INTO `tbl_subjectcat` VALUES (4, 'Chinese', 'LP');
INSERT INTO `tbl_subjectcat` VALUES (5, 'Malay', 'LP');
INSERT INTO `tbl_subjectcat` VALUES (6, 'Tamil', 'LP');

----------------

CREATE TABLE `tbl_tsubjectlevel` (
  `TSubjectLevelID` int(11) NOT NULL auto_increment,
  `SubjectCatID` int(11) NOT NULL default '0',
  `LevelType` varchar(11) NOT NULL default '',
  `TutorID` int(11) NOT NULL default '0',
  PRIMARY KEY  (`TSubjectLevelID`),
  KEY `TutorID` (`TutorID`)
) TYPE=MyISAM AUTO_INCREMENT=4697 ;

INSERT INTO `tbl_tsubjectlevel` VALUES (2338, 1, 'LP', 235);
INSERT INTO `tbl_tsubjectlevel` VALUES (2339, 2, 'LP', 235);
INSERT INTO `tbl_tsubjectlevel` VALUES (2340, 3, 'LP', 235);
INSERT INTO `tbl_tsubjectlevel` VALUES (2341, 5, 'LP', 235);
My attempt has been nested if , nested while, nested foreach but results not what I need

Code: Select all

$result = mysql_query("SELECT SubjectCatID, Subject, LevelType FROM tbl_subjectcat WHERE	LevelType="LP" ");
				
 
   $result_LP_selected= mysql_query("SELECT SubjectCatID, LevelType FROM tbl_tsubjectlevel WHERE 	LevelType="LP" AND TutorID=" .$TutorID." ");
			
		  	       
	/*	
		while($myrowSubject=mysql_fetch_row($result))
		{
		
		while($myrowT_Subject=mysql_fetch_row ($result_LP_selected)) 
		 {

		 if ($myrowT_Subject[0]==$myrowSubject[0])
		 	{
			printf(" <input type="checkbox" value="$myrowSubject[0]" name="$myrowSubject[2][]" checked>$myrowSubject[1]<br>");
			
			 break;
				
		}else{
	if ($myrowT_Subject[0]!=$myrowSubject[0])
		{
			printf(" <input type="checkbox" value="$myrowSubject[0]" name="$myrowSubject[2][]">$myrowSubject[1]<br>");
					}
		
		}//end while
		}*/
Any help appreciated!


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

you can show the checkbox selected by simply adding CHECKED attribute to them like this

Code: Select all

<inpu type="checkbox" value="Maths" name="Something" CHECKED>
simply alter ur printf to add CHECKEDD where the condition is true in DB. as simple as that.

:P:P:P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

^^ *cough* checked="checked"
rave
Forum Newbie
Posts: 7
Joined: Thu Jul 10, 2003 6:32 am

Post by rave »

Thks! Its not the syntax 'checked' . I already have that down. It is the logic. The two tables must be compared and build all checkboxes including user selected ones. So users can change selection for update. I seem to not be able to walk through the inner loop array correctly. eg. 1,2,3,4,5,6 is in tbl_subjectcat. 1,2,3,5 is in tbl_subjectcat which user selected. The seems to to missing the user selected 5. As it compares it with 4 in the tbl_subjectcat. I need 1,2,3,5 checked and 4,6 unchecked.
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

May be u should consider this

Step 1> declare an array of 6 elem. Iterate over the 2nd table. store 1 in array where loop equals col[2] of 2nd table

Step 2> next iterate over the 1st table normally as u have done but now check if the curr. pos in the array stores 1.
if yes u can show it checked.

If u are not able to get me, say so and i will post the correct code 4 ya
;-)
User avatar
carlmcdade
Forum Newbie
Posts: 24
Joined: Thu Dec 02, 2004 2:19 am
Location: sweden

Post by carlmcdade »

I will give you a hint on this as I just last night had a similar thing happen. First I had to dump the array of checked boxes to see that using $_GET, $_POST and foreach() was changing the array keys selected by the user. 1,3,7,8 became 1,2,3,4 on the other end after $_POST. This was because I was using an empty array var[] ($myrowSubject[0]) which was loading the array okay but because of the way checkboxes work (only those checked are sent) $_POST was filling in the keys automatically to 1,2,3,4.

The solution was to explicitly set the array keys so they did not change on sending:

$i = 0;
$myrowSubject[$i];
$i++
rave
Forum Newbie
Posts: 7
Joined: Thu Jul 10, 2003 6:32 am

Post by rave »

Thks for the replies! Carlmcdade, sorry already have the table with user input . If I was starting from scratch your solution would be great.

n00b Saibot, I think I see where you are going with this. I think it is what I need to resolve this issue. Could you post a simply example for the benefit of my clarity as well as for those who who may need something like this resolved.

Thks again! A Very Happy Binary New Year to ALL !
010101011110110111011011101010111101010111
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

Ok! here it is...

Code: Select all

$result = mysql_query("SELECT SubjectCatID, Subject, LevelType FROM tbl_subjectcat WHERE LevelType="LP" ");
$result_LP_selected= mysql_query("SELECT SubjectCatID FROM tbl_tsubjectlevel WHERE LevelType="LP" AND TutorID=" .$TutorID." ");
$check_arr =array(0,0,0,0,0,0);
while($myrowT_Subject=mysql_fetch_row ($result_LP_selected)) 
{
$check_arr[$myrowT_Subject[0]] = 1 //this will store 1 in right place. no tension of any iterator or skipping
}
while($myrowSubject=mysql_fetch_row($result))
{
if ($check_arr[$myrowT_Subject[0]]==1)
{
printf(" <input type="checkbox" value="$myrowSubject[0]" name="$myrowSubject[2][]" checked>$myrowSubject[1]<br>");
}
else
{
printf(" <input type="checkbox" value="$myrowSubject[0]" name="$myrowSubject[2][]">$myrowSubject[1]<br>");
}

}//end while
as simple as that (as adapted from ur code, he he he ;-))
tell me how that works since i haven't tried it out...
rave
Forum Newbie
Posts: 7
Joined: Thu Jul 10, 2003 6:32 am

Post by rave »

n00b Saibot ,
U the MAN! Your kick ass solution worked great! I had so many hours of grief over it. Thanks for the relief! No skipping now.
Post Reply