如何在PHP中实现jQuery异步树视图?

时间:2010-02-01 14:10:23

标签: php jquery treeview

我想在PHP中集成异步树视图。现在我想知道如何使用PHP生成source php文件。

现在我的数据库结构是这样的:

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

现在,我想实现树视图。你对实施它有什么想法吗?

非常感谢。

补充问题:当我完成树视图时。如果我单击树视图中的项目,它将通过单击生成结果表。你知道怎么做吗?

但是,首先我应该完成树视图。

3 个答案:

答案 0 :(得分:1)

如果您希望使用jQuery完成此树视图,我建议使用this plugin。你只需要运行一个JS函数就可以了。你所有PHP部分要做的就是从db获取数据并生成一个jQuery插件可以转换为treeview的HTML。

答案 1 :(得分:1)

所以基本上你需要(伪代码):

$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);

现在很明显,当你经历每个循环时,你需要协助你的其他树属性。那么当你们所有人都完成了json_encode数组并回应它我需要的地方。至少那就是id如何工作呢。但请注意,你可以在没有使用某些连接的单个查询的情况下执行此操作,但我不想深入了解所有这些 - 如果性能完全是一个问题,你肯定会探索它。

答案 2 :(得分:0)

在实现树视图时,您可能会对此article/example code感兴趣。