Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Hierarchy using MySQL and PHP
Old 04-05-2006, 01:45 PM Hierarchy using MySQL and PHP
Experienced Talker

Posts: 47
Trades: 0
I am trying to write a hierarchy systme to use for a database at the company I work for. I am going by this guide:
http://dev.mysql.com/tech-resources/...ical-data.html

Right now I have set the lft and rgt columns for about a little over 600 people on our database, the numbers seem to be fine. The only problem is when I try to make a downline report for some reason it doesn't seem to work.

There is 2 problems with it, first it skips the first generation, and secondly the generation (depth) numbers don't always show up right.

The query i am using is as follows:
Code:
SELECT node.*, (COUNT(parent.name) - (sub_tree.depth + 1)) AS gen
FROM ' . $table . ' AS node,
    ' . $table . ' AS parent,
    ' . $table . ' AS sub_parent,
    (
        SELECT node.id, (COUNT(parent.name) - 1) AS depth
        FROM ' . $table . ' AS node,
        ' . $table . ' AS parent
        WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.id = ' . $parent . '
        GROUP BY node.name
        ORDER BY node.lft
    )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
    AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
    AND sub_parent.id = sub_tree.id
GROUP BY node.name
ORDER BY node.lft;
You can see a sample of what it prints out here:
https://dev.mywfm.com/tree.php

The fields are gen, last, first, code to id this person, L: left value, R: right value

A dump of everyone by generation (not using this method) can be found here:
https://dev.mywfm.com/dl.php
This one was generated using the parent method that was incorporated at first but we are trying to move to a more efficient method.

The table looks as follows:

Table: associate
Fields: id, code, name, last, <other personal fields>, sponsor_code, lft, rgt
id is an auto_increment id
code is a code given by the company to each associate
name and last are first and last names
sponsor_code is how the old system was running, sponsor_code basically was the code of the parent for this associate
lft and rgt are the columns for the hierarchy system that we are trying to incorporate.

Does anyone have any idea/suggestions or knows how to fix the problem? I would really appreciate some help as I've been looking at this query for hours trying to figure out why it is doing that.

Thanks a bunch!
__________________

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
Synchronize is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-08-2006, 12:49 PM Re: Hierarchy using MySQL and PHP
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
Ouch... that looks complicated...

I've not taken much of a look at your query above, but have you made sure the database you are querying is in a consistent state, ie the lft and rgt values are all correct? Your select query could be fine but there may be a problem with your inserts and updates that make the generation numbers come out wrong.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Reply     « Reply to Hierarchy using MySQL and PHP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.22639 seconds with 12 queries