Indented parent-child list inside drop down selection SOLVED

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Indented parent-child list inside drop down selection SOLVED

Postby guy13 » Wed Mar 19, 2014 2:29 pm

Hi all,

Thanks in advance for having a look at this.

What I'm trying to do is build a local website with database to play with my CD collection. Nothing too fancy, just a bunch of tables and some php pages that help me find stuff and enter new data into the tables. I'm currently trying to build a form that allows me to add works from classical composers to individual CDs.

The form is supposed to do the following: after selecting a composer you get a number of fields in which to enter or select data after which these data are sent to a new table from which I can then reconstruct content lists of CDs. One of the select boxes should contain an indented list of the compositions of the selected composer. This list comes from a table called 'werkenlijst_klassiek' with the following layout:

ID | Parent | Comp_ID | Werknr | Titel

I found a solution for the indented list in this article: http://www.sitepoint.com/hierarchical-data-database/ and managed to adapt it to my table(s). When I tried it in a separate test page I got a nicely indented list. However when I tried to implement it in my drop down box I got one very, very long line of compostitions, one after the other. The code I ended up with after trying to fit the indented list bit into the drop down list bit is the following:

Code: Select all
  <tr>
      <td class="veldnaam">Werk:&nbsp;</td>
      <td class="gegevens">
           <select name="Werk">

                <?php

                function toon_werkenlijst($parent, $level) {
                        // display all children of $parent
                    $result = mysql_query('SELECT ID, Titel, Werknr FROM werkenlijst_klassiek
                    WHERE parent="'.$parent.'";');
                        // display every child
                    while ($row = mysql_fetch_array($result)) {
                        // indent + display Titel & Werknr
                    echo str_repeat('&nbsp &nbsp &nbsp &nbsp &nbsp',$level) . $row['Titel'] . " (".$row['Werknr'] .
                        ")"; ?><br><?php
                       // execute function again and display next generation
                   toon_werkenlijst($row['ID'], $level+1);

                        }
                    }

                $result = mysql_query("SELECT DISTINCT ID
                FROM werkenlijst_klassiek
                WHERE Comp_ID = $Compnr                                               
                AND Titel = 'Verzameld werk'");
                $row = mysql_fetch_array($result);
                while ($row) {
                     ?>
                     <option value="<?php print($row['ID']); ?>"><?php toon_werkenlijst($row['ID'], 0);                     

                     $row = mysql_fetch_array($result);
                }
                ?>


                ?>
           </select>
      </td>
    </tr>


I'm not comfortable with the $row['ID'] variable as it seems to be two different things to me but whenever I try to change that I get no more results at all. (The $Compnr variable is the result of the composer selection at the beginning and together with the title 'Verzameld werk' (Collected works) makes sure that the works of the right composer are selected.)

Any light shed on this would be most welcome.

P.S. I know I should be using mysqli- instead of mysql-queries but I want to get a grip on this problem before tackling a rewrite...
Last edited by guy13 on Mon Mar 24, 2014 8:07 pm, edited 1 time in total.
guy13
 
Posts: 47
Joined: Wed Apr 13, 2005 7:39 pm
Location: Belgium

Postby guy » Wed Mar 19, 2014 9:04 pm

Are you using the HTML Forms <select> for your drop-down box? If so, try using the <option> element for each entry instead of the <br> line break between them. It's not just another web page.
Cheers,
Guy
The eternal help vampire
User avatar
guy
LXF regular
 
Posts: 1086
Joined: Thu Apr 07, 2005 12:07 pm
Location: Worcestershire

Postby johnhudson » Wed Mar 19, 2014 9:25 pm

Child lists are normally <option> elements within <optgroup> and <select> elements or within a <datalist>.

I've never used this combination in anger but my guess would be that the <datalist> approach might do what you want.
johnhudson
LXF regular
 
Posts: 881
Joined: Wed Aug 03, 2005 1:37 pm

Postby guy13 » Wed Mar 19, 2014 11:08 pm

Thanks for the replies: they look promising. Will give them my full attention tomorrow.

G.
guy13
 
Posts: 47
Joined: Wed Apr 13, 2005 7:39 pm
Location: Belgium

Postby guy13 » Mon Mar 24, 2014 8:05 pm

Once again thanks for the help. It took me some trial and error to finally realize where the suggested extra <option> tags had to be but I got there in the end.

Just in case anyone else is interested: I dropped the <br> tag in the function, put the whole thing inside an <optgroup> tag and put an <option> tag around the line inside the function that starts with "echo str_repeat" as per your suggestion. Drop down list now actually looks kinda neat.

With humble greetings,

Guy
guy13
 
Posts: 47
Joined: Wed Apr 13, 2005 7:39 pm
Location: Belgium


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests