Display pod items by category
Hi there,
I have a pod for "Verdicts" and another for "verdict_category". On my podpage i'd like to show a list of verdicts by category and each set of verdicts under its respective category name. However, i'd like for this to be dynamic, so that when i add more categories and verdicts, they show up automatically without me having to modify the page template over and over. Any help would be appreciated and no, i'm unfortunately not a PHP developer, so you'll have to spell it out for me a bit. Thanks!
6 Answers
Try this out:
<?php
$pods = new Pod('verdict_category');
$pods->findRecords('t.name',-1);
$verdicts = new Pod('verdicts');
while($pods->fetchRecord())
{
$verdicts->findRecords('t.name',-1,'verdict_category.id='.$pods->get_field('id'));
if($verdicts->getTotalRows())
{
?>
<h2><?php echo $pods->get_field('name'); ?></h2>
<?php
echo $verdicts->showTemplate('verdict_list');
}
}
edited Oct 27 '10 at 1:32
Follow my previous example, and go another level deeper:
<?php
$pods = new Pod('verdict_category');
$pods->findRecords('t.name',-1);
$verdict_subcats = new Pod('verdict_category');
$verdicts = new Pod('verdicts');
$verdict_array = array(); // do this to add further logic to not show category names unless there are verdicts
while($pods->fetchRecord())
{
$verdict_subcats->findRecords('t.name',-1,'parent.id='.$pods->get_field('id'));
if($verdict_subcats->getTotalRows())
{
while($verdict_subcats->fetchRecord())
{
$verdicts->findRecords('t.name',-1,'verdict_category.id='.$verdict_subcats->get_field('id'));
if($verdicts->getTotalRows())
{
ob_start();
echo $verdicts->showTemplate('verdict_list');
if(!isset($verdict_array[$pods->get_field('name')]))
{
$verdict_array[$pods->get_field('name')] = array();
}
$verdict_array[$pods->get_field('name')][$verdict_subcats->get_field('name')] = ob_get_clean();
}
}
}
}
foreach($verdict_array as $category_name => $subcats)
{
?>
<h2><?php echo $category_name; ?></h2>
<?php
foreach($subcats as $subcat_name => $verds)
{
?>
<h3><?php echo $subcat_name; ?></h3>
<?php
echo $verds;
}
}
edited Oct 28 '10 at 2:34
Thank you sir, that worked perfectly!
Question #2: How would i use this with your category package where child categories and their respective items would need to be displayed under parent category names ?
THANK YOU. I needed this and struggled with it for hours until I found this. thank you thank you thank you!
Alright - I have one slight mod request to the first code (the one without sub cats)
I want to be able to show/not show a category based on a boolean field "active" in the "verdict_category" pod.
I would like the code to work the same as it is now, except <?php if( $pods->get_field('active')==1 ) : ?> needs to wrap around the h2/html output.
When I try to put it before h2 and after the echo $verds I get an error.
Many thanks!


