multi list / categories
2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 8, 2013 (RSS)
By rez - October 3, 2013 - edited: October 3, 2013
i have a category setup with categories in one editor and projects in another. in projects, i use a checkbox list to add projects to multiple categories. sort of like tags. it works well with my usual setup of selecting a category and using:
// if there is a category selected, make the multi selections
if ($selectedCategory):
list($itemsRecords, $itemsMetaData) = getRecords(array(
'tableName' => 'projects',
'where' => "category LIKE '%\t{$selectedCategory['num']}\t%'",
'perPage' => '16',
//'debugSql' =>'true'
));
endif;
but in a new portfolio im making, i would like the landing page when nothing is selected yet (master / details same page):
<?php if (!$selectedCategory): ?>
<?php $counter = 1; ?>
//portfolio preview 7 project per category
<ul class="mycarousel"><?php $counter = 1; ?>
<?php foreach ($projectsRecords as $record): ?>
<?php if($counter < 7) : ?>
<?php if($record['category']==$category['num']): ?>
<li>
<div class="item">
<?php foreach ($record['image'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath4'] ?>" alt="" />
<?php endif ?>
<?PHP endforeach ?>
<div class="item_title"><div class="type audio"></div><a href="#"><?php echo $record['title'] ?></a></div>
<div class="item_descr"><?php echo maxWords($record['content'], 6); ?> <a href="#">Read more...</a></div>
</div>
</li>
<?php endif ?>
<?php $counter++ ?>
<?php endif ?>
<?PHP endforeach ?>
</ul>
<?php endif ?>
But that doesn't work because of the multiple categories each project may be in. $record['category'] will not equal $category['num']) How can i show a number of projects on a page for each category (check boxes are true) when no category is selected / in the url? I hope i can do this without too may loops lagging my page :(
By Daryl - October 8, 2013 - edited: October 9, 2013
Hi Rez,
If my understanding is right, you can list all the categories and all the projects in each category if no category is selected by looping through all the categories instead of projects and pull out the projects that are in that category.
Example:
<?php if (!$selectedCategory): ?>
// load all categories if no category is selected
$getCategoryRecords = mysql_select('categories');
// loop through each category
foreach ($getCategoryRecords as $category){
// load all the projects under this category
list($projectRecords, $projectRecordsMetaData) = getRecords(array(
'tableName' => 'projects',
'where' => "category LIKE '%\t{$category['num']}\t%'",
'perPage' => '7'
));
// if there's a project in this category, loop through the projects
if ($projectRecords){
foreach ($projectRecords as $projects) {
// your code here
}
}
}
<?php endif ?>
If you're worried about your page lagging because of the nested loops above, we're only pulling out and looping through 7(?) projects per category so I don't think it will cause any significant lagging.
But if you have a really big list of categories and projects, what I would do is I will add a pagination with 5-10 categories per page. We might also want to consider the file size of the images for each projects that will be loaded to the page.
Hope this helps!
Cheers,
PHP Programmer - interactivetools.com