开发者

How to realize jQuery async treeview in PHP?

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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜