You can have a look at the site here:
http://eveythingfree.buildtolearn.net/classifieds
Disregard the silly catagory names, I was just trying to fill up as much space as possible! As you can see the script never generates more than two columns (on the left hand side) leaving too much space on the right.
Maybe this will make it clearer what I'm trying to do.
Thanks, Robert
Hopefully the script shall be clearer now:
[php]// ================================================== ==========
// generates the Yahoo style table with all first and second
// level categories with number of listings and number of new
// listings
// ================================================== ==========
//
function generate_home_category_list($portal) {
global $BBClass_verb;
$now = date("Y-m-d H:i:s");
$query = "SELECT
classifieds_categories.*,
COUNT(classifieds_listings.listing_id) AS item_count
FROM
classifieds_categories
LEFT JOIN
classifieds_listings ON
((classifieds_categories.category_id=SUBSTRING(cla ssifieds_listings.category_id,1,2) OR
classifieds_categories.category_id=SUBSTRING(class ifieds_listings.category_id,1,4)) AND
classifieds_listings.status=20 AND classifieds_listings.exp_date >= '$now')
WHERE
(classifieds_categories.display=1 AND
(classifieds_categories.category_id LIKE '__' OR classifieds_categories.category_id LIKE '____'))
GROUP BY
classifieds_categories.category_id
ORDER BY
classifieds_categories.category_name, classifieds_categories.category_id";
$this->db->query($query);
if ($this->db->num_rows() > 0) {
$output = '<table border="0" cellspacing="0" cellpadding="10" width="100%"><tr>' . "\n";
// fetch the data
while ($this->db->next_record()) {
$category_id = $this->db->f('category_id');
$category_name = $this->db->f('category_name');
$item_count = $this->db->f('item_count');
$parent = substr($category_id,0,2);
$is_parent = (strlen($category_id) == 2) ? 1 : 0;
if ($is_parent) {
$parent_categories[$category_id] = array( "category_name" => $category_name,
"item_count" => $item_count);
} else {
$child_categories[$parent][$category_id] = array( "category_name" => $category_name,
"item_count" => $item_count);
}
}
// build the content
$i=0;
reset($parent_categories);
while(list($key, $val) = each($parent_categories)) {
$output .= '<td align="left" valign="top" width="50%"><span class="cfmaincat"><b>' . "\n";
$output .= '<a class="cfmaincat" href="'.$portal.'?cf_action=list&category_id='.$ke y.'">'.$val["category_name"].'</a>';
if ($val["item_count"] > 0 || $this->show_zero_category_count) {
$output .= ' ('.$val["item_count"].')';
}
$output .= '</b></span>';
if ($this->show_children_at_home) {
$output .= '<br /><span class="cfsubcat">' . "\n";
$this_child = $child_categories[$key];
reset($this_child);
$j=0;
while(list($key, $val) = each($this_child)) {
if ($j > 0) $output .= ', ';
$output .= '<a class="cfsubcat" href="'.$portal.'?cf_action=list&category_id='.$ke y.'">' . $val["category_name"] . '</a>';
if ($val["item_count"] > 0) {
$output .= ' ('.$val["item_count"].')';
}
$j++;
}
}
$output .= '</td>';
if ($i % 2) {
$output .= '</tr><tr>' . "\n";
$add_td = false;
} else {
$add_td = true;
}
$i++;
}
if ($add_td) $output .= "<td> </td>";
$output .= '</tr></table>';
} else {
$output = '<p class="bbclasserror">There are no categories defined yet.</p>';
}
return $output;
}[\php]