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

9 Years
Discussion Span
Last Post by tmks

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.

<fieldset><label for="msgr">Primary Messenger:</label>
	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>";

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.

$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>";



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.

$book1 = array("chapter1"    =>    array(),
                     "chapter2"    =>    array(),
                     "chapter3"    =>    array());

now, i'd like to populate the second dimension of this array by using for-loops, as follows:

$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;

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:

echo "<select name=\"book1\"><option value=\"bleh\">Choose Part</option>";
    foreach($book1 as $itemindex[] => $item[])
        echo "<option value=\"$item\">$item</option>\n";
    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


hi again.

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

thanks in advance

/* 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);

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.

/* HTML */
      <select onChange="updateLink(this)">....</select>

      /* JavaScript */
     function updateLink(list) {
            link = "bleh";

       return link;


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

thanks so much, really


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


hello! I have a problem creating a dropdown list from my database.. my code is as follows:

$sql = "select * from subjectarea";
		 $result = mysql_query($sql);
		 $num = mysql_num_rows($result);
		while ($row = mysql_fetch_array($result)) 
			 $i =0;

			$tpl -> assign( array( "{KEYWORD}" => htmlspecialchars(stripslashes($result_array[$i])) ));
      } //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..

			<td align="right">
				<strong>1st Keyword *:</strong>
				<select name="KEYWORD1" style="margin-left: 10pt;">
					<option value="{KEYWORD}">{KEYWORD}</option>										

where am I wrong? I cannot avoid using tpl. Thank you in advance... :)

$yr = date("Y")+2;
$end = ($yr-40);
for ($i=$yr; $i>=$end; $i--){
    echo "<option value=\"$i\">$i<option>";

this code leaves blank <option></option> after every loop
<option value ="2014">2014</option>

can anyone help

Edited by Dani: Formatting fixed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.