Main
Index
Search
Posts
Who's
Online
Log
In

Home: Products: CMS Builder:
Sub Category Display - When Empty

 

 


theclicklab
User

Dec 5, 2011, 5:17 PM

Post #1 of 7 (1216 views)
Shortcut
Sub Category Display - When Empty Can't Post

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?


Code
<?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
~~~~~~~~~~~~~~~~~~
Jan Dunlop
theClickLab.com
nz.linkedin.com/in/jandunlop
~~~~~~~~~~~~~~~~~~


Collin
Novice


Dec 5, 2011, 7:19 PM

Post #2 of 7 (1211 views)
Shortcut
Re: [theclicklab] Sub Category Display - When Empty [In reply to] Can't Post

You can do it like this:


Code
<?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; ?>



theclicklab
User

Dec 5, 2011, 8:26 PM

Post #3 of 7 (1202 views)
Shortcut
Re: [Collin] Sub Category Display - When Empty [In reply to] Can't Post

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
~~~~~~~~~~~~~~~~~~
Jan Dunlop
theClickLab.com
nz.linkedin.com/in/jandunlop
~~~~~~~~~~~~~~~~~~


Collin
Novice


Dec 5, 2011, 9:41 PM

Post #4 of 7 (1197 views)
Shortcut
Re: [theclicklab] Sub Category Display - When Empty [In reply to] Can't Post

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:


Code
<?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:


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; ?>



(This post was edited by Collin on Dec 5, 2011, 10:02 PM)


theclicklab
User

Dec 6, 2011, 11:10 AM

Post #5 of 7 (1181 views)
Shortcut
Re: [Collin] Sub Category Display - When Empty [In reply to] Can't Post

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

BTW...


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


gave me the following error:

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

any idea why?

Many thanks again :)
~~~~~~~~~~~~~~~~~~
Jan Dunlop
theClickLab.com
nz.linkedin.com/in/jandunlop
~~~~~~~~~~~~~~~~~~


Jason
Staff / Moderator


Dec 7, 2011, 10:36 AM

Post #6 of 7 (1171 views)
Shortcut
Re: [theclicklab] Sub Category Display - When Empty [In reply to] Can't Post

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:


Code
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 - Programmer 
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/ 


theclicklab
User

Dec 7, 2011, 10:39 AM

Post #7 of 7 (1169 views)
Shortcut
Re: [Jason] Sub Category Display - When Empty [In reply to] Can't Post

Thanks Jason, that's good to know.

Cheers
~~~~~~~~~~~~~~~~~~
Jan Dunlop
theClickLab.com
nz.linkedin.com/in/jandunlop
~~~~~~~~~~~~~~~~~~