gpt4 book ai didi

php - 如何获取子/子创建新 UL 的查询

转载 作者:行者123 更新时间:2023-11-29 02:30:49 25 4
gpt4 key购买 nike

我正在尝试为父子类别制作无序列表,如果有任何子类别,它将创建另一个无序列表(如缩进文本),以便用户可以正确理解。

我有 fetch sql,但是对于 foreach,我不知道如何通过在父类别下创建另一个未排序列表来设置子类别仅显示在父类别下的位置。

这是我的代码

$query_cat =    "SELECT * FROM ^categories";
$query = qa_db_query_sub($query_cat);
$catsid = qa_db_read_all_assoc($query);

echo '<UL>';
foreach ($catsid as $catid){
echo '<LI>'. $catid['title'].' '. $catid['categoryid'].'</LI>';
}
echo '</UL>';

The table image for the category

所以最终结果是

  • 第一类
    • 子类别 1
  • 第二类

编辑:

使用@vlcekmi3 回答修改代码后 https://stackoverflow.com/a/13451136/1053190我得到这个结果

enter image description here

现在如何从父列表中排除子类别?

最佳答案

对于您的设计,没有真正简单的解决方案。最有效的方法是添加像 order_in_list 这样的列(也许还有 depth_in_list)。

它们将在循环中预先计算(伪代码):

START TRANSACTION
UPDATE t1 SET order_in_list = 0 // Restart whole loop

$ids = array(0);

while $id = array_shift($ids){
$record = SELECT * FROM t1 WHERE id = $id // Get id details, order_in_list is important
$children = SELECT * FROM t1 WHERE parent_id = $id // get list of all childs
// If it's root element, start indexing from 0
$root_order = ($record ? $record->order_in_list : 1)
$child_no = count($children) // How many child will be adding

// No children, nothing to do:
if $child_no < 1{
continue;
}

append_to_array($ids, $children) // Store ids to process

// Shift all later records, we'll be creating gap in order_in_list 1,2,3,4,5
// To 1,2,5,6,7 to insert items on places 3,4
UPDATE t1 SET order_in_list = (order_in_list + $child_no)
WHERE order_in_list > $record->order_in_list

// Okay, set IDs for direct children
foreach( $children as $child){
UPDATE t1 SET order_in_list = $root_order, depth_in_list = $record->depth_in_list+1
WHERE id = $child->id
$root_order++;
}
}
COMMIT

这样你会得到如下记录:

First category, 1, 1
Second category 3, 1
Sub category, 2, 2

你可以用简单的循环显示:

$last_depth = 0;
foreach( (SELECT * FROM t1 ORDER by `order_in_list`) as $row){
if( $last_detph > $row['depth_in_list'])){
// Close level </ul>
} else if($last_detph < $row['depth_in_list']){
// Opening level <ul>
} else {
// The same depth
}
$last_depth = $row['depth_in_list'];
}


不修改数据库

构建两个包含根元素和所有元素的数组可能是最有效的:

$root_elements = array();
$all_elements = array();

foreach( (SELECT * FROM t1) as $row){
// Store details into all_elements, note that entry may have already be created when
// processing child node
if( isset( $all_elements[$row['id']])){
// set details
} else {
$all_elements[$row['id']] = $row;
$all_elements[$row['id']]['children'] = array(); // Array of child elements
}

if( $row['parent_id'] == NULL){
$all_elements[] = $row['id']; // Add row element
} else {
if( isset( $all_elements[ $row[ 'parent_id']])){
$all_elements[ $row[ 'parent_id']]['children'][] = $row['id'];
} else {
// Create new record:
$all_elements[ $row[ 'parent_id']] = array();
$all_elements[ $row[ 'parent_id']]['children'] = array($row['id']);
}
}
}

然后写成:

foreach( $root_elements as $element_id){
write_recursive( $all_elements[ $element_id]);
}

// And display
function write_recursive( $element)
{
echo '<ul>...';
if( count( $element['children'])){
foreach( $element['children'] as $child){
write_recursive( $all_elements[ $child]);
}
}
echo '</ul>';
}

你最好为此创建类(以使用全局变量代替),但你应该有一个可靠的方法来做到这一点。无论如何尽量避免将它用于大量记录(我不会超过 2000-5000 个菜单条目),尝试至少缓存它。

注意:解决方案旨在显示列表时对数据库的请求数量最少。

关于php - 如何获取子/子创建新 UL 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13450839/

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