Grouping by Categories then Sub-Categories

2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 11, 2011   (RSS)

By MickC - October 9, 2011 - edited: October 9, 2011

Hi guys,

I have implemented Gerry's code for inserting headings when a category changes, works great.

I am trying to generate reports that list all of our equipment by "Type" then groups each type into its "branch"

I effectively got the code working by doubling it up, and changing fields, and added in listing counts at each heading.

I can get it to work, however the issue is this:

When a "Type" heading changes, and the only listing is one for the same "branch" as the last record in the previous "type", it doesnt list the "Branch" at the start of the group.

Sounds a bit complex, so attached is a screenshot of the results I am getting.
Note that the heading "Lawn Mowers', the listings are from "Haeuslers Echuca", which is the same as the last record from the previous Equipment type, and therefore does not list the branch heading because it is not different to the last listing.

Basically, every time the Equipment type changes, i need the branch heading to be displayed, regardless of if it hasn't changed from the last Equipment type.


Also here is the very dodgy and un-refined code I have so far:


<?php $old_type = ''; ?>
<?php $old_branch = ''; ?>
<?php foreach ($listingsRecords as $record): ?>

<?php $branch = $record['branch'];
$type = $record['type'];// load sub-group value from record.
if ($type != $old_type) { // If different from the last sub-group value, print the sub-group name.
print '<div class="spacer"> space </div><table bgcolor="#009933" style="page-break-before:auto; page-break-after:avoid" width="900" height="5" align="center" border="0">';
print '<tr><td class="toplinksl" align="left"><strong>';
echo "$type";
print '</strong></td><td align="right"><span class="toplinksr"> Listings: <strong> ';
list($listingsCount, $listingsCountMetaData) = getRecords(array(
'tableName' => 'listings',
'where' => "type='$type'"
));

echo $listingsCountMetaData['totalRecords'];
//echo mysql_select_count_from($listingsCount, 'branch');
print '</strong></span></td></tr></table>';}
if ($type != $old_type) { // If different from the last sub-group value, print the sub-group name.
print '<hr width="900" style="margin-bottom:0; margin-top:0" color="#009900" />'; } ?>

<?php $old_type = $type; // retain sub-group name before moving to new record. ?>

<?php if ($branch != $old_branch) { // If different from the last sub-group value, print the sub-group name.
print '<table style="page-break-before:auto; page-break-after:avoid" width="900" align="center" border="0">'; print '<hr width="900" noshade="noshade" style="margin-bottom:0; margin-top:1" color="#009900" />';
print '<tr><td class="toplinks2" align="left"><strong>&nbsp;&nbsp;';
print 'Haeuslers '; echo "$branch";
print '</strong></td><td align="right"><span class="toplinksr2"> Listings: <strong> ';
list($listingsCount2, $listingsCount2MetaData) = getRecords(array(
'tableName' => 'listings',
'where' => "branch='$branch' and type='$type' and approved='1'"

));
echo $listingsCount2MetaData['totalRecords'];
//echo mysql_select_count_from($listingsCount, 'branch');
print '</strong></span></td></tr></table>';}
if ($branch != $old_branch) { // If different from the last sub-group value, print the sub-group name.
print '<hr width="900" noshade="noshade" style="margin-bottom:0; margin-top:0" color="#009900" />'; }
?>
<?php $old_branch = $branch; // retain sub-group name before moving to new record. ?>

Attachments:

cmsb.png 258K

Re: [MickC] Grouping by Categories then Sub-Categories

By Jason - October 11, 2011

Hi,

So you want the branch heading to change if the current branch is different from the previous branch, or if the type has changed. Is that right?

If so, you could change your if statement like this:

if ($branch != $old_branch || $type != $old_type) { // If different from the last sub-group value, print the sub-group name.

Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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