Jump to content

Archived

This topic is now archived and is closed to further replies.

imafish

stuck - list checkboxes, only some ticked

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

Share this post


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

Share this post


Link to post
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.

Share this post


Link to post
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]

Share this post


Link to post
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.
 

Share this post


Link to post
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.

Share this post


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

Share this post


Link to post
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

Share this post


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

Share this post


Link to post
Share on other sites
Please post the code you're working with, posting one line out of context is not useful.

Ken

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.