categories as tags

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

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

By gregThomas - November 8, 2012

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: [greg] categories as tags

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

The first solution works great. I need array experience, nice.

For the second part, no there will always be categories that have projects so your code would always be true. What I need to do is when listing every category in that sidebar navigation, skip the empty categories that don't have any projects assigned to them (with a checkbox in the project editor).

I'll try this again but would appreciate any help when you can. thanks!

Re: [rez] categories as tags

By gregThomas - November 9, 2012

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