2012-09-18 152 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