- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为父子类别制作无序列表,如果有任何子类别,它将创建另一个无序列表(如缩进文本),以便用户可以正确理解。
我有 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>';
使用@vlcekmi3 回答修改代码后 https://stackoverflow.com/a/13451136/1053190我得到这个结果
现在如何从父列表中排除子类别?
最佳答案
对于您的设计,没有真正简单的解决方案。最有效的方法是添加像 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/
我正在尝试使用 jQuery 来提交一个基本的 Accordion 样式菜单。 这是菜单: http://www.cybart.com/bscg/ 这是为其提供 Accordion 功能的代码片段:
这是我的网站: http://www.simply-a-christian.com/index.html 在导航栏上,如果您移动到: 信念 玛丽安教义 您可以看到每个 Marian Dogma(列出了
这可能是一个很容易回答的问题,但是,我找不到解决方案。我已经建立了一个非常简单的菜单,每当我将鼠标悬停在 ul 父项上时,它的子项 ul ul 就会变得可见。基本上它是有效的,但 parent 让位于
这是一张图片: 问题是(你可以看到):“页面”按钮太低,无法点击。我想要的是它应该靠近“联系人”。 现在是代码: HTML Home About
我有一个这样的嵌套 ul 列表: Subcat Subcat2
如果标题不好理解,我来解释一下... 我有一个 css/jquery 切换菜单。按“+”号时它变大,按“-”号时它变小。我使几个元素的填充、边距和高度在切换/单击时发生变化。菜单顶部充满了链接,然后当
我正在尝试创建一个列表,该列表会在单击上/下时更改它的第一个元素。 This is what i want to do. 此代码运行良好。但问题是我也在我网站的主菜单中使用列表,并且这段代码也在我的主
我需要使用水平缩放的 ul > li > ul 的组合来创建一个菜单。每个 ul 都应该有一个 max-height 并在溢出时滚动。悬停在 li 上时,如果里面有另一个 ul 标签,它应该会在右侧打
我正在 ul 中编写 ul 来制作 Accordion 式菜单。但是当我在 html 验证器中检查下面的代码时,它给了我这些错误 Element ul not allowed as child of
我有两个列表,第二个列表是可排序的,我可以从第一个列表拖动到第二个列表,但不知道如何从第一个列表中删除。 如果我删除 helper: "clone" ,该项目 (li) 会被删除,但拖放操作效果不佳(
JSFiddle 问题:https://jsfiddle.net/td6szj3o/ /我有一个 ul li,每个 li 都有标准文本项。该列表是动态的,因此有时每个内容 li 小于其所在容器的 wi
我有两个元素 - 一个在另一个里面。问题是第二个元素显示在第一个 ul li 元素的顶部。 HTML: Home Merchants
我有一些结构如下的菜单: First level link 1 Child link 1 Child link 2
我有很长的ul,李的数目不是恒定的 1 2 3 4 5 6 7 8 9 我想使用Moovweb SDK将其分为两个ul。 1 2 3 4
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎偏离主题,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或 include a mini
我有一棵信息“树”,显示在许多嵌套列表中(ul li ul li ....)。两个问题: 当我点击时,父 ul 和子 ul 都会切换。 如果我点击在其中没有 ul 的嵌套列表中(例如,下面的 Cran
我正在尝试在 jQuery 中创建以下内容: 我有一个项目列表(例如 10 个)。如果项目数超过 6,则将剩余项目(从 6 到 10)复制到新的 UL 中以创建下拉菜单结构。 菜单的第一个版本是 ht
嘿大家 - 我在工作中尝试 jquery 的第二天,有点卡住了。 我有一个无序列表的链接..在无序列表的链接中 点击 ,我想要的 child 变得可见。 点击(文档上的任何位置)后,我想要 child
嗨,这就是我想要实现的目标。我有一个 ID="columns"的 div,在本例中我有 3 列,但可能会有更多或更少...我正在使用 JQuery 动态添加列表项。
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: Using .after() to add html closing and open tags 我想显示高度几乎相
我是一名优秀的程序员,十分优秀!