Sub Category Display - When Empty

7 posts by 3 authors in: Forums > CMS Builder
Last Post: December 7, 2011   (RSS)

Hi there,

So I have the following code which shows a list of pages within a section but if the array is empty I still get the surrounding h2 and ul...

My question is: How to I test for an empty array to hide this?

<?php
// Subcat navigation - First Level
list($pagesRecords, $pagesMetaData) = getCategories(array(
'tableName' => 'pages',
'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'loadUploads' => '0',
// 'debugSql' =>'true',
));
?>

<?php $selectedCat=$pagesRecord['num']; ?>

<h2>In this Section:</h2>
<ul>
<?php foreach ($pagesRecords as $record): ?>
<?php if ($record['depth'] == 0) { continue; } ?>
<?php if($record['hide']==0 && $record['parentNum']==$selectedCat): ?>
<li><a href="<?php echo ($record['_link']);?>"><?php echo htmlspecialchars($record['name']);?></a></li>
<?php endif ?>
<?php endforeach; ?>
</ul>


Many thanks

Re: [Collin] Sub Category Display - When Empty

Ah doesn't work :(

probably because I am on a page record already so its not actually empty....

See attached - subnav code starts at line 60

any other solutions?

MAny thanks

Re: [theclicklab] Sub Category Display - When Empty

By Collin - December 6, 2011 - edited: December 6, 2011

I can't see an attachment, but I think I see where the problem is. You're actually filtering out when depth == 0 in the foreach loop, so the pagesRecords is not actually empty. Perhaps try this when you are getting the records:

<?php
// Subcat navigation - First Level
list($pagesRecords, $pagesMetaData) = getCategories(array(
'tableName' => 'pages',
'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'loadUploads' => '0',
'where' => 'depth <> 0', // filter out the depth of 0 <----------
// 'debugSql' =>'true',
));
?>


Edit: I just noticed you have further filtering on this line:
<?php if($record['hide']==0 && $record['parentNum']==$selectedCat): ?>
We should probably add these filters to the getCategories call too.

$selectedCat = pagesRecord['num'] <-- not sure where pagesRecord is defined.

It'll probably be more clear when I see the attachment. Could you try attaching it again?

Thanks.

Edit2: I just thought of another solution that will require less rewriting of your code:

<?php $links = ''; ?>
<?php foreach ($pagesRecords as $record): ?>
<?php if ($record['depth'] == 0) { continue; } ?>
<?php if($record['hide']==0 && $record['parentNum']==$selectedCat): ?>
<?php $links .= '<li><a href="' . $record['_link'] . '">' . htmlspecialchars($record['name']) . '</a></li>'; ?>
<?php endif ?>
<?php endforeach; ?>

<?php if (!empty($links)): ?>
<h2>In this Section:</h2>
<ul>
<?php echo $links; ?>
</ul>
<?php endif; ?>

Re: [Collin] Sub Category Display - When Empty

Hi Collin, that second option worked, thanks so much !!

BTW...

'where' => 'depth <> 0',

gave me the following error:

Category Viewer (pages) errors
Unknown option 'where' specified

any idea why?

Many thanks again :)

Re: [theclicklab] Sub Category Display - When Empty

By Jason - December 7, 2011

Hi Jan,

The reason you're getting an error is that the getCategories function doesn't have a "where" option in it. If you need a where option, you can use a getRecords call instead like this:

list($pagesRecords, $pagesMetaData) = getRecords(array(
'tableName' => 'pages',
'loadUploads' => '0',
'where' => 'depth != 0', // filter out the depth of 0 <----------

));


This will only work if you don't need any of the special fields that come with the getCategories call (ie, listItemStart, etc). If you do need those fields, you can use getCategories (without the where option) and then filter out your records in your foreach loop.

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] Sub Category Display - When Empty

Thanks Jason, that's good to know.

Cheers