0

私のコードを完成させるのに助けが必要です。私のDBのテーブルでparent_sections = 2を指すすべてのセクションを検索し、その結果をselectタグに出力します。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 テーブル セクション: TABLE セクション

SQL テーブルのセクションの詳細: TABLE section_detail

結果:

結果

必要な結果:

必要な結果

4

1 に答える 1

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>

opgroup のネストは許可されていないことに注意してください。ここで読むことができるように、許可される optgroup レベルは 1 つだけです。

于 2012-09-18T19:52:47.253 に答える