Re: Help with counting total items using hierarchical data structure



On 31.07.2007 09:18 Neeper wrote:
I'm having trouble coming up with a query to count the number of total
items each subcategory with the lowest cost (CPU item).

I'd like to be able to display the total in my pages like so using
PHP.

Automotive (4 items)
- Cars (2 items)
- Trucks (2 items)
- Vans (0 items)
Electronics (6 items)
- Digital Cameras (0 items)
- Portable Audio (3 items)
- Home Theatre & Projectors (0 items)
- TV, DVD & VCR Players (3 items)
- Sound Systems & Components (0 items)

Getting the count a subitems is trivial with nested sets:

count of subnodes for some node = (rgt - lft) div 2

The problem is that "items" are not part of the tree (may I ask why?)

So, you need to get all categories first that are below the given category and then get items from those categories:

SELECT COUNT(*)
FROM items
INNER JOIN categories
ON items.category_id = categories.category_id
WHERE
categories.lft BETWEEN $CURRENT_LEFT AND $CURRENT_RIGHT

(untested)

BTW, this is a rather sql question than php, you'd probably get better answers in the mysql group.


--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
.



Relevant Pages

  • Re: Object Oriented Content System - the idea
    ... >I expected php to be smarter then that. ... >form like bytecode in memory for the next request. ... each action added some stuff to a "response" XML document. ... that map/object/data and formats it for HTML display using an includeed PHP ...
    (comp.lang.php)
  • Re: session trouble
    ... Note that you're building your display block AFTER your loop. ... <?php session_start; ... The problem here is going to be since you have multiple school clubs, it's going to be hard to relate a particular club to the entry in your database if it going to be changed. ... Another way is to add a suffix to each field, often times the primary key for the table. ...
    (comp.lang.php)
  • Re: Redirecting between PHP Pages
    ... but at the very least in a php script. ... > on the outcome of the validation, you want to do one of two things: ... with the various bits of HTML being ... Display login form ...
    (comp.lang.php)
  • Re: Open and process remote page
    ... William Hudson wrote: ... I have toyed with some of the php functions for opening URLs, ... > I need to fully emulate a browser, ... > display the remote page). ...
    (comp.lang.php)
  • Re: Redirecting between PHP Pages
    ... Display login form ... >>other php files, you should never need a redirect your page except to ... pass it on to smarty which then displays it as html for me. ...
    (comp.lang.php)