Combo Page List Viewer by Category
2 posts by 2 authors in: Forums > CMS Builder
Last Post: August 15, 2013 (RSS)
By mnoyes - August 14, 2013
I have a list of services, and each service can be placed into a category. I would like to have the list divided into the categories, with the category name at the head of each list. I have ben tried everything I can, and no luck.
here is the code I am working with....
-------------------------------
<?php
/* TEchnology Solutions */
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/apitech1/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load detail record from 'solutions'
list($solutionsRecords, $solutionsMetaData) = getRecords(array(
'tableName' => 'solutions',
'where' => whereRecordNumberInUrl(1), // If no record # is specified then latest record is shown
'loadUploads' => true,
'allowSearch' => false,
));
$detailRecord = @$solutionsRecords[0]; // get first record
if (!$detailRecord) { dieWith404("Record not found!"); } // show error message if no record found
// load list records from 'solutions'
list($solutionsRecords, $solutionsMetaData) = getRecords(array(
'tableName' => 'solutions',
'loadUploads' => false,
'allowSearch' => false,
));
?>
--------------------------------
Here is the list code......
----------------------------------
<?php foreach ($solutionsRecords as $listRecord): ?>
<?php $isSelected = ($listRecord['num'] == $detailRecord['num']); ?>
<?php if ($isSelected) { print "<b>"; } ?>
<img src="A.png" width="8" height="9" alt="<?php echo htmlencode($listRecord['title']) ?>" /> <a href="<?php echo htmlencode($listRecord['_link']) ?>"><?php echo htmlencode($listRecord['title']) ?></a><br/><br/>
<?php if ($isSelected) { print "</b>"; } ?>
<?php endforeach ?>
-------------------------------
You can see the page here:
http://www.apitechnology.com/solutions.php
Would like the list menu divide by ['category']
any direction or help would be appreciated.
Hi mnoyes,
Assuming your using a single select and not a multi select for your categories you could try using the following system:
<?php
/* TEchnology Solutions */
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/apitech1/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load detail record from 'solutions'
list($solutionsRecords, $solutionsMetaData) = getRecords(array(
'tableName' => 'solutions',
'where' => whereRecordNumberInUrl(1), // If no record # is specified then latest record is shown
'loadUploads' => true,
'allowSearch' => false,
));
$detailRecord = @$solutionsRecords[0]; // get first record
if (!$detailRecord) { dieWith404("Record not found!"); } // show error message if no record found
// load list records from 'solutions'
list($solutionsRecords, $solutionsMetaData) = getRecords(array(
'tableName' => 'solutions',
'loadUploads' => false,
'allowSearch' => false,
));
//Create a multi dimensional array with each record that appears in a category below
$solutionsRecords = array_groupBy($solutionsRecords, 'category:label', true);
?>
This is just example code, so you might have to make a few changes to get it working.
So the array_groupBy function cycles through an mutlidimensinal array and categorizes by an item in array. In the above example the category:label is used to group items and order items.
<?php foreach ($solutionsRecords as $key => $catRecord): ?>
<?php echo $key; ?>
<?php foreach ($catRecords as $listRecord): ?>
<?php $isSelected = ($listRecord['num'] == $detailRecord['num']); ?>
<?php if ($isSelected) { print "<b>"; } ?>
<img src="A.png" width="8" height="9" alt="<?php echo htmlencode($listRecord['title']) ?>" /> <a href="<?php echo htmlencode($listRecord['_link']) ?>"><?php echo htmlencode($listRecord['title']) ?></a><br/><br/>
<?php if ($isSelected) { print "</b>"; } ?>
<?php endforeach; ?>
<?php endforeach ?>
Then for displaying the items I've modified the code so that a foreach loop cycles through each category, and then a second foreach loop cycles through the records in each category.
Let me know if you have any questions.
Cheers!
Greg
PHP Programmer - interactivetools.com