how to store pull down menu variables in an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

how to store pull down menu variables in an array

Post by Tokunbo »

hello,

I want to ask if this is possible in PHP: to store user selections from a drop down list into an array?

For example: a list with 5-items (item-1 ......item-5).

A user makes two selections, one by one. The first time the user makes a selection(ex: item-2), it should be stored in position[0] of $my_array. The next time the user selects iotem-5 and hits the submit button, it should be stored in position[1]....and so on and so forth.



cheers
Toks
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to store pull down menu variables in an array

Post by social_experiment »

Tokunbo wrote:I want to ask if this is possible in PHP: to store user selections from a drop down list into an array?
Very. If you modify your html code as the example below, the $_POST value for the select element becomes an array containing selected items.

Code: Select all

<select name="options[]">
<option value="1">One</option>
<option value="2">Two</option>
</select>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: how to store pull down menu variables in an array

Post by Tokunbo »

@social_experiment,

thanks for the reply. This is my testcode:

Code: Select all

<?php
session_start();
$drop_array=array($_POST['options']);
print_r($drop_array);
?>


<html>
<body>

<form action="dropdown1.php" method="post">
ABC <select name="options[]">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
</select>

<input type="submit" />
</form>

With the above, I observed the users selection is always stored at array[0]. Any other submission is replaced. What I want to do is to have the first selection stored at [0], the next at [1], and then [2], etc.

I googled and found the "array_push" command. Where / how do I use array_push in the above. Do I need to assign it to another variable?

pls advise.
Toks
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: how to store pull down menu variables in an array

Post by social_experiment »

Tokunbo wrote:Do I need to assign it to another variable?
Yes, since you are already using sessions, you could store the chosen value in a session variable $_SESSION['chosenValsAry']

Code: Select all

<?php
 // after the button has been submitted and the select element is NOT an array
 $newArray = array_push($_SESSION['chosenValsAry'], $_POST['options']);
 // or if $_POST['options'] is an array
 $newArray = array_merge($_SESSION['chosenValsAry'], $_POST['options']);
?>
Something else just occured, you wouldn't need the dropdown menu to be an array then, but if it was, you could also use array_merge() to combine the two arrays.

PHP Manual on array_merge()
The Manual wrote: Description
array array_merge ( array $array1 [, array $array2 [, array $... ]] )
Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply