【问题标题】:sql queries results from parentssql查询父母的结果
【发布时间】:2012-09-19 03:52:47
【问题描述】:

需要一些帮助来完成我的代码它的作用是在我的数据库的表中搜索指向 parent_sections=2 的所有部分,然后在选择标记中打印结果我想要一些关于 sql 查询的帮助来检查如果 parent_sections=2 有孩子的,如果有的话,无论如何都要用 optgroup 样式打印它们,请检查下面的图片,他们解释了我需要什么。

PHP 和 SQL 代码:

<?php
include('../application_top.php');

function get_title_sections($id){
$sections_query = tep_db_query("select title_sections_detail from  sections_detail  where  id_sections = '" . (int)$id . "' and id_lang='0' order by title_sections_detail");
$result='';
    while ($sections = tep_db_fetch_array($sections_query)) {
 $result=$sections['title_sections_detail']; 
    }
    return $result;
}
?>


<form>
<select name="sections"> 
<option selected="selected" value="">Please choose an option</option>                           
        <?php   
        $sections_sql=tep_db_query("select p.id_sections, d.title_sections_detail as title from sections p, sections_detail d where  p.id_sections=d.id_sections and d.id_lang='0' and p.status_sections='1' and p.parent_sections='2'  order by d.title_sections_detail asc");         

    while ($sections = tep_db_fetch_array($sections_sql)) {
    $id_sec=$sections['id_sections'];
    $title_sec=get_title_sections($id_sec);
 ?>     
    <option value="<?php echo $id_sec ?>" ><?php echo $title_sec ?></option>

<?php }?>
</select>
</form>

SQL 表部分:

SQL 表sections_detail:

结果:

我需要的结果:

【问题讨论】:

  • 您需要使用&lt;optgroup&gt; 并使用另一个循环。

标签: php mysql sql


【解决方案1】:

这是我的解决方案:

<?php

include('../application_top.php');

function load_sections($id_parent) {
    $sections_sql = tep_db_query("select p.id_sections, d.title_sections_detail as title 
                                    from sections p, sections_detail d 
                                   where p.id_sections=d.id_sections 
                                     and d.id_lang='0' 
                                     and p.status_sections='1' 
                                     and p.parent_sections='".$id_parent."'  
                                   order by d.title_sections_detail asc");
    $sect = array();
    while ($sections = tep_db_fetch_array($sections_sql)) {
        $sections['childs'] = load_sections($sections['id_sections']);
        $sect[] = $sections;
    }
    return($sect);
}

function print_section($sect) {
    if (count($sect['childs'])) {
        echo "<optgroup label=\"".htmlentities($sect['title'])."\">\n";
        foreach ($sect['childs'] as $subsect) {
            print_section($subsect);
        }
        echo "</optgroup>\n";
    } else {
        echo "<option value='".$sect['id_sections']."'>".htmlentities($sect['title'])."</option>\n";
    }
}

?>
<!DOCTYPE html>
<html>
<body>
    <form>
        <select name="sections"> 
            <option selected="selected" value="">Please choose an option</option>                           
            <?php foreach (load_sections(2) as $sect) print_section($sect); ?>
        </select>
    </form>
</body>
</html>

注意不允许嵌套操作组;只允许一个 optgroup 级别,您也可以阅读here

【讨论】:

  • 嘿,感谢您的帮助,如果在一个 optgroup 级别之后需要嵌套以简单地添加空格而不是 optgroup?
  • @Ered 抱歉,我没有理解你的问题。请你解释清楚好吗?
  • 我知道只允许一个 optgroup 级别,但如果需要第二个级别,有什么解决方案吗?添加空格而不是 optgroup 或者我正在考虑而不是使用 optgroup 简单地使父母变得粗体并添加 margin-left:5px;给每个孩子。
  • 看看this answer
  • function print_section($sect) { if (count($sect['childs'])) { echo "&lt;option class=\"optionGroup\" value=\"".$sect['id_sections']."\"&gt;".htmlentities($sect['title'])."\n"; foreach ($sect['childs'] as $subsect) { print_section($subsect); } echo "&lt;/option&gt;\n"; } else { echo "&lt;option class=\"optionChild\" value='".$sect['id_sections']."'&gt;".htmlentities($sect['title'])."&lt;/option&gt;\n"; } }
猜你喜欢
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2011-11-01
  • 2015-09-04
  • 1970-01-01
  • 2022-06-21
相关资源
最近更新 更多