1 Star 0 Fork 32

web小义/家谱familytree

forked from jiehu0992/家谱familytree 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bttype.php 2.06 KB
一键复制 编辑 原始数据 按行查看 历史
jiehu0992 提交于 2023-04-21 15:01 . 宝塔样式核心代码php7.4
<?php
// Database connection using mysqli
$mysqli = new mysqli("127.0.0.1","root","root", "Database");
if ($mysqli->connect_errno) {
die("Failed to connect to MySQL: " . $mysqli->connect_error);
}
// Prepare statement
$sql = "SELECT * FROM tree_lr";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
// Fetch results into an array
$array = array();
while ($row = $result->fetch_assoc()) {
$array[] = $row;
}
// Optimize the lefttree() function
function lefttree($array){
$tree = array();
$categorylist = "";
foreach ($array as $v) {
$list = @$tree[$v['pid']] ?: array();
$list[] = $v;
$tree[$v['pid']] = $list;
}
if (is_array($tree[0])) {
$categorylist = "<ul>\n";
$categorylist .= sonTree($tree[0], $tree);
$categorylist .= "</ul>\n";
}
return $categorylist;
}
function sonTree($arr, $tree, $level = 1) {
$categorylist = "";
foreach ($arr as $k => $v) {
if ($tree[$v['id']]) {
$categorylist .= "<li><a href=\"#\" style='color: ";
$categorylist .= ($v['sex'] == '女') ? '#ff1493' : 'black';
$categorylist .= ";'>{$v['name']}</a>\n";
$categorylist .= "<ul>\n";
$categorylist .= sonTree($tree[$v['id']], $tree, $level + 1);
$categorylist .= "</ul>\n";
$categorylist .= "</li>\n";
} else {
if ($v['is_link'] == 0) {
$categorylist .= "<li><a href=\"#\" style='color: ";
$categorylist .= ($v['sex'] == '女') ? '#ff1493' : 'black';
$categorylist .= ";'>{$v['name']}</a></li>\n";
}
}
}
return $categorylist;
}
// Generate the menu
$menu = lefttree($array);
if ($link && mysqli_ping($link)) {
mysqli_close($link);
}
?>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
PHP
1
https://gitee.com/cfx123000/genealogy_familytree.git
git@gitee.com:cfx123000/genealogy_familytree.git
cfx123000
genealogy_familytree
家谱familytree
master

搜索帮助