Combo Page List Viewer by Category

2 posts by 2 authors in: Forums > CMS Builder
Last Post: August 15, 2013   (RSS)

By gregThomas - August 15, 2013

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

Greg Thomas







PHP Programmer - interactivetools.com