Listing Records that have Multiple Category Options

8 posts by 5 authors in: Forums > CMS Builder
Last Post: November 3, 2010   (RSS)

By studio-a - April 2, 2010

We have a listing display problem which we’re not clear how to code.

01. We have created a table for CATEGORY so that our client can add or remove category records as they see fit.
02. We have another table for JOBS which references the CATEGORY table and is displayed within the admin section as check boxes for multiple selections. Each Job record may have more than one Category (hence the check box cross references).
03. We want to display a Weekly New Job listings on the home page.
04. Rather than list the Job by its title, the client prefers to list the new posting by its related category and link to the Listing Page for that category.
05. The Weekly New Jobs will be coded as an "include" for the home page file.
06. Since each Job may have more than one category, how do we
-- A) Limit the New listing to ONE category being displayed for that one record?
-- B) Not repeat the category being displayed from another job record within the loop for the include?
-- C) Link each Job listing (as a category name) to the list page for that category?

This is what we have started, but needs serious help! We realize the echo join is wrong, but it’s the only way we know how to output something that works without errors.

<?php
require_once dirname(dirname(__FILE__)) . "/cmsAdmin/lib/viewer_functions.php";

list($jobsRecords, $jobsMetaData) = getRecords(array(
'tableName' => 'jobs',
'orderBy' => 'updatedDate DESC',
'limit' => '10',
));

?>
<?php foreach ($jobsRecords as $record): ?>
<a href=" listJobs.php?jobs=<?php echo $record['category '] ?>”>
<?php echo join(', ', getListLabels('jobs', 'category', $record['category'])); ?>
</a><br/>

<?php endforeach ?>
<?php if (!$jobsRecords): ?>
No records were found.<br/>
<br/>
<?php endif ?>


Your help is appreciated!

Re: [studio-a] Listing Records that have Multiple Category Options

By Chris - April 5, 2010

Hi studio-a,

I'm confused about 6.a: how can a single category be displayed for a job that could belong to multiple categories? Should a category be picked at random?

Here's an approach which gathers up all the categories referenced by the jobs (preventing duplicates) and then displays them all.

<?php
require_once dirname(dirname(__FILE__)) . "/cmsAdmin/lib/viewer_functions.php";

list($jobsRecords, $jobsMetaData) = getRecords(array(
'tableName' => 'jobs',
'orderBy' => 'updatedDate DESC',
'limit' => '10',
));

$categoryNums = array();
foreach ($jobsRecords as $record) {
foreach (explode("\t", trim($record['category'])) as $catNum) {
$categoryNums[$catNum] = true;
}
}

?>
<?php foreach (array_keys($categoryNums) as $catNum): ?>
<a href="listJobs.php?jobs=<?php echo $catNum ?>">
<?php echo join('', getListLabels('jobs', 'category', $catNum)); ?>
</a><br/>
<?php endforeach ?>

<?php if (empty($categoryNums)): ?>
No job categories were found.<br/>
<br/>
<?php endif ?>


Does this help? Please let me know if you have any questions.
All the best,
Chris

Re: [zick] Listing Records that have Multiple Category Options

By Toledoh - November 2, 2010

Hey Zick.

I expect that something similar to this thread would work... but it's pushng my grey matter a bit far!
http://www.interactivetools.com/iforum/Products_C2/CMS_Builder_F35/Listing_Records_that_have_Multiple_Category_Options_P79113/gforum.cgi?post=72447;search_string=group;t=search_engine#72447
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Listing Records that have Multiple Category Options

By Toledoh - November 3, 2010

Re: [zick] Listing Records that have Multiple Category Options

By Jason - November 3, 2010

Hi Zick,

Try using this code:
(changes highlighted in red)

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'abcsite/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/a/b/c/site/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 records
list($media_galleryRecords, $media_galleryMetaData) = getRecords(array(
'tableName' => 'media_gallery',
'perPage' => '9',
));

// load records
list($media_categoriesRecords, $media_categoriesMetaData) = getRecords(array(
'tableName' => 'media_categories',
));


//get unique years
$years=array();
foreach($media_galleryRecords as $record){
$tmpYear = date("Y",strtotime($record['date']));
$years[$tmpYear]=$tmpYear;
}

?>


<div id="media_search">
<!-- search filter -->
<!-- search media category filter -->
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="gallery_categories_query">
<option value="">categories</option>

<?php foreach ($media_categoriesRecords as $categoryRecord): ?>
<option value="<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></option>
<?php endforeach ?>
</select>

<!-- search media date filter -->
<select name="date_query">
<option value="">Year</option>

<?php foreach($years as $year):?>
<option value="<?php echo $year;?>"><?php echo $year;?></option>
<?php endforeach ?>


</select>

<input name="submit" type="submit" class="button" value="begin">
</form>
<!-- /search filter --></div>


This code first creates an array of years. Any duplicates overwrite each other so you can only have 1 of each year. The foreach loop inside your <select> simply uses the $years array for it's options.

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/

Re: [Jason] Listing Records that have Multiple Category Options

By Mikey - November 3, 2010

Works perfectly!
Thanks for the help Jason!

Re: [Toledoh] Listing Records that have Multiple Category Options

By Mikey - November 3, 2010

Toledoh, I wasn't able to figure out how to utilize the threads you suggested to implement the Date drill down I needed, but the threads you suggested contain very useful information that I plan to apply to a few other sections of my site... so a big thanks for sharing the threads!

Zick