Jump to content

stuck - list checkboxes, only some ticked


imafish

Recommended Posts

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 [i]isn't[/i] in.

Thanks
Link to comment
Share on other sites

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 [b]supermarket, bottleshop, petrolstation, firehouse[/b] should be ticked.
Link to comment
Share on other sites

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.

[code]//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>";
}[/code]
Link to comment
Share on other sites

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.
 
Link to comment
Share on other sites

Sorry

The tables are

[b]groupsuser[/b]
groupid | userid
    9    |    60
    5    |    60
    3    |    60

[b]groups[/b]
groupid | name
    1    |  firestation
    2    |  police
    3    |  supermarket

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

[code]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']);
}[/code]

Basically I want it so that each item from $row['name'] goes into the array.
Link to comment
Share on other sites

Try:
[code]<?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'];
}?>[/code]
Initialize the array first, then add each name into it.

Ken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.