Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Indented parent-child list inside drop down selection SOLVED

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
guy13



Joined: Wed Apr 13, 2005 8:39 pm
Posts: 47
Location: Belgium

PostPosted: Wed Mar 19, 2014 2:29 pm    Post subject: Indented parent-child list inside drop down selection SOLVED Reply with quote

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:
  <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
Back to top
View user's profile Send private message
guy
LXF regular


Joined: Thu Apr 07, 2005 1:07 pm
Posts: 1070
Location: Worcestershire

PostPosted: Wed Mar 19, 2014 9:04 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
johnhudson
LXF regular


Joined: Wed Aug 03, 2005 2:37 pm
Posts: 870

PostPosted: Wed Mar 19, 2014 9:25 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
guy13



Joined: Wed Apr 13, 2005 8:39 pm
Posts: 47
Location: Belgium

PostPosted: Wed Mar 19, 2014 11:08 pm    Post subject: Reply with quote

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

G.
Back to top
View user's profile Send private message
guy13



Joined: Wed Apr 13, 2005 8:39 pm
Posts: 47
Location: Belgium

PostPosted: Mon Mar 24, 2014 8:05 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast