| | |
Populating a Drop-down List
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved
![]() |
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
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."
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 1
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.
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.
PHP Syntax (Toggle Plain Text)
<fieldset><label for="msgr">Primary Messenger:</label> <?php echo "<select name=\"msgr\" id=\"msgr\">\n"; $msgrArr = array(); $msgrArr[] = "MSN"; $msgrArr[] = "AIM"; $msgrArr[] = "YIM"; $msgrArr[] = "ICQ"; $msgrArr[] = "Jabber/Gtalk"; foreach($msgrArr as $v){ if ($msgr == $v) { echo "<option value=\"$v\" selected=\"selected\">$v</option>\n"; } else { echo "<option value=\"$v\">$v</option>\n"; } } echo "</select>"; ?> </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.
Another solution would be, instead of using a foreach() you could use a for() loop.
Enjoy.
php Syntax (Toggle Plain Text)
$arr = array("cat", "dog", "horse", "frog"); echo "<select name=dropdown_list><option value="bleh">Choose Me</option>"; // loop while $i is less than count(the amout of elements) of $arr for($i=0; $i < count($arr); $i++) echo <option value=". $arr[$i] .">" . $arr[$i] . "</option>"; echo "</select>";
Enjoy.
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.
now, i'd like to populate the second dimension of this array by using for-loops, as follows:
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:
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
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)
$book1 = array("chapter1" => array(), "chapter2" => array(), "chapter3" => array());
PHP Syntax (Toggle Plain Text)
$chapter1_amount = 4; $chapter2_amount = 6; $chapter3_amount = 8; for($i=1; $i <= $chapter1_amount; $i++) { $book1["chapter1"][] = $i; } for($i=1; $i <= $chapter2_amount; $i++) { $book1["chapter2"][] = $i; } for($i=1; $i <= $chapter3_amount; $i++) { $book1["chapter3"][] = $i; }
afterwards, i show the drop-down list as you guys have suggested:
PHP Syntax (Toggle Plain Text)
echo "<select name=\"book1\"><option value=\"bleh\">Choose Part</option>"; foreach($book1 as $itemindex[] => $item[]) { echo "<option value=\"$item\">$item</option>\n"; } echo "</select>";
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."
PHP Syntax (Toggle Plain Text)
/* init */ $arr = array( "chapter1" => array(), "chapter2" => array(), "chapter3" => array() ); $chapter1_amount = 4; $chapter2_amount = 6; $chapter3_amount = 8; for($i = 1; $i <= $chapter1_amount; $i++) array_push($arr['chapter1'], $i); for($i = 1; $i <= $chapter2_amount; $i++) array_push($arr['chapter2'], $i); for($i = 1; $i <= $chapter3_amount; $i++) array_push($arr['chapter3'], $i);
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)
/* HTML */ <select onChange="updateLink(this)">....</select> /* JavaScript */ function updateLink(list) { if(list.value='bleh') link = "bleh"; ...... return link; }
Last edited by dr4g; May 2nd, 2007 at 11:22 pm.
![]() |
Similar Threads
- I lost all my program on start menu and word/excel docs (Windows NT / 2000 / XP)
- database table design problems (Database Design)
Other Threads in the PHP Forum
- Previous Thread: Upload file without User Interaction
- Next Thread: Is ColdFusion and PHP coexisting on the same box possible?
| Thread Tools | Search this Thread |
action address advanced ajax apache api array auto autoincrement basics beginner broken cakephp class cms code codingproblem combobox cron curl database date datepart display domain dynamic email error errorlog file folder form format forms function functions google head href htaccess html image include includingmysecondfileinthechain interactive ip javascript job joomla js limit link load login malfunctioning masterthesis menu multiple mysql nodes oop outofmemmory paging password paypal pdf php popup problem procedure query ram random reference script search server sessions smarty source space sql stored syntax system table traffic tutorial unicode unset up-to-date upload url validation variable video web webapplications websitecontactform youtube





