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: [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: [northernpenguin] Sorting Multiple Memberships

By Jason - August 23, 2011

Hi Ragi,

Here is an approach we can try. We'll first get an array of all of the group options. Then create an array where the key is the name of the group and the elements are all the members inside that group.

We would create those arrays like this:

// load records
list($directorsRecords, $directorsMetaData) = getRecords(array(
'tableName' => 'directors',
'loadUploads' => '0',
));



// get array of groups
$groups = getListOptions('directors', 'members');

// 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;
}
}

}


We'll can then output this using 2 loops like this:

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

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

<?php endforeach ?>
</table>


Give that a try and let me know if you run into any issues.

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: 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