Categories for Dummies - Multi Category, Multi Records Help

7 posts by 2 authors in: Forums > CMS Builder
Last Post: August 6, 2010   (RSS)

By crazyfish - August 6, 2010 - edited: August 6, 2010

Also titled PHP for dummies...

On the Catalyst Group website they post studies and reports. http://catalystgrp.com/php/tcgr.php ( click on Studies and Reports )

They enter some studies in more than one category but they only show up in the first category checked off.

"Categories" are in menu type category. In Studies & Reports, field name Category is a field type list, get options from database (advanced) and the section table name is caterories, num, name.

My code is as follows for a list page for category 1

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


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/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($studies_reportsRecords, $studies_reportsMetaData) = getRecords(array(
'tableName' => 'studies_reports',
'perPage' => '10',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => 'category=1',
'orderBy' => 'publish_date DESC',
));

?>
<?php

// Usage: < ?php print maxWords($content, 25); ? >
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 foreach ($studies_reportsRecords as $record): ?>


<span class="boldblack"><?php echo $record['title'] ?></span><br/>
<?php echo maxWords($record['content'],75) ?>...<br/>

Publish Date: <?php echo date("M, Y", strtotime($record['publish_date'])) ?><br/>

<a href="<?php echo $record['_link'] ?>">Read More</a><br/>
<br/>
<br/>
<?php endforeach ?>

<?php if ($studies_reportsMetaData['invalidPageNum']): ?>
Results page '<?php echo $studies_reportsMetaData['page']?>' not found, <a href="<?php echo $studies_reportsMetaData['firstPageLink'] ?>">start over &gt;&gt;</a>.<br/>
<br/>
<?php elseif (!$studies_reportsRecords): ?>
There are no current updates.<br/>
<br/>
<?php endif ?>
<!-- Display Records -->


<!-- Display Page Links -->
<?php if ($studies_reportsMetaData['prevPage']): ?>
<a href="<?php echo $studies_reportsMetaData['prevPageLink'] ?>">&lt;&lt; prev</a>
<?php else: ?>
&lt;&lt; prev
<?php endif ?>

- page <?php echo $studies_reportsMetaData['page'] ?> of <?php echo $studies_reportsMetaData['totalPages'] ?> -

<?php if ($studies_reportsMetaData['nextPage']): ?>
<a href="<?php echo $studies_reportsMetaData['nextPageLink'] ?>">next &gt;&gt;</a>
<?php else: ?>
next &gt;&gt;
<?php endif ?>

I know I'm missing something simple here. I thank you for your time.

Re: [crazyfish] Categories for Dummies - Multi Category, Multi Records Help

By Jason - August 6, 2010

Hi,

The category field in your Studies & Reports table, is it a multi-select drop down? If it is, the problem is CMS Builder stores multiple selects as a list of values separated by a tab character (\t). If you're trying to get records who's category equals "1", try something like this:

// load records
list($studies_reportsRecords, $studies_reportsMetaData) = getRecords(array(
'tableName' => 'studies_reports',
'perPage' => '10',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => "category LIKE '%\t1\t% ' ",
'orderBy' => 'publish_date DESC',
));


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] Categories for Dummies - Multi Category, Multi Records Help

By crazyfish - August 6, 2010

The field type is a list, checkboxes ( multi value )
Would that matter?

Re: [Jason] Categories for Dummies - Multi Category, Multi Records Help

By crazyfish - August 6, 2010

No luck, in fact it comes up as no records as in the error "There are no current updates"

Re: [crazyfish] Categories for Dummies - Multi Category, Multi Records Help

By Jason - August 6, 2010

Hi,

Make sure there is no spaces between the % and the '
So it looks like this:
'where' => "category LIKE '%\t1\t%' ",

Give that a try. If this doesn't work, please attach a copy of the .php file you're working with so I can take a closer look.

Thanks.
---------------------------------------------------
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] Categories for Dummies - Multi Category, Multi Records Help

By crazyfish - August 6, 2010

That's the trick. So much for cut and paste!
Thanks for the help, you guys are great.