Sub Category Display - When Empty

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

By theclicklab - December 5, 2011

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

By Collin - December 5, 2011

You can do it like this:

<?php if (!empty($pagesRecords)): ?>
<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>
<?php endif; ?>

Re: [Collin] Sub Category Display - When Empty

By theclicklab - December 5, 2011

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

By theclicklab - December 6, 2011

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

By theclicklab - December 7, 2011

Thanks Jason, that's good to know.

Cheers