categories as tags

5 posts by 2 authors in: Forums > CMS Builder
Last Post: November 9, 2012   (RSS)

By rez - November 8, 2012 - edited: November 8, 2012

The bottom line is that I want to make <?php echo join(', ', $record['category:labels']); ?>
a list of links.


For a portfolio, I have a category menu setup. So far, its all one level. (project categories)

I have projects in another editor using these categories from an advanced list, num for value, breadcrumb for label.

I have my categories showing up in a navigation list and showing / searching ok in the address bar (setup in viewer urls to show projects and details on the same page), like:
mysite.com/portfolio.php?chairs-18

Each item / project can be assigned to multiple categories (checkboxes) in the project editor (advanced list mentioned above).

The display code (from code generator) for a project uses:
<?php echo join(', ', $record['category:labels']); ?>
to list all the categories a project is assigned to with a checkbox.

This is all working great. What I simply want to do is make each of the categories that are checked and already displaying per project, a link to that category (a tag). The same way my sidebar navigation list is working to load a category but in the display code per project, I dont know how to make that happen in the array (if that's the way to do it)


list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'project_cats',
//'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'categoryFormat' => 'showall', // showall, onelevel, twolevel
));


// 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;
list($blockRecords, $blockMetaData) = getRecords(array(
'tableName' => 'projects',
'orderBy' => 'RAND()',
//'debugSql' =>'true'
));

<?php if (!$selectedCategory): ?>
<?php foreach ($blockRecords as $block): ?>
show random projects
<?php endforeach ?><?php endif ?>

<?php if ($selectedCategory): ?>
<?php foreach ($itemsRecords as $itemRecord): ?>
<?php foreach ($itemRecord['image'] as $index => $upload): ?>
<a href="<?php echo $upload['urlPath'] ?>" class="thumb" data-lightbox="fancybox[215]" title="<?php echo htmlencode($itemRecord['title']) ?>">
<img src="<?php echo $upload['thumbUrlPath'] ?>" class="attachment-ci_listing_thumb" alt="<?php echo htmlencode($itemRecord['title']) ?>" title="<?php echo htmlencode($itemRecord['title']) ?>"></a>
<?php endforeach ?>

<div class="tags">
<?php echo join(', ', $itemRecord['category:labels']); ?>
</div>
<?php endforeach ?><?php endif ?>

Re: [rez] categories as tags

By rez - November 8, 2012 - edited: November 8, 2012

by the way, here is my sidebar code that loads categories to this portfolio page the same way the tags should. Some categories are currently empty (no projects with a category checkbox selected in the projects editor) How can I skip displaying empty category links in this nav menu?

<ul class="style6">
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<strong>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
</strong>
<?php else: ?>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>

Re: [rez] categories as tags

Hi Rez,

I would create the category links to the portfolio page like this:

<?php $linkArray = array_combine($itemRecord['category:values'], $itemRecord['category:labels']); ?>

<?php foreach($linkArray as $link => $title): ?>
<a href="http://mysite.com/portfolio.php?num=<?php echo $link; ?>" ><?php echo $title; ?></a>,
<?php endforeach; ?>


So first I've combined the array category labels and keys into one array. Then I've used a foreach loop to cycle through them, and display a link to the portfolio page.

Do you mean that the $categoryRecords array is sometimes returned with nothing in it? You can check if the categories array is currently empty like this:

<?php if($categoryRecords): ?>
<ul class="style6">
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<strong>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
</strong>
<?php else: ?>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>


So if the $categoryRecords array is empty then the the foreach loop will not execute.

Thanks!
Greg Thomas







PHP Programmer - interactivetools.com

Re: [rez] categories as tags

So you only want to display a category from the 'project_cats' table if it has an associated checkbox ticked under the 'projects' table? Or is this checkbox under the 'project_cats' table?

If it is under the project_cats table you could do something like this:

<ul class="style6">
<?php foreach ($categoryRecords as $categoryRecord): ?>
<?php if($categoryRecord['checkBoxField'] != '1') ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<strong>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
</strong>
<?php else: ?>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>


If a checkbox field is not ticked then nothing will be displayed for that row.

If it is the 'category' checkbox under the projects table then you might have to do something a bit more complicated like this:

<ul class="style6">
<?php foreach ($categoryRecords as $categoryRecord): ?>

<?php
$hasproject = false;
foreach($itemsRecords as $row){
if(in_array($categoryRecord['num'], $row['category:values']) ){
$hasproject = true;
}
}
?>
<?php if($hasproject): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?>
<strong>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
</strong>
<?php else: ?>
<?php if (!$categoryRecord['_hasChild']):?>
<a href="<?php echo $categoryRecord['_link'] ?>">
<?php endif; ?>
<?php echo $categoryRecord['name'] ?>
<?php if (!$categoryRecord['_hasChild']):?>
</a>
<?php endif; ?>
<?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endif; ?>
<?php endforeach; ?>
</ul>


Having to cycle through 3 arrays like this will be very server intensive and could slow down your site a bit. You might be better adding a hide/show tick box to the project_cats section and using the first method I suggested.

Thanks
Greg Thomas







PHP Programmer - interactivetools.com