0

need some help completing my code what it does is searches in the tables of my DB for all the sections pointing to the parent_sections=2 and then prints the results in a select tag i would like some help with the sql query to check if the parent_sections=2 has child's and if so to print them with a optgroup style anyway please check the images below they explain what i need.

PHP & SQL CODE:

<?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: TABLE sections

SQL table sections_detail: TABLE sections_detail

RESULT:

RESULT

RESULT I NEED:

RESULT I NEED

Ered
  • 465
  • 1
  • 6
  • 23

1 Answers1

1

Here is my solution:

<?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>

Be aware that nesting opgroups is not allowed; only one optgroup level is allowed, as you can also read here.

Community
  • 1
  • 1
  • hey thanks for ur help any solution if nesting is required after one optgroup level to simple add a space instead of optgroup? – Ered Sep 18 '12 at 20:10
  • @Ered Sorry, but I have not understood your question. Can you explain it better, please? –  Sep 18 '12 at 20:13
  • i understand only one optgroup level is allowed but in case a second level is required is there any solution for that? add spaces instead of optgroup or i was thinking instead of using optgroup simple make parents bold and add a margin-left:5px; to each child. – Ered Sep 18 '12 at 20:22
  • Have a look at [this answer](http://stackoverflow.com/questions/5288792/how-to-indent-multiple-levels-of-select-optgroup-with-css) –  Sep 18 '12 at 20:34
  • `function print_section($sect) { if (count($sect['childs'])) { echo "\n"; } else { echo "\n"; } }` – Ered Sep 18 '12 at 20:49
  • ok so i edited the print_section function and replaced optgroup with option also i added a style the problem is how to difference between single menus and childs menus. `` it should be something like if(child){echo "class=\"optionChild\"";}else{NOTHING} – Ered Sep 18 '12 at 20:52
  • I've tested your changes with my Chromium browser, and they didn't work: there is no bold, no italic and no padding on any option. In my opinion trying to apply a CSS on an "option" element is not a good idea, because cross browser styling is so inconsistent that it's not worth the bother. Maybe you should not use a "select", but rather use a list (eg.: an unordered list with a checkbox for every list item). –  Sep 18 '12 at 21:11
  • well that's another option anyway if u could help me out with a sample i will really appreciate it – Ered Sep 18 '12 at 21:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16849/discussion-between-ered-and-enzino) – Ered Sep 18 '12 at 21:22