gpt4 book ai didi

php - 如何在PHP中实现jQuery异步树形 View ?

转载 作者:行者123 更新时间:2023-12-01 04:31:24 26 4
gpt4 key购买 nike

我想在 PHP 中集成异步 TreeView 。现在我想知道如何生成source php使用 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

现在,我想实现 TreeView 。您有实现的想法吗?

非常感谢。

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

但是,首先我应该完成 TreeView 。

最佳答案

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

$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 并在我需要的地方回显它。至少这就是我的工作方式。但请注意,您可能可以在不使用某些连接的单独查询的情况下执行此操作,但我不想深入了解所有这些 - 尽管性能根本是一个问题,但您肯定会想要探索这一点。

关于php - 如何在PHP中实现jQuery异步树形 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2177201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com