Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 08-25-2002, 06:41 AM   #16 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
mmilano, I understand your frustration. I'd agree that abc123's processNewCat function is a good example of recursion. If you decide to dig further at some later date, do a search on php and recursion. That seems to be the snag.

My lengthy ramble was based on a java app I wrote to construct a navigation sitemap for a large website that was moving to a content management system. PHP is not my strong point, so there may be differences that require a slightly different solution. Anyway, now you've got me curious as to whether I can do this in PHP. I'm going to try it in my spare time. If I get it to work, I'll share the results. It may not be in time to help you now, but maybe for some future project.
technobard is offline   Reply With Quote
Old 08-28-2002, 06:50 PM   #17 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
Working Code

I have written a solution based on my earlier outline. As Scott pointed out, there are other ways of doing this. This is a single parent model. It can be extended to include multiple parents with some (what I believe to be) minor modifications.

I learned a few things about references (not quite pointers) in PHP along the way. I may write this up as a NUM.

If anyone has any questions in the meantime, let me know.

Code:
**** sample main loop for building and printing a tree structure
**** include file to follow for Hierarchy class

<?
include "hierarchy.inc";
$username = "user";
$password = "pass";
$databasename = "test";

$link = mysql_connect($hostname, $username, $password);
mysql_select_db($databasename, $link);

$stmt = "select id, parent_id, description from hierarchy";
$result = mysql_db_query($databasename, $stmt);

$object = array();  /* lookup table of references to objects */

//$object["0"] = new Hierarchy(0);   -- will be added automatically
//$object["0"]->setDesc("root");

while ($row = mysql_fetch_object($result)) {
   if (is_null($object["$row->id"])) {   /* if object doesn't already exist  */
      $tmp_obj = &new Hierarchy($row->id);
   } else {
       $tmp_obj = &$object["$row->id"];     /* lookup existing object */
   }
      $tmp_obj->setParent_id($row->parent_id);
      $tmp_obj->setDesc($row->description);

      $object["$row->id"] = &$tmp_obj;    /* add object reference to lookup
table  */

   if (is_null($object["$row->parent_id"])) {   /* if parent doesn't already exist,
create it */
      $tmp_obj2 = &new Hierarchy($row->parent_id);
      $object["$row->parent_id"] = &$tmp_obj2;
   } else {
      $tmp_obj2 = &$object["$row->parent_id"];
   }

   $tmp_obj2->addChild($row->id);

}  /* end of while loop to load data */

mysql_free_result($result);   /* free resultset */

/* print entire tree starting at root node - 0 */

$object[0]->printChildren(0, $object);


?>
*** include file "hierarchy.inc" ***
The Hierarchy Class (each node with a list of it's children)
Code:
<?

class Hierarchy {
   var $id;
   var $parent_id;
   var $description;
   var $children;
   var $level;

   function Hierarchy($new_id) {
      $this->setId($new_id);
      $this->children = array();
   }


   function setId($new_id) {
      $this->id = $new_id;
   }

   function setParent_id($newParent_id) {
      $this->parent_id = $newParent_id;
   }

   function setDesc($desc) {
      $this->description = $desc;
   }

   function addChild($child_id) {
      $this->children[] = $child_id;
   }

   function printDesc($num_tabs) {
      $tab = "&amp;nbsp; &amp;nbsp; &amp;nbsp;";
      
      for ($i=1; $i<=$num_tabs; $i++ ) {
         $tabs = $tabs.$tab;
      }
      print $tabs.$this->description;
      print "<BR>";
   }

   function printChildren($tree_lvl, &$hashtab) {
      /* loop through array of $children, print $description, */
      /* call printChildren(); stop when all children are processed */
      
     $this->level = $tree_lvl;

     foreach ($this->children as $child) {
         $hashtab["$child"]->printDesc($this->level + 1);
         $hashtab["$child"]->printChildren($this->level + 1,$hashtab);
     }
   }

}

?>
technobard is offline   Reply With Quote
Old 08-28-2002, 07:28 PM   #18 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
hey thanks a lot! i was planning on taking a break from coding for the week, but i am starting to think i will have to try this out!

i'll will for sure let you know if i have some questions.

thanks again =)
sde is offline   Reply With Quote
Old 08-29-2002, 05:49 AM   #19 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
Hey, I just noticed that I lost something from the code in posting it. In the Hierarchy class, $tab should look like:

function printDesc($num_tabs) {
$tab = "&amp;nbsp; &amp;nbsp; &amp;nbsp;";
....
...


I'll also change it in the original post.
technobard is offline   Reply With Quote
Old 09-05-2002, 08:38 AM   #20 (permalink)
technobard
Centurion Nova Prime
 
technobard's Avatar
 
Join Date: May 2002
Location: Oak Park, IL (USA)
Posts: 287
technobard is on a distinguished road
Hey, Mmilano.

I know I'm off on a tangent here, but I was wondering if you had done any XML using PHP? Assuming you had a good routine for building the XML document, you could navigate it using the DOM methods. Using XML is probably more overhead than what I wrote ( ), but I thought this would be an interesting way of handling the problem.

I still prefer java for this sort of thing. You can use javabeans to load your tree into the session so that you only have to read it from disk once. (assuming you're not updating it in your app) Just a little java plug. Speaking of which, I should start on that NUM I wanted to write.
technobard is offline   Reply With Quote
Old 09-05-2002, 09:14 AM   #21 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,505
sde is on a distinguished road
i've only read xml documents with php.. i haven't generated any , although it wouldn't be that difficult. putting xml in the middle seems like it would slow things down a bit.

sorry i haven't even tried this script yet.. i am working on another system project and i'm in the process of moving. i do need to get my online store going again soon.. i will keep you posted.

also, i have don't know anytthing about java.. i tried once a long time ago, but i don't think java would be an option for me.

thanks
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 05:37 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting