Links In Category Help!!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Links In Category Help!!!

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
    ...
Mr. Tech
Forum Contributor
Posts: 205
Joined: Tue Feb 11, 2003 4:18 pm
Location: Australia

Post by Mr. Tech »

Unlimited. :D
Post Reply