I have a classified script, and on the main page where it lists the categories, if there are any subcategories it puts how many there are next to the main category... for example:
Main Category
Another Main Category (2)
... The '2' is how many subcategories there is inside that main category. How would remove that, and instead of how many subcategories, how do I get it to put how many ads are in that category?
Here's the code on the main page:
//If $_GET['cat'] isn't set...
if(!isset($_GET['cat']) || $_GET['cat'] == '')
$_GET['cat'] = '0'; //Set it to 0. (First-level categories)
//If $_GET['Hot'] isn't set...
if(!isset($_GET['Hot']))
$_GET['Hot'] = 'false'; //Set it to an empty string. (Order by date, not views)
$cats = new CCategory($sql, $_GET['cat']); //Create the CCategory object that will represent the category listing
$backtrack = $cats->OutputCategoryBacktrack();
echo '<table width=100%><tr><td><img src="images/bar1.gif">';
foreach($backtrack as $out)
echo $out.'';
echo '</td></tr><tr><td class="blue_cell2">';
$output = $cats->OutputCategoryListing();
if(!$output)
echo 'Sorry. There are no sub-categories in this category. Please try another category.';
else
echo $output;
echo '</td></tr></table>';
And here is the code on CCategory.php:
<?php
class CCategory
{
var $Items; //array of objects in the category
var $cat; //TRUE if Items contains CCategory objects, FALSE if it contains CAd objects
var $sql; //Pointer to SQL class
function CCategory(&$sql, $category_id=0)
{
$this->sql = $sql;
if($category_id == '')
return TRUE;
$this->ID = $category_id; //Just in case LoadCategory() isn't called, throw the ID in here...
//If $category_id is 0, you're in the first-level category, which isn't technically a category
//So, you can't load information from the categories table for it.
if($category_id != 0)
$this->LoadCategory($category_id);
$this->LoadItems($category_id);
}
//Fill the $this->CItems array
function LoadItems($category_id)
{
$this->Items = array(); //Empty $this->Items to reload the ads/categories list
//Query
$this->sql->query("SELECT * FROM categories WHERE sub_cat = '%s'", $category_id);
//If there are categories within
if($this->sql->num_rows() > 0)
{
//$this->items contains CCategory objects
$this->cat = TRUE;
//Fill
foreach($this->sql->getrowset() as $cat)
$this->Items[] = new CCategory($this->sql, $cat['ID']);
return TRUE;
}
//If there are no categories within this one (i.e. there are items within it)
else
{
//$this->Items contains items
$this->cat = FALSE;
//Fill
foreach($this->sql->getrowset() as $item)
{
$tmp = new CAd($this->sql);
$tmp->LoadAd($item['ID']);
$this->Items[] = $tmp;
}
return TRUE;
}
}
//Load category information (ID / name)
function LoadCategory($category_id)
{
$this->sql->query("SELECT * FROM categories WHERE ID = '%s'", $category_id); //query
$rs = $this->sql->getrowset();
class_extract($rs[0], $this); //load
return TRUE;
}
//Output the HTML list of all categories under $from (0 = first-level.)
function OutputCategoryListing()
{
//If this category contains items, not categories...
if(!$this->cat)
return FALSE;
//Grab all first-level categories
$this->sql->query("SELECT * FROM categories WHERE sub_cat = '%s'", $this->ID);
//Start the table
$out = '<table width=100%>';
//The table will be NUM_CATEGORY_COLS columns wide. $x keeps track of the current column. When it hits NUM_CATEGORY_COLS, a new row is started.
$x = 0;
foreach($this->sql->getrowset() as $cat)
{
//If you've already written NUM_CATEGORY_COLS cols, go to the next row and reset $x
if($x == NUM_CATEGORY_COLS)
{
$out .= '</tr><tr>';
$x = 0;
}
//Check to see how many categories are in this category...
//This serves a dual purpose. First, we know whether we're going to another page
//of categories, or a page of items; second, we can output the number of categories
//in this category. (which is really not needed, but it looks nicer, and, while we're
//bugging MySQL anyway...)
$this->sql->query("SELECT ID FROM categories WHERE sub_cat = '%s'", $cat['ID']);
$num_sub_cats = $this->sql->num_rows();
//Show this category
$out .= '<td>';
$out .= '<a href="'.(($num_sub_cats == 0) ? 'show_ads.php?Hot='.$_GET['Hot'] : 'index.php?Hot='.$_GET['Hot']).'&cat='.$cat['ID'].'">';
$out .= $cat['name'].(($num_sub_cats != 0) ? (' ('.$num_sub_cats.')') : '');
$out .= '</a></td>';
$x ++;
}
$out .= '</table>';
return $out;
}
//Output the HTML for a list of categorys tracing from "All Categories" (0) to $this category
function OutputCategoryBacktrack()
{
//Query to get the entire categories table
$this->sql->query("SELECT * FROM categories");
//Dump the entier categories table into $cats using categories.ID as the index
$cats = $this->sql->getrowset('', 'ID');
//Until we're down to category 0, keep doing this
$cur_cat = $this->ID;
do
{
//If we're at cat 0, call it All Categories
if($cur_cat == 0)
$out[] = '';
//Otherwise, use its real name
else
$out[] = '<a href="index.php?cat='.$cur_cat.'">'.$cats[$cur_cat]['name'].'</a>';
//Backtrack one more level
$cur_cat = $cats[$cur_cat]['sub_cat'];
} while($cur_cat != NULL);
//Reverse the order of $out to get it to display correctly
return array_reverse($out);
}
//Output a 2D array of {categoryID, categoryName} for all bottom-level categories [Mainly for new/edit ad]
function OutputCategoryArray()
{
$list = array(); //Ready the returned variable
$this->sql->query("SELECT * FROM categories");
$categories = $this->sql->getrowset(); //Get a 2D array of all categories
foreach($categories as $category) $parents[] = $category['sub_cat']; $parents = array_unique($parents);
foreach($categories as $category)
{
//If it isn't a parent of any category
if(!in_array($category['ID'], $parents))
$list[] = array($category['ID'], $category['name']);
}
return $list;
}
/*
NOTE: I don't think I'll need category admin functions...
//Self-explanitory I should hope
function AddCategory($name, $sub)
{
$this->sql->query("INSERT INTO categories SET name = '%s', sub_cat = '%s'", $name, $sub); //Add the category to MySQL
}
//Rename $this category
function RenameCategory($name)
{
$this->sql->query("UPDATE categories SET name = '%s' WHERE ID = '%s'", $name, $this->ID);
}
//Delete $this category
function DeleteCategory()
{
$this->sql->query("DELETE FROM items WHERE category = '%s'", $this->ID);
$this->sql->query("DELETE FROM categories WHERE ID = '%s'", $this->ID);
$this->sql->query("UPDATE categories SET sub_cat = '' WHERE sub_cat = '%s'", $this->ID);
}*/
}
?>
Thanks for any help!! 🙂