Forum Moderators: coopster
thanks in advance
function cat_list() {
global $query_ext;
if (SHOW_TOTAL_ARTICLES == 1) {
$count_ext = ", COUNT(*) AS total_per_cat";
}
// get list of categories
$sql = "
SELECT * $count_ext
FROM pqdb_categories, pqdb_articles
WHERE category_id > 0
AND category_id = article_cat
$query_ext
GROUP BY category_id
ORDER BY category_name ASC";
$result = mysql_query($sql);
// are there any rows?
if (mysql_num_rows($result) < 1) {
$categories = "Oops! Something went wrong";
} else {
// loop through categories and make links
while ($row = mysql_fetch_array($result)) {
// format category names (get rid of "Information ...")
$take_out = array("& Information", "Information on", "Information");
$category_desc = str_replace($take_out, "", $row['category_desc']);
if (SHOW_TOTAL_ARTICLES == 1) {
$totals = "(" . $row['total_per_cat'] . " Articles)";
}
$categories .= "<li><a href=\"" . make_url('showarticles', $row['category_id'], $row['category_name']) . "\">$category_desc</a>$totals</li>";
}
}
return $categories;
}
I would approach the query like this:
SELECT * FROM pqdb_categories as c LEFT JOIN pqdb_articles as a
ON a.article_cat=c.category_id
....
rest of query here.
The Key being the LEFT JOIN, as apposed to a normal JOIN ... this will allow you to also select the categories that have no articles.
function cat_list() {
global $query_ext;
if (SHOW_TOTAL_ARTICLES == 1) {
$count_ext = ", COUNT(*) AS total_per_cat";
}
// get list of categories
$sql = "
SELECT * $count_ext FROM pqdb_categories as c LEFT JOIN pqdb_articles as a
ON a.article_cat=c.category_id
$query_ext
GROUP BY category_id
ORDER BY category_name ASC";
with the count_ext variable now it shows the categories but the ones that have no articles instead of putting 0 articles beside it says 1 article and when u click it shows none .