Sorting Multiple Memberships

8 posts by 2 authors in: Forums > CMS Builder
Last Post: August 23, 2011   (RSS)

I have a section editor which is used to manage which groups members belong to. One issue is that people can be members of more than one group, which makes it a bit difficult to display properly.

I would like it to look like this: http://aircadetleague.ns.ca/directory.htm
Unfortunately, it looks like this: http://ns.aircadetleagueofcanada.ca/membership.php

Here is the code:
<table width="920" style="font-size:.7em;">
<?php $old_group = ''; ?>
<?php foreach ($directorsRecords as $record): ?>
<?php $group = $record['member']; ?>
<tr align="left">
<th colspan="5">
<?php if ($group != $old_group) {
echo "<h3>$group</h3>";
} ?>
</th>
<th><?php if ($group != $old_group) {
echo "<h3>Home</h3>";
} ?></th>
<th><?php if ($group != $old_group) {
echo "<h3>Work</h3>";
} ?></th>
<th><?php if ($group != $old_group) {
echo "<h3>Fax</h3>";
} ?></th>
<th><?php if ($group != $old_group) {
echo "<h3>Mobile</h3>";
} ?></th>
</tr>
<tr>
<td>
<a href="mailto:<?php echo $record['email_address'] ?>"><?php echo $record['first_last_name'] ?></a>
</td>
<td>
<?php echo $record['position'] ?>
</td>
<td>
<?php echo $record['address'] ?>
</td>
<td>
<?php echo $record['city'] ?>
</td>
<td>
<?php echo $record['postal_code'] ?>
</td>
<td>
<?php echo $record['home_phone'] ?>
</td>
<td>
<?php echo $record['work_phone'] ?>
</td>
<td>
<?php echo $record['fax'] ?>
</td>
<td>
<?php echo $record['mobile'] ?>
</td>
</tr>
<?php $old_group = $group; ?>
<?php endforeach ?>
</table>

--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [northernpenguin] Sorting Multiple Memberships

By Jason - August 22, 2011

Hi,

So if I understand the problem correctly, the issue is that even if a person is a member of more than 1 group, they are only being displayed once, where they should be displayed multiple times. Is that right?

If so, how are these groups stored for each member? In your code, you seem to be using the field "member" to store group. The way it's being used, though, looks like it's just a text field.

One approach would be to first get a list of all available groups. Then, as you output each group, use a query to get all the members associated with that group. So you'll have 1 foreach loop inside another.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Sorting Multiple Memberships

Jason: "members" is already a list (checkbox). Each record includes the groups the person is a member of, which is one of the reasons the code prints out the way it does.

I will try the foreach loop idea.

Thanx

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [Jason] Sorting Multiple Memberships

Ok, maybe I'm missing something. I tried your suggestion, but don't seem to be getting anywhere.

The major issue is that some people are in more than one group, so I want to be able to sort them by group. Like this:

Group1
John Doe
Peter Doe

Group 2
John Doe
Jane Doe

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke

Re: [northernpenguin] Sorting Multiple Memberships

By Jason - August 23, 2011

Hi Ragi,

How/where are you storing your list of groups? How are you storing the members of those groups (ie, or a given member, how do you know which group(s) they belong to)?

Let me know along with the current version of your .php file. I can take a look and see if I can make any suggestions.

Thanks
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Sorting Multiple Memberships

Jason: All the people records are stored in a multi-record table. The groups are in stored as a list (checkbox) in the same table.

When a new record is created, the user is assigned membership in one or more groups from the list.

Attached is my current file.

Thanx!

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke
Attachments:

membership.php 12K

Re: [Jason] Sorting Multiple Memberships

Jason: Worked well! I did have to make some minor corrections. Below is the ne code:
<?php
// get array of groups
$groups = getListOptions('directors', 'member');

// gather members by group
$groupToMembers = array();

foreach ($groups as $value => $label) {
$groupToMembers[$label] = array();

foreach ($directorsRecords as $director) {
if (in_array($value, $director['member:values'])) {
$groupToMembers[$label][] = $director;
}
}

}
?>
<table width="920" style="font-size:.7em;">
<?php foreach ($groupToMembers as $group => $member): ?>

<tr align="left">
<th colspan="5"><h3><?php echo $group; ?></h3></th>
<th><h3>Home</h3></th>
<th><h3>Work</h3></th>
<th><h3>Fax</h3></th>
<th><h3>Mobile</h3></th>
</tr>

<?php foreach ($member as $members): ?>
<tr>
<td>
<a href="mailto:<?php echo $members['email_address'] ?>"><?php echo $members['first_last_name'] ?></a>
</td>
<td>
<?php echo $members['position'] ?>
</td>
<td>
<?php echo $members['address'] ?>
</td>
<td>
<?php echo $members['city'] ?>
</td>
<td>
<?php echo $members['postal_code'] ?>
</td>
<td>
<?php echo $members['home_phone'] ?>
</td>
<td>
<?php echo $members['work_phone'] ?>
</td>
<td>
<?php echo $members['fax'] ?>
</td>
<td>
<?php echo $members['mobile'] ?>
</td>
</tr>
<?php endforeach ?>

<?php endforeach ?>
</table>


Thank you!

Ragi
--
northernpenguin
Northern Penguin Technologies

"Any sufficiently advanced technology
is indistinguishable from magic."
........Arthur C. Clarke