Jump to content


Photo

stuck - list checkboxes, only some ticked


  • Please log in to reply
12 replies to this topic

#1 imafish

imafish
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 04:33 AM

Hi
I've got two tables, one is the name of the group the other is the memberships the user is in.

I want a all the groups to come up, but the groups the user is in to be ticked.

I can make the list of groups the user is in come up, but not the rest of the groups the user isn't in.

Thanks

#2 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 04:35 AM

im not sure... can you give me an example of a user, the groups hes in, and the groups hes NOT in.
and ill give it a shot.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 04:36 AM

Can you post any code you've already written and the script for the form? Without these we would be grasping at straws for solutions.

Ken

#4 imafish

imafish
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 04:38 AM

Ok.
User 60 is in groups: supermarket, bottleshop, petrolstation, firehouse

IS NOT in the rest;
garage, garden, busstop, airport, boat, policehouse

they should all come up in the list, but only supermarket, bottleshop, petrolstation, firehouse should be ticked.

#5 imafish

imafish
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 04:41 AM

Here is what I have got, that selects the groups (from the group table) that the user is in. All I need to add is the OTHER group names, that are not selected.

//Query the table to see if the user is part of any groups
			$query_groupsmember = "SELECT u.id, um.id, u.name, u.description, um.userid, um.groupid FROM usersgroup u, usersmembership um WHERE userid=$userid AND um.groupid = u.id;";
			$result_groupsmember = mysql_query($query_groupsmember) or die ('Query failed: ' . mysql_error());

			while($row = mysql_fetch_array($result_groupsmember, MYSQL_ASSOC)){
				//Displays and marks the existing groups
				echo "<tr><td>";
				$groupname = $row['name'];
				$groupdesc = $row['description'];
				$groupid = $row['id'];
				$group_group = $row['groupid'];

				echo "<input type=\"checkbox\" checked=\"yes\" id=\"$groupid\" value=\"$groupname\" title=\"$groupdesc\">$groupname<br>";
				echo "<td></tr>";
			}


#6 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 04:49 AM

its getting late and im having a hard time thinking here...
what table shows the list of groups the user is in and how is that formatted

the way i would have it
Table: groups

userid | group1 id | group2 id | ... |
----------------------------------
101  |    1        |      0      |  0 |


basically shows the userid and if the user is in the group marks it with a 1, if its a 0 user is not in the group.
then you just make a for loop with an if else statement.
 


#7 imafish

imafish
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 04:52 AM

Sorry

The tables are

groupsuser
groupid | userid
    9    |    60
    5    |    60
    3    |    60

groups
groupid | name
    1    |  firestation
    2    |  police
    3    |  supermarket

So groupuser links to groups, so a user can be in many groups.



#8 Branden Wagner

Branden Wagner
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts

Posted 23 July 2006 - 05:02 AM

well im brain dead for the night, thats a pretty difficult question.. ill look back at it tomorrow when im refreshed and see if i can figure it out for you.
my preference would be to rewrite the groups table like i mentioned previously.

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 05:16 AM

What you probably want to do is create an array that holds all of the possible groups. Then in the while loop, collect all the groups the user is a member of in another array. After that array is created, a use a foreach loop on the first array, checking to see if each group is in the user's array, if it is, check it.

Ken

#10 imafish

imafish
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 05:19 AM

Thanks for that;
I've just started it!

Except I get only the last member of the array.

Ive created an array by going
$a = array($groupnamelist);

that correct?


#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 05:25 AM

Please post the code you're working with, posting one line out of context is not useful.

Ken

#12 imafish

imafish
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 23 July 2006 - 05:26 AM

while($row = mysql_fetch_array($result_groupsmember, MYSQL_ASSOC)){
				//Get all groups user is part of and store in an array
				//echo $row['name'];
				$a = array($row['name']);
			}

Basically I want it so that each item from $row['name'] goes into the array.

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 23 July 2006 - 05:29 AM

Try:
<?php
$a = array()
while($row = mysql_fetch_array($result_groupsmember, MYSQL_ASSOC)){
				//Get all groups user is part of and store in an array
				//echo $row['name'];
				$a[] = $row['name'];
			}?>
Initialize the array first, then add each name into it.

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users