SORTING BY GROUP AND INSERTING GROUP HEADINGS IN A LIST VIEWER (ANOTHER APPROACH) - Mar 23rd, 2012


This approach seems to be a bit easier to implement but it only worked with a bit of help from Jason Sauchuk, from
Interactive Tools.

I wanted to allow for an expanding list of categories (actually board of director positions in an organization) that
could be managed by my client from the management interface, without resorting to any coding changes.

The first thing I did was to create a multi-record editor called board_of_director_positions with only one text field
called position. I set the sort to dragSortOrder DESC so that the positions would appear in the correct order.

Then, in the accounts editor, since some board members held 2 positions, I populated 2 single value list fields
(board_of_director_position_1 and board_of_director_position_2) using the board_of_director_positions table as the
source, the num field for the option values and the position field for the label values.

Then I used array_pluck after the list records call to create a variable containing a list of the positions so that I
could display their names in my viewer


<?php

list($accountsRecords, $accountsMetaData) = getRecords(array(
'tableName' => 'accounts',

));

list(
$board_of_director_positionsRecords, $board_of_director_positionsMetaData) = getRecords(array(
'tableName' => 'board_of_director_positions',
));

$directorposition = array_filter(array_pluck($board_of_director_positionsRecords, 'position')); ?>


In the body of the viewer, I looped through and listed all of the positions, and then looped through all of the accounts
editor records for matches to those positions.

After much consternation, Jason suggested adding the pseudo field :label to the accounts record fields, since otherwise
I was trying to compare a record number to a text label. And that popped the office holders names into their correct
slots.



?php foreach ($directorposition as $position): ?>

<h3 class="your_class_1"><?php echo strtoupper($position); ?></h3>

<?php foreach ($accountsRecords as $record): ?>

<?php if ($record['board_of_director_position_1:label'] == $position || $record['board_of_director_position_2:label'] ==
$position) : ?>

<span class="your_class_2"><?php echo strtoupper($record[full_name']); ?></span>
<?php endif ?>

<?php endforeach ?> <hr />
<?php endforeach ?>


This concept is easily applied to something like an FAQ page where there are multiple categories and multiple questions
in each category.

Create a multi-record FAQ CATEGORIES editor with one text field called category, a dragSortOrder field and an optional
hidden checkbox.
Create a multi-record FAQ editor with a single value pull down list field populated using the faq_categories table as
the source, the num field for the option values and the position field for the label values.

Then, as above, I used array_pluck after the list records call to create a variable containing a list of the categories
so that I could display their names in my viewer



$faqgroup = array_filter(array_pluck($faq_categoriesRecords, 'category'));



Again as above, In the body of the viewer, I looped through and listed all of the categories, and then looped through
all of the FAQ editor records for matches to those categories.


<?php foreach ($faqgroup as $group): ?>
<h1 class="your_class_1"><?php echo strtoupper($group); ?></h1>
<?php foreach ($faqRecords as $record): ?>
<?php if ($record['category:label'] == $group) : ?>
<?php $question = htmlspecialchars($record['question']); ?> <span class="your_class_2"><?php echo strtoupper($question);
?></span>
<div align="left" class="your_class_3"><?php echo maxWords($record['answer'], 25); ?>
<?php if (wordCount($record['answer']) > 25) : ?><a class="special" href="<?php echo $record['_link']; ?>"><span
class="body-text-bold-10">... read more</span></a><?php endif; ?>
</div>
<?php endif ?>

<?php endforeach ?>
<hr />
<?php endforeach ?>


Since I'm using a detail page for the complete answers, I included functions called maxWords and wordCount to show a
"read more" link only if there were more than 25 words in the answer. These functions are defined in the head section of
my viewer, with:



<?PHP
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (
count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return
$output;
}
?>

<?PHP
function wordCount($textOrHtml) {
$text = strip_tags($textOrHtml, "<b></b><i></i>");
$words = preg_split("/\s+/", $text);

return
count($words);
}
?>


This works well and allows for discontinuous category entries in the record list, but I'd like to be able to skip
categories headings with no record matches.

Any ideas? Pass them on through the contact page.



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service