For anyone else who wishes a vertical menu in a similar fashion, here's the final code (many, many thanks to Eric).
This will give you a menu in list fashion (Yeah, CSS!), where the (#) is the sum total of listings in the Sub Sub Categories of the parent Sub Category ... gee, I hope that makes sense. If not, hopefully the illustration below will help.
Menu Structure Example:
<ul>
<li>Main Category
<ul>
<li>Sub Category (#)</li>
<li>Sub Category (#)</li>
<li>Sub Category (#)</li>
<li>Sub Category (#)</li>
</ul>
</li>
</ul>
Essentially, the actual listings themselves are found only in the Sub Sub Categories:
The Main Category does not allow listings.
The Sub Categories do not allow listings.
The Sub Sub Categories have listings.
To Implement the Menu ...
----------------------------------------------------------------------------------------
First create a new file named:
includes/classes/smarty/plugins/function.categories_ul.php
Then include this code:
<?php
/**
* smarty_function_categories_ul
*
* This function is useful for using your categories in the navigation.
*
* All source code & content (c) Copyright 2006, 68 Classifieds
* unless specifically noted otherwise.
*
* @package 68classifieds
* @author Eric Barnes
* @copyright 68 Classifieds
* @link
http://www.68classifieds.com
* @$Revision: 1.1 $
* @Updated: $Date: 2007/01/08 19:12:22 $
*/
function smarty_function_categories_ul($params, &$smarty)
{
global $db;
$ul_attr = '';
$li_attr = '';
$depth=2;
$show_total = 'Y';
foreach ($params as $_key=>$_value)
{
switch ($_key)
{
case 'depth':
$$_key = (int)$_value;
break;
case 'ul_attr':
case 'li_attr':
case 'show_total':
$$_key = (string)$_value;
break;
}
}
$sSQL="SELECT id,name,parent_id,cOrder,cLink FROM ".PREFIX."categories WHERE parent_id=0 AND display<>'N' ORDER BY cORDER DESC, name ASC";
$result=$db->query($sSQL);
$output="<ul ".$ul_attr." class=\"menu\">\n";
while($row = $result->fetch())
{
$id=$row['id'];
$parent=$row['parent_id'];
if($row['cLink']<>'')
{
$link=$row['cLink'];
}
else
{
$link='category.php?type='.$id;
}
$output .= "<li ".$li_attr."><a class=\"topborder\" href=\"".$link."\">".$row['name']."</a>\n";
/*
if($show_total=='N')
{
$arr_childs='';
$arr_childs = array($id);
get_cat_ids($arr_childs);
$total=get_cat_count($arr_childs);
$output.="( ". $total .")";
}
*/
//get sub categories
$sSQL2="SELECT id,name,parent_id,cOrder,cLink FROM ".PREFIX."categories WHERE parent_id=".$id." AND display<>'N' ORDER BY cORDER DESC, name ASC";
//$output.=$sSQL2."\n";
$result2=$db->query($sSQL2);
if($result2->size() > 0)
{
$output.="<ul class=\"submenu\">\n";
while($row = $result2->fetch())
{
$subid=$row['id'];
if($row['cLink']<>'')
{
$link=$row['cLink'];
}
else
{
if($subid>1)
{
$link='category.php?type='.$id.'&sec='.$subid;
}
else
{
$link='category.php?type='.$subid;
}
}
$output .= "<li ".$li_attr."><a href=\"".$link."\">".$row['name']."\n";
if($show_total=='Y') {
$arr_childs = array($subid);
$total=get_cats($subid);
$output.="(". $total .")</a>";
}
}
$output.="</li>\n";
$output.="</ul>\n";
}
$output.="</li>\n";
}
$output.="</ul>\n";
return $output;
}
function get_cats($parent)
{
global $db;
$sSQL="SELECT id FROM ".PREFIX."categories WHERE parent_id = ". $parent;
//echo $sSQL;
$result=$db->query($sSQL);
$i=0;
while($rs=$result->fetch())
{
$cat[]=$rs['id'];
$i++;
}
if($cat == '')
{
$total=0;
}
else
{
$total=get_cat_count($cat);
}
return $total;
}
function get_cat_count($arr_ids)
{
global $db, $modules;
$sSQL="SELECT COUNT(*) AS size FROM ".PREFIX."products WHERE ".$where." section In (". implode(', ', $arr_ids) .") AND expiration > NOW() AND (display='Y' OR display='S')";
$result=$db->query($sSQL);
$row = $result->fetch();
$total=$row['size'];
return $total;
}
?>
Next in your template file add:
{categories_ul show_total='Y'}
-------------------------------------------------------------------------------------------
If your listings are nested only one level deep (Main Category>Sub Category), then use this code in the functions.categories_ul.php instead of that above:
<?php
/**
* smarty_function_categories_ul
*
* This function is useful for using your categories in the navigation.
*
* All source code & content (c) Copyright 2006, 68 Classifieds
* unless specifically noted otherwise.
*
* @package 68classifieds
* @author Eric Barnes
* @copyright 68 Classifieds
* @link
http://www.68classifieds.com
* @$Revision: 1.1 $
* @Updated: $Date: 2007/01/08 19:12:22 $
*/
function smarty_function_categories_ul($params, &$smarty)
{
global $db;
$ul_attr = '';
$li_attr = '';
$show_total = 'N';
foreach ($params as $_key=>$_value)
{
switch ($_key)
{
case 'depth':
$$_key = (int)$_value;
break;
case 'ul_attr':
case 'li_attr':
case 'show_total':
$$_key = (string)$_value;
break;
}
}
$sSQL="SELECT id,name,parent_id,cOrder,cLink FROM ".PREFIX."categories WHERE parent_id=0 AND display<>'N' ORDER BY cORDER DESC, name ASC";
$result=$db->query($sSQL);
$output="<ul ".$ul_attr." class=\"menu\">\n";
while($row = $result->fetch())
{
$id=$row['id'];
$parent=$row['parent_id'];
if($row['cLink']<>'')
{
$link=$row['cLink'];
}
else
{
$link='category.php?type='.$id;
}
$output .= "<li ".$li_attr."><a href=\"".$link."\" class=\"topborder\">".$row['name']."</a>\n";
if($show_total=='N') {
$output.="( ". getTotal($id) .")";
}
//get sub categories
$sSQL2="SELECT id,name,parent_id,cOrder,cLink FROM ".PREFIX."categories WHERE parent_id=".$id." AND display<>'N' ORDER BY cORDER DESC, name ASC";
//$output.=$sSQL2."\n";
$result2=$db->query($sSQL2);
if($result2->size() > 0)
{
$output.="<ul class=\"submenu\">\n";
while($row = $result2->fetch())
{
$subid=$row['id'];
if($row['cLink']<>'')
{
$link=$row['cLink'];
}
else
{
if($subid>1)
{
$link='category.php?type='.$id.'&sec='.$subid;
}
else
{
$link='category.php?type='.$subid;
}
}
$output .= "<li ".$li_attr."><a href=\"".$link."\">".$row['name']."\n";
if($show_total=='Y') {
$output.="(". getTotal($subid) .")";
}
}
$output.="</a></li>\n";
$output.="</ul>\n";
}
$output.="</li>\n";
}
$output.="</ul>\n";
return $output;
}
function getTotal($cat)
{
global $db;
$sSQL="SELECT COUNT(*) AS total FROM ".PREFIX."products WHERE display = 'Y' AND expiration > NOW() AND section=".$cat;
$result=$db->query($sSQL);
$row = $result->fetch();
return $row['total'];
}
?>