//pid 父级id result数据存储数组 spac子级前缀数量
function getlist($pid=0,&$result=array(),$spac=0)
{
//调用数据库连接方法
$db = SingleCase::getInstance();
$spac += 2;
$sql = "SELECT * FROM recursion WHERE Pid = $pid";
$res = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
foreach ($res as $key => $value) {
$value['catename'] = str_repeat(' ',$spac)."|--".$value['catename'];
$result[] = $value;
getlist($value['id'],$result,$spac);
}
return $result;
}function displayCate($showid)
{
$data = getlist();
$str .= "<select name='cate'>";
foreach ($data as $key => $value) {
$selected = '';
if ($value['id'] == $showid) {
$selected = "selected";
}
$str .= "<option {$selected}>{$value['catename']}</option>";
}
return $str .= "</select>";
}$showid = $_GET['pid'];
echo displayCate($showid);
结果示例图片
//cid 上一级主id result返回数据的数组
function getCateLink($cid,&$result=array())
{
$db = SingleCase::getInstance();
$sql = "SELECT * FROM recursion WHERE id = $cid";
$data = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
foreach ($data as $key => $value) {
$result[] = $value;
getCateLink($value['pid'],$result);
}
krsort($result);
return $result;
}function displayLink($cid)
{
$data = getCateLink($cid);
foreach ($data as $key => $value) {
echo "<a href='recursion.php?pid={$value['id']}'>{$value['catename']}</a> >";
}
}displayLink(10);
结果示例图片