Restricting Category Table to top 2 levels

2 posts by 2 authors in: Forums > CMS Builder
Last Post: January 23, 2013   (RSS)

By gregThomas - January 23, 2013

Hi Jan,

I think the easiest method too retrieve the top two levels of a category section would be to use the getCategories function instead. You could use something like this:

//The selectedCategoryNum key will ensure that top level items are selected, the categoryFormat key will ensure that only level one and two items are selected.

list($mainNav, $selectedNav) = getCategories(array( 

  'tableName' => 'destinations',
  'loadUploads' => false,
  'selectedCategoryNum' => "0",
  'categoryFormat' => 'twolevel'
));

$valuesToLabels = array();

//Create a list of all the values in the category array. You might need to change the fields used to match your table
foreach($mainNav as $category){
  $valuesToLabels[$category['num']] = $category['name'];
}

?>
<p class="destinations">Destinations:
<?php $count2=0; ?>
<?php foreach ($valuesToLabels as $value => $label): ?><?php echo ($count2==0)? "" : ", " ?><a href="/destination.php/<?php echo str_replace(" ","-",strtolower($label."-".$value)); ?>/"><?php echo htmlspecialchars($label); ?></a><?php $count2++;?><?php endforeach ?></p>

This is just an example, so you might need to change some of the variable names to get this working. Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com