944,116 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 46755
  • PHP RSS
Apr 18th, 2007
0

Populating a Drop-down List

Expand Post »
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
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
pygmalion is offline Offline
71 posts
since Dec 2006
Apr 18th, 2007
0

Re: Populating a Drop-down List

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.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
landae is offline Offline
4 posts
since Apr 2007
Apr 20th, 2007
0

Re: Populating a Drop-down List

Another solution would be, instead of using a foreach() you could use a for() loop.
php Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
May 1st, 2007
0

Re: Populating a Drop-down List

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.

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
pygmalion is offline Offline
71 posts
since Dec 2006
May 2nd, 2007
0

Re: Populating a Drop-down List

hi again.

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

thanks in advance
pygmalion
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
pygmalion is offline Offline
71 posts
since Dec 2006
May 2nd, 2007
0

Re: Populating a Drop-down List

PHP Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
May 4th, 2007
0

Re: Populating a Drop-down List

i did everything as you said, and it worked neatly.

thanks so much, really
pygmalion
Reputation Points: 12
Solved Threads: 1
Junior Poster in Training
pygmalion is offline Offline
71 posts
since Dec 2006
May 4th, 2007
0

Re: Populating a Drop-down List

Not a problem.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 16th, 2010
0

How would I stop selectedindex vlaue in php

Hello guyz,

I am trying to slect value from dropdown as well as page contents wanted to change on that selected value.

I have a problem in a control where I don't want to selectedindex change..i.e I want to stop this property .How will it be possible in PHP
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kishan_23 is offline Offline
1 posts
since Jul 2010
Oct 4th, 2011
0
Re: Populating a Drop-down List
hello! I have a problem creating a dropdown list from my database.. my code is as follows:
PHP Syntax (Toggle Plain Text)
  1. $sql = "select * from subjectarea";
  2. $result = mysql_query($sql);
  3. $num = mysql_num_rows($result);
  4. $j=0;
  5.  
  6. while ($row = mysql_fetch_array($result))
  7. {
  8. $result_array[$j]=$row["expertise"];
  9. $j++;
  10. }
  11. $i =0;
  12.  
  13. while($i<$num)
  14. {
  15. $tpl -> assign( array( "{KEYWORD}" => htmlspecialchars(stripslashes($result_array[$i])) ));
  16. $i++;
  17. } //end while

when I do echo in the second while, I can see all my values correctly.. but I can't see them in my form. I have the .tpl file as follows..

PHP Syntax (Toggle Plain Text)
  1. <tr>
  2. <td align="right">
  3. <strong>1st Keyword *:</strong>
  4. </td>
  5. <td>
  6. <select name="KEYWORD1" style="margin-left: 10pt;">
  7. <option value="{KEYWORD}">{KEYWORD}</option>
  8. </select>
  9. </td>
  10. </tr>

where am I wrong? I cannot avoid using tpl. Thank you in advance...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ilonael is offline Offline
1 posts
since Oct 2011

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Keywords list (PHP, JavaScript and HTML)
Next Thread in PHP Forum Timeline: Problem with PHP + GD - PNG Image Quality and Transparency problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC