Recursive total of books in category view

Started by lsu, 11 August 2011, 14:59:37

Previous topic - Next topic
A financial contribution is greatly appreciated as a support, to help us to keep live the project.
If you like this project you can donate some piece of BitCoin to this address: bc1qy5tgq6tvrckac2a57unxvqcnxamrvhduve9sj9

lsu

Hello,

In category view we wanted to see the total of books in each category including its subcategories. Therefor I extended the default.php template files of the category and categories by removing this

(<?php echo $row->numero?>)

and adding this

(<?php 

$numero $row->numero;
$depth 0;
$parents[$depth] = $row->id;
/* Count the total number of books, including subcategories */
/* This method only works because the tree is sorted        */
foreach($rows as $rownum) {
$couldbeachild true;
while ($couldbeachild) {
/* Is it a children? */
if ($rownum->parent_id == $parents[$depth]) {
/* Yes, its a child, add the number of books*/
$numero += $rownum->numero;

/* Assume there is are child item(s) inside, increase depth... */
$depth++;
$parents[$depth] = $rownum->id;
break;
}
elseif ($depth == 0)
$couldbeachild false;
else
$depth--;
}
}
echo $numero;

 ?>
)


Would be cool if this code could be included into the upstream version. Also it may be necessary to add a option for this feature (should be easy, just put an if around the whole for loop)

Bye
Stefan

A financial contribution is greatly appreciated as a support, to help us to keep live the project.
If you like this project you can donate some piece of BitCoin to this address: bc1qy5tgq6tvrckac2a57unxvqcnxamrvhduve9sj9

federica

Hi Stefan,
thanks for your contribution.
I will add your code in the next version of abook.
Al mondo ci sono 10 tipi di persone, quelli che hanno capito il codice binario e quelli che non l'hanno capito.
[url=http://www.antivirushelp.it]http://www.antivirushelp.it[/url]
[url=http://www.disconnessi.org]stacca la spina![/url]