Listing Records that have Multiple Category Options

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

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: [chris] Listing Records that have Multiple Category Options

By Mikey - November 2, 2010 - edited: November 2, 2010

Chris,
I've racked my brains over this and can't seem to figure this out. Here's what I'm trying to do. I've got a Media/Photo Gallery ($media_gallery) that has Categories ($media_categories) pulled in from a section field using the advanced option and I've also created a Date field for the Media/Photo Gallery ($media_gallery) section so when I upload new photos or media to the Media/Photo Gallery I can assign a Date value to the media and/or photos.

In the form below I have two drop down menus to allow site visitors to drill down on the media and photos by Category and/or Date.

The Category search filter is working fine, but for my Date drop down search filter, I'm getting repeating dates such as 2009, 2009, 2010, 2010, 2010... basically a date for each photo of media item that has been uploaded.

I'm trying to trim the date results in the Date drop down menu so that it only displays one 2009, 2010, 2011, and so on. So when someone chooses the Date filter and selects a Year, it returns all results for media and photos uploaded for the year selected. And/Or if the site visitor chooses a Category filter and a Date filter, it would return results for the Category chosen based on the Date filter selected.

Can you shed some light on this and what I need to do to get this working?
Thanks Zick

<?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',
));




<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 ($media_galleryRecords as $record): ?>
<option value="<?php echo date("Y", strtotime($record['date'])) ?>"><?php echo date("Y", strtotime($record['date'])) ?></option>
<?php endforeach ?>
</select>

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

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