Populating a Drop-down List

Thread Solved
Reply

Join Date: Dec 2006
Posts: 71
Reputation: pygmalion is an unknown quantity at this point 
Solved Threads: 1
pygmalion's Avatar
pygmalion pygmalion is offline Offline
Junior Poster in Training

Populating a Drop-down List

 
0
  #1
Apr 18th, 2007
hi

i have the following issue. i'd much appreciate someone helping me out here.

i'm trying to let a drop-down list (which will be placed, and vary on many pages) with the contents of an array. how can i get that done?

plus, once a certain element has been selected, i'd like that a link appears that links to a specific (filename with certain naming, according to the selected element) file. how could that be implmented?

again, i'd much apprecite your help
pygmalion
Microsoft Employee: "Hey, it compiles! Ship it."
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: landae is an unknown quantity at this point 
Solved Threads: 1
landae landae is offline Offline
Newbie Poster

Re: Populating a Drop-down List

 
0
  #2
Apr 18th, 2007
Below is a basic example of how I wrote an array drop down for a choice of messengers. $msgr is simply a post result for msgr, so it retains what you selected.

  1. <fieldset><label for="msgr">Primary Messenger:</label>
  2. <?php
  3. echo "<select name=\"msgr\" id=\"msgr\">\n";
  4. $msgrArr = array();
  5. $msgrArr[] = "MSN";
  6. $msgrArr[] = "AIM";
  7. $msgrArr[] = "YIM";
  8. $msgrArr[] = "ICQ";
  9. $msgrArr[] = "Jabber/Gtalk";
  10. foreach($msgrArr as $v){
  11. if ($msgr == $v) {
  12. echo "<option value=\"$v\" selected=\"selected\">$v</option>\n";
  13. } else {
  14. echo "<option value=\"$v\">$v</option>\n";
  15. }
  16. }
  17. echo "</select>";
  18. ?>
  19. </fieldset>

As for the other thing your on about, perhaps you want to write some javascript thats populated then just shows the data. Alternatively you could use AJAX or simply reload the page based on the selection. Perhaps you can explain what your after more in depth.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Populating a Drop-down List

 
0
  #3
Apr 20th, 2007
Another solution would be, instead of using a foreach() you could use a for() loop.
  1. $arr = array("cat", "dog", "horse", "frog");
  2.  
  3. echo "<select name=dropdown_list><option value="bleh">Choose Me</option>";
  4.  
  5. // loop while $i is less than count(the amout of elements) of $arr
  6. for($i=0; $i < count($arr); $i++)
  7. echo <option value=". $arr[$i] .">" . $arr[$i] . "</option>";
  8.  
  9. echo "</select>";
  10.  

Enjoy.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 71
Reputation: pygmalion is an unknown quantity at this point 
Solved Threads: 1
pygmalion's Avatar
pygmalion pygmalion is offline Offline
Junior Poster in Training

Re: Populating a Drop-down List

 
0
  #4
May 1st, 2007
thanks a lot for your elaborate replies.
the populating of the drop-down list now works fine.

the case is as follows, i'm trying to populate the dropdown-list via a multidimensional array (because they will be audio recordings of parts of each chapter of a book).
now, first i'd like to declare the multi-dimensional array, which i have done as follows.

  1. $book1 = array("chapter1" => array(),
  2. "chapter2" => array(),
  3. "chapter3" => array());
now, i'd like to populate the second dimension of this array by using for-loops, as follows:

  1. $chapter1_amount = 4;
  2. $chapter2_amount = 6;
  3. $chapter3_amount = 8;
  4.  
  5. for($i=1; $i <= $chapter1_amount; $i++)
  6. {
  7. $book1["chapter1"][] = $i;
  8. }
  9.  
  10. for($i=1; $i <= $chapter2_amount; $i++)
  11. {
  12. $book1["chapter2"][] = $i;
  13. }
  14.  
  15. for($i=1; $i <= $chapter3_amount; $i++)
  16. {
  17. $book1["chapter3"][] = $i;
  18. }
i think herein lies the mistake, at declaring the variables at the 2nd dimension. am i doing this correctly?

afterwards, i show the drop-down list as you guys have suggested:

  1. echo "<select name=\"book1\"><option value=\"bleh\">Choose Part</option>";
  2.  
  3. foreach($book1 as $itemindex[] => $item[])
  4. {
  5. echo "<option value=\"$item\">$item</option>\n";
  6. }
  7.  
  8. echo "</select>";
when doing it this way, i get a drop-down list which just has three identical elements, all entitled 'Array'.

once this is done, i was just wondering how i could for example manipulate a link next to it to link to a file according to the chosen element in the list.

i'd very much appreciate your help, really
pygmalion
Microsoft Employee: "Hey, it compiles! Ship it."
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 71
Reputation: pygmalion is an unknown quantity at this point 
Solved Threads: 1
pygmalion's Avatar
pygmalion pygmalion is offline Offline
Junior Poster in Training

Re: Populating a Drop-down List

 
0
  #5
May 2nd, 2007
hi again.

i'd much appreciate anyone's help. this is quite of an importance to me.

thanks in advance
pygmalion
Microsoft Employee: "Hey, it compiles! Ship it."
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Populating a Drop-down List

 
0
  #6
May 2nd, 2007
  1. /* init */
  2. $arr = array(
  3. "chapter1" => array(),
  4. "chapter2" => array(),
  5. "chapter3" => array()
  6. );
  7.  
  8. $chapter1_amount = 4;
  9. $chapter2_amount = 6;
  10. $chapter3_amount = 8;
  11.  
  12. for($i = 1; $i <= $chapter1_amount; $i++)
  13. array_push($arr['chapter1'], $i);
  14.  
  15. for($i = 1; $i <= $chapter2_amount; $i++)
  16. array_push($arr['chapter2'], $i);
  17.  
  18. for($i = 1; $i <= $chapter3_amount; $i++)
  19. array_push($arr['chapter3'], $i);
This theoretically works. It's 3am and i didnt compile it, but looks fine to me.

For manipulating the link acording to the dropdown list.
You'd need to call a javascript function to generate the link.
When you choose an option in the list, you'd call the javascript function to update the link.

  1. /* HTML */
  2. <select onChange="updateLink(this)">....</select>
  3.  
  4. /* JavaScript */
  5. function updateLink(list) {
  6. if(list.value='bleh')
  7. link = "bleh";
  8.  
  9. ......
  10.  
  11. return link;
  12.  
  13. }
Last edited by dr4g; May 2nd, 2007 at 11:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 71
Reputation: pygmalion is an unknown quantity at this point 
Solved Threads: 1
pygmalion's Avatar
pygmalion pygmalion is offline Offline
Junior Poster in Training

Re: Populating a Drop-down List

 
0
  #7
May 4th, 2007
i did everything as you said, and it worked neatly.

thanks so much, really
pygmalion
Microsoft Employee: "Hey, it compiles! Ship it."
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Populating a Drop-down List

 
0
  #8
May 4th, 2007
Not a problem.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC