2012-09-18 120 views
0

需要一些帮助来完成我的代码它所做的是在我的数据库表中搜索指向parent_sections = 2的所有部分,然后将结果打印在选择标记中我想要一些帮助用于检查parent_sections = 2是否具有子项的sql查询,如果是,则使用optgroup样式打印它们,请检查下面的图像以解释我需要的内容。sql查询结果来自父母

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桌子部分: TABLE sections

SQL表sections_detail: TABLE sections_detail

结果:

RESULT

结果,我需要:

RESULT I NEED

+0

您需要使用''并使用另一个循环。 – Kermit

+0

添加父标记 – wesside

回答

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> 

注意,嵌套opgroups是不允许的;只允许一个optgroup级别,因为您也可以阅读here

+0

嘿感谢您的帮助任何解决方案,如果需要嵌套后一个optgroup级别简单地添加空间,而不是optgroup? – Ered

+0

@埃雷很抱歉,我还没有理解你的问题。你能解释一下吗? – 2012-09-18 20:13:05

+0

我明白只有一个optgroup级别是允许的,但如果需要第二个级别,是否有解决方案?添加空格,而不是optgroup,或者我在想,而不是使用optgroup简单地让父母粗体并添加一个margin-left:5px;给每个孩子。 – Ered