Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-29-2012, 11:19 AM   PM User | #1
darksecu
New Coder

 
darksecu's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 95
Thanks: 11
Thanked 1 Time in 1 Post
darksecu is an unknown quantity at this point
Tree Structure

I want to learn how to make a basic tree structure with php and mysql ..

can some one post a sample of table and php code ?

Thanks,
Rahul
darksecu is offline   Reply With Quote
Old 06-29-2012, 11:36 AM   PM User | #2
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,942
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
what type of tree ? MPTT I use MPTT structures and its a love-hate relationship
what do you want to do exactly ?
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 06-29-2012, 11:47 AM   PM User | #3
darksecu
New Coder

 
darksecu's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 95
Thanks: 11
Thanked 1 Time in 1 Post
darksecu is an unknown quantity at this point
Quote:
Originally Posted by firepages View Post
what type of tree ? MPTT I use MPTT structures and its a love-hate relationship
what do you want to do exactly ?
First Of All Thanks For Making A Quick Reply.

Never Heard Of MPTT, How That Works ?
I am looking for some way to make a tree of referrals joining under users.

so if there are 5 user referred by a user ABC
and 5 user referred by the referrals of ABC.

i want all of them to appear in a tree structure ..

Thanks Again,
Rahul
darksecu is offline   Reply With Quote
Old 06-29-2012, 12:01 PM   PM User | #4
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,942
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
I don't like pointing you off site but its hard to explain without a diagram , but have a quick look here , esp at the diagram since the implementation is not quite pure MPTT .

It also talks about adjacency lists which is the other option (though I have head MPTT called adjacency tables as well (I dunno))

essentially when you have your tree built its really easy to pull hierarchical trees from the structure though there are gotchyas as well.
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 06-30-2012, 05:26 AM   PM User | #5
darksecu
New Coder

 
darksecu's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 95
Thanks: 11
Thanked 1 Time in 1 Post
darksecu is an unknown quantity at this point
Tried To Process The Codes But Didn't Got Echo of parent and child on page.

this is what i did ..
PHP Code:
$parent='1';
$level='0';
function 
display_children($parent$level) {
$result mysql_query("SELECT * FROM tree WHERE parent='$parent'"); 
while (
$row mysql_fetch_array($result)) {
$chidrenstr_repeat('   ',$level).$row['child']."\n";
echo 
$children;
display_children($row['child'], $level+1); 
}} 
I Don't have any idea what i am doing wrong in this :-?
darksecu is offline   Reply With Quote
Old 06-30-2012, 06:12 AM   PM User | #6
firepages
Super Moderator


 
Join Date: May 2002
Location: Perth Australia
Posts: 3,942
Thanks: 7
Thanked 82 Times in 81 Posts
firepages will become famous soon enough
so sorry, my bad, but you have to go to page 2 of the article http://www.sitepoint.com/hierarchical-data-database-2/ to see real(ish) MPTT tables with a left and right value.

with the structure on page 2 you dont have to make recursive queries or functions to pull the data, so for your function above it would be like

SELECT * FROM $table WHERE `$right` BETWEEN $parent_left AND $parent_right ORDER BY `$left`
__________________
resistance is...

MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
firepages is offline   Reply With Quote
Old 06-30-2012, 07:14 AM   PM User | #7
darksecu
New Coder

 
darksecu's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 95
Thanks: 11
Thanked 1 Time in 1 Post
darksecu is an unknown quantity at this point
Doh !! I am Total Noob, cant get any of them to work ..
PHP Code:
include 'database.php';
$root='Food';
function 
display_tree($root) { *
--->> 
$result=mysql_query("SELECT lft,rgt FROM tree WHERE title=$root"); <<---
$row=mysql_fetch_array($result);
$right = array();
$result mysql_query("SELECT title, lft, rgt FROM tree WHERE lft BETWEEN $row[lft] AND $row[rgt] ORDER BY lft ASC");
 * *while (
$row mysql_fetch_array($result)) {
 * * * *if (
count($right)>0) {
 * * * * * *while (
$right[count($right)-1]<$row['rgt']) {
 * * * * * * * *
array_pop($right);
 * * * * * *}
 * * * *}
 * * * *echo 
str_repeat(' *',count($right)).$row['title']."\n";
 * * * *
$right[] = $row['rgt']; *
 * *} *

PHP Code:
include 'database.php';
$parent='Food';
$level='0';
function 
display_children($parent$level) {
$result mysql_query("SELECT * FROM tree WHERE rgt BETWEEN 1 AND 18 ORDER BY lft ASC"); 
while (
$row mysql_fetch_array($result)) {
echo 
str_repeat('   ',$level).$row['title']."\n";
display_children($row['title'], $level+1); 
}} 
Restructured table as same on article 2 with same inputs ...

when i tried first code for error on 4th lines, marked there..
when tried second code as you said, no output on page ...

~Edit~ It Seems Like function variable() giving no out put...

any way to check that .. ??

Last edited by darksecu; 06-30-2012 at 08:15 AM..
darksecu is offline   Reply With Quote
Old 06-30-2012, 05:20 PM   PM User | #8
darksecu
New Coder

 
darksecu's Avatar
 
Join Date: Dec 2011
Location: India
Posts: 95
Thanks: 11
Thanked 1 Time in 1 Post
darksecu is an unknown quantity at this point
Thought of many things and found that i cant ryt and lft table structure the way i want ..
actually i need something like which you displayed on first page of article ..

but when i execute that php,sql no output is coming, just a blank page ..
darksecu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 10:20 PM.


Advertisement
Log in to turn off these ads.