Page 1 of 1

Links In Category Help!!!

Posted: Fri Aug 22, 2003 1:51 am
by Mr. Tech
Hey!

I have a links directory and need to calculate the number of links in a category and it's subcaetgroies and so on.

I tried this:

Code: Select all

<?php
function links($cat) {
global $tbl_name;
$select_pcats = "select cat from ".$tbl_name."_cats where parent='$cat'";
$select_pcats_result = mysql_query($select_pcats) or die("<b>MySQL Error:</b> " . mysql_error());
$rows = mysql_num_rows($select_pcats_result);
for($c=0;$c<$rows;$c++) {
	$resrow = mysql_fetch_row($select_pcats_result);
	$pcatsid = $resrow[0];

	$linkscount = mysql_fetch_array(mysql_query("select count(*) as links from ".$tbl_name."_links where cat='$pcatsid'"));
	$otherlinks .= $linkscount[links] . '+';

	links($pcatsid);
	}
return $otherlinks;
}

$cat = "0";
$links = mysql_fetch_array(mysql_query("select count(*) as links from ".$tbl_name."_links where cat='$cat'"));
$links = $links[links] + $otherlinks;
links($cat);
?>
That's supposed to get the links in the first category, then select the amount of links in their sub categories and so on until I get all the links in the category and its categories.

Im not all that good with functions or adding numbers this way...

Any ideas?

Thanks

Posted: Fri Aug 22, 2003 5:56 am
by volka
how deep can your categories tree be nested?
only one level like
  • cat 1
    • cat 1.1
      cat 1.2
    cat 2
    • cat 2.1
    ...
or more levels, like
  • cat 1
    • cat 1.1
      • cat 1.1.1
      cat 1.2
    cat 2
    • cat 2.1
      • cat 2.1.1
    ...

Posted: Fri Aug 22, 2003 6:02 pm
by Mr. Tech
Unlimited. :D