finding last child entry in category menu with 2 levels
5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 15, 2011 (RSS)
I was wondering if anybody knows how to get php to find out if an entry in a foreach loop is the last child of the parent entry. So, if I had 3 level 0 entries and each had 4 or 5 child entries (depth 1), how could I vary the output for the final entry in each main record?
I imagine an "if" statement would work here, but I'm not really sure how to structure it.
Any help is appreciated!
Ian
Re: [buttermilk] finding last child entry in category menu with 2 levels
By Jason - August 15, 2011
You can use the _isLastChild field to determine this. So for example:
<?php foreach ($categoryRecords as $category): ?>
<?php if ($category['_isLastChild']): ?>
//output when the current record is the last child
<?php else: ?>
// output when the current record is not the last child
<?php endif ?>
<?php enforeach?>
Hope this helps get you started.
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] finding last child entry in category menu with 2 levels
By buttermilk - August 15, 2011 - edited: August 15, 2011
I gave that a shot but it didn't work out. Below that code I tried a simple if statement that I've used successfully before just to make sure the foreach loop was working. Then, below that, I used the standard list viewer. The latter two worked for me as expected, but I couldn't the _isLastChild working.
my sql code was:
<?php
// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/home/jocotoco/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
// load records
list($speciesRecords, $speciesMetaData) = getRecords(array(
'tableName' => 'species',
));
?>
The php in the order described above is below:
<h1>Species - List Page Viewer</h1>
***********************
<br />
<br />
<?php foreach ($speciesRecords as $category): ?>
<?php if ($category['_isLastChild']): ?>
last child
<?php else: ?>
NOT the last child
<?php endif ?>
<?php endforeach?>
<br />
<br />
*******************
<br />
<br />
<br />
<br />
<?php foreach ($speciesRecords as $category): ?>
<?php if ($category['depth'] == 0): ?>
This is 0 depth<br />
<?php else: ?>
NO <br />
<?php endif ?>
<?php endforeach?>
<br />
<br />
*****************************
<br />
<br />
<br />
<br />
<?php foreach ($speciesRecords as $record): ?>
Parent Category: <?php echo $record['parentNum'] ?><br/>
<h2><?php echo $record['name'] ?></h2>
Content: <?php echo $record['content'] ?><br/>
<hr/>
<?php endforeach ?>
I tried tweaking it sundry ways, but no joy.
Oh here's the output I was getting:
http://74.52.72.36/~jocotoco/species_test3.php
Ian
p.s. I've attached a screenshot of the section I'm working with. (the "last" entries are only there because I came up with some code that would insert content based on their presence. It's kind of a hack solution, hence the question.)
Re: [buttermilk] finding last child entry in category menu with 2 levels
By Jason - August 15, 2011
_isLastChild is a pseudo field that get's created when you use getCategories.
Try this change:
// load records
list($speciesRecords, $speciesMetaData) = getCategories(array(
'tableName' => 'species',
));
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] finding last child entry in category menu with 2 levels
Works perfectly. Thanks, Jason!
Ian