To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 01-06-2004, 09:00 PM   #1
thoand
Senior Member
 
Join Date: Mar 2002
Posts: 338
Tree script

Hi here are a litle recursive script that builds a tree like:

name
-name
--name
---name
-name
--name
-name
--name
---name

... and so on.

It takes data from a db structure like this

id int(9) auto_increment ... uniqe id
name char(255) ... name to be printed
_parent int(9) ... holds the parent id (id)


PHP Code:
<?
$link
= mysql_connect("localhost", "", "");
mysql_select_db("test");
?>


<?
function buildTree($id = 0, $cnt = 1) {
    
$query = "SELECT * FROM names WHERE _parent = ".$id."";
    
$result = mysql_query($query);
    while (
$row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo
str_repeat("*",$cnt);
        echo
$row['name']."<br>";
        
buildTree($row['id'], $cnt + 1);
    }
}
?>


<?
buildTree
();
?>
Ypu can ofcourse modify it to suit your needs,
what do you think?

best regards
Thomas A
thoand is offline   Reply With Quote
Old 01-06-2004, 09:01 PM   #2
thoand
Senior Member
 
Join Date: Mar 2002
Posts: 338
and ofcours

all top level names has to have 0 as _parent
thoand is offline   Reply With Quote
Old 01-07-2004, 12:59 AM   #3
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
set the query to
PHP Code:
"SELECT * FROM names WHERE _parent in ( ".$id.",null)"
and you can get around that.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 01-07-2004, 01:03 AM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
and it'd be nice to be able to specify what character to use instead of "*" all the time...
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 01-07-2004, 01:26 AM   #5
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Moonglobe
and it'd be nice to be able to specify what character to use instead of "*" all the time...
Like, select the fields you actually want?
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 01-07-2004, 01:27 AM   #6
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
that'd be nice too, but i meant that you could use different chars for indenting.... maybe per-level if you passed an array, so it would rotate.
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 01-07-2004, 07:35 AM   #7
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
Quote:
Originally posted by Moonglobe
that'd be nice too, but i meant that you could use different chars for indenting.... maybe per-level if you passed an array, so it would rotate.
More flexible perhaps, a <span> style based on the depth of node ( "treelevel1", "treelevel2", ... etc.).
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 01-07-2004, 09:43 AM   #8
thoand
Senior Member
 
Join Date: Mar 2002
Posts: 338
thanks

Hi folks,
thanks for the input,

best regards,
Thomas
thoand is offline   Reply With Quote
Old 01-08-2004, 02:57 AM   #9
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
I was bored so attached you'll find a script that will test to see if your environment can run and the script and if not configure your environment to do so.

the changes are listed here:
PHP Code:
/* CHANGE LOG
    0) Changed _parent column to parent column - I made a boo boo on table creation
       and this change was easier then that fix.
    1) Add parameter $idnt to allow the user to specify indentation
    2) Changed default $cnt to 0 so that user is not forced into having an
       indent on the root level
    3) added mysql_free_result to clean up after self
    4) added parameter checking
        a) added parameter $running so we know if we have to do parameter checking
    5) return an array instead of echoing the tree to allow more flexibility
*/
function buildTree($id = 0, $cnt = 0, $idnt = '*', $running = 0) {
    
    
//if we are not in a recursive loop
    
if(!$running || !is_numeric($running)) {
        if(!
is_numeric($id)) {return -1;}
        if(!
is_numeric($cnt)) {return -1;}
        if(!
is_numeric($cnt)) {return -1;}
    }
//end if
    
    
$retVal = array();
    
    
$query = "SELECT * FROM names WHERE parent = ". $id;
    
$result = mysql_query($query);
    while (
$row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        
array_push($retVal,(str_repeat($idnt,$cnt) . $row['name']));
        
$retVal = array_merge($retVal,buildTree($row['id'], $cnt + 1,$idnt,1));
    }
    
mysql_free_result($result);
    return(
$retVal);
}
//end build tree
Attached Files
File Type: zip reec_tree_builder.zip (1.8 KB, 120 views)
drawmack is offline   Reply With Quote
Old 01-08-2004, 06:44 AM   #10
thoand
Senior Member
 
Join Date: Mar 2002
Posts: 338
Cool

Thanks for the interest,
but one question,
I thought mysql_free_result had no effect anymore, that PHP always cleans up.

Thomas
thoand is offline   Reply With Quote
Old 01-08-2004, 10:06 AM   #11
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
never trust someone else's code to clean up after yours
drawmack is offline   Reply With Quote
Old 01-09-2004, 08:08 AM   #12
Shrike
Not Yet Involved
 
Shrike's Avatar
 
Join Date: Oct 2003
Location: The Eighth, Sursamen
Posts: 2,254
Re: Cool

Quote:
Originally posted by thoand
Thanks for the interest,
but one question,
I thought mysql_free_result had no effect anymore, that PHP always cleans up.

Thomas
PHP frees results when script execution has ended, mysql_free_result() can free up the memory beforehand, useful if you have a huge recordset slowing everything down.
Shrike is offline   Reply With Quote
Old 12-21-2005, 01:40 PM   #13
tokeiito
Junior Member
 
Join Date: Aug 2002
Posts: 2
nice code, i've wrote smth like that too, but it's alitle bit longer

but now i have problem with identifying subNode's. exmpl:

node
-node
--node
-node
node
-node
--node
---node
----node

and so on...

my dhtml meniu use <ul><li></li></ul> for node. exmpl:

<ul>
<li>node</li>
<li>node
<ul>
<li>node2</li>
</ul>
</li>
</ul>


and know i'm stuck. how to identify where is node with subNode or, subNode with subSubNode and so on...
tokeiito is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes 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 Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:45 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.