views:

106

answers:

3

Hello,

I want to integrate an async treeview in PHP. Now i want to know how to generate a source php file with PHP.

Now my database structure is like this:

Create table School(
id int(10) NOT NULL AUTO_INCREMENT, 
name varchar(50), 
primary key(id)
);
Create table Class(
id int(10) NOT NULL AUTO_INCREMENT,
name varchar(50), 
school_id int(10), 
primary key(id), 
foreign key(school_id) reference School(id) on delete cascade
);
Student(
id int(10) NOT NULL AUTO_INCREMENT, 
name varchar(50), 
class_id int(10),
primary key(id), 
foreign key(class_id) reference Class(id) on delete cascade
);



School -> Class -> Student

Now, i want to realize the treeview. Do you have any ideas of implementing it?

Thanks a lot.

Additional question: When i finish the treeview. If i click the items in the treeview, it will generate a result table by clicking. Do you know how to do that?

However, firstly i should finish the treeview.

A: 

Hi garcon,

It looks like this article/example code might be of interest to you in implementing the tree view:

http://jquery-easyui.wikidot.com/tutorial:tree2

HTH,

-aj

AJ
Thanks, it seems to be a nice framework etmvc. But actually i'm using jQuery-treeview. http://jquery.bassistance.de/treeview/demo/async.html
garcon1986
+1  A: 

If you want this treeview to be done with jQuery I would suggest using this plugin. You just need to run one JS function and it will work. And all your PHP part has to do is get data from db and generate a HTML that jQuery plugin can transform into treeview.

RaYell
@RaYell, Thanks. I'm using this plugin, but i don't know how to generate a HTML. It's my problem now.
garcon1986
+1  A: 

So basically you need to (pseudo code):

$tree = array();
/**
 * the following line essentially executes the Query:
 * SELECT * from schools;
 * and returns all the rows in an array like 
 * $schools = Array(0=>array('id'=>1, 'name' => 'name'))
 */
$schools = $db->schools()->selectAll();

foreach($schools as $school)
{
   $schoolArr = array('text' => $school['name']);

   /**
    * Similar to the calls to school above except the query would be:
    * SELECT * from class where class.school_id = $school['id']
    * $school['id'] is the pk from the particular school record
    */
   $classes = $db->classes()->select("school_id = ?", $school['id']);
   $classesArr = array();
   foreach($classes as $class)
   {
      $classArr = array('text' => $class['name']);
      /**
       * Similar to the calls to school above except the query would be:
       * SELECT * from student where student.class_id = $class['id']
       * $class['id'] is the pk from the particular class record
       */
      $students = $db->students()->select('class_id = ?', $class['id']);
      $studentsArr = array();
      foreach($students as $student)
      {
          $studentsArr[] = array('text' => $student['name']);
      }

      $classArr['children'] = $studentsArr;
      $classesArr[] = $classArr;
   }

   $schoolArr['children'] = $classesArr;
   $tree[] = $schoolArr;
}

$jsTreeString = json_encode($tree);

Now obviously as you go through each loop you need to be assinging your other tree properties. then when youre all done just json_encode the array and echo it where i needs to be. At least thats how id work it. Note though, that you can probably do this without an individual query using some joins but i didnt want to get into all that - youll definitely wann explore that though if performance is at all an issue.

prodigitalson
@prodigitalson, Thanks, you're definitely right. But, i don't know how to do that? What does your second line mean? Have you used any frameworks yet?
garcon1986
The second line is jsut some pseudo code to get the info from the db... you didnt specify which db extension and/or ORM you were using so i just made up some pseudo stuff assuming youd supplement thos calls with whatever you normally use to access your db. ill ammend with some detail...
prodigitalson
@prodigitalson, thank you so much. ;)
garcon1986
@garcon: regarding your question on frameworks - I use Symfony, Zend, Agavi, and my own little mini framework depending onthe project. Most of my work is doen with Symfony or Zend. Normally i use Doctrine as my ORM in combination with one of those frameworks unless the data schema is super simple. In the past i used Propel - i actually still prefer Propel but it looks like Doctrine is the future so im trying to get more familiar.
prodigitalson
@prodigitalson, WOW, You're so good at PHP. I should learn from you guys. And it's the first time i heard of Propel or Doctrine. I've got a lot to learn.
garcon1986
@garcon: Im not like a superstar or anything but im `competent++` :-) Learning Propel/Doctrine isnt nessecarily an indicator of being good at php its just an indicator of being able to use those tools - same with frameworks. And its always better if you learn how to impleent things yourself first - then you have a better understanding of the language and whatever framework you use (or dont use).
prodigitalson
@prodigitalson, thanks for your suggestions. It's pretty pratical. ;) I'm learning to use PHP for half an year, and it's difficult for me to use PHP or Framework fluently. It will be a long journey for me to master these tools.
garcon1986
@garcon: pas de problème... votre anglais est meilleur que mon français.. ;-) (im assuming french from your handle... but we knwo what assuming does...)
prodigitalson
@prodigitalson, merci bien, lol ;) i think your french is pretty good.
garcon1986