By jeepfreak on
I'm developing my own groups module. I've got a mysql table with group members (with a group id column and a member id column), and a group info mysql table with all the information regarding each group. I want to output a table with the name, date created, and number of members for each group. I also want to be able to sort on each of the headings. I got everything working, but I don't like the way the member count works. The problem is that the number of members is updated AFTER it's sorted. Can anybody please suggest a better way?
Here's what I have:
function group_groups_home()
{
$sql = "SELECT group_info.gid, group_info.title, group_info.nid, group_info.created, group_info.members FROM (group_info)";
$header = array(
array('data' => t('Group Name'), 'field' => 'title', 'width' => '100%'),
array('data' => t('Created'), 'field' => 'created', 'align' => 'center'),
array('data' => t('Members'), 'field' => 'members', 'align' => 'center')
);
$rows = array();
$sql .= tablesort_sql($header);
$result = pager_query($sql, 20);
while ($group = db_fetch_object($result)) {
// I don't like the way this is done, but this is the only way I could figure out how to sort by membercount
$membercount = mysql_num_rows(db_query("SELECT * FROM (group_members) WHERE group_members.gid = '%d'", $group->gid));
db_query("UPDATE (group_info) SET group_info.members = '%d' WHERE group_info.gid = '%d'", $membercount, $group->gid);
$row = array(
array('data' => l("$group->title", "node/$group->nid"), 'width' => '100%'),
array('data' => format_date($group->created, 'custom', "M, Y"), 'align' => 'center'),
array('data' => $group->members, 'align' => 'center')
);
$rows[] = $row;
}
$form = theme('table', $header, $rows, $attributes);
$form .= theme('pager', null, 20, 0, tablesort_pager());
print theme('page', $form);
}
Any help is really appreciated!