Store Combo Box values in an Array
Moderator: General Moderators
-
jimgym1989
- Forum Newbie
- Posts: 10
- Joined: Thu May 14, 2009 8:31 am
Store Combo Box values in an Array
Hi everyone! I am new in PHP.
Is there a way to store a value from a combo box to an array?
Can you show me an example.
Thanks!
Is there a way to store a value from a combo box to an array?
Can you show me an example.
Thanks!
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Store Combo Box values in an Array
If you are refering to a dropdown list you have to modify your html
Your php will be something like
The values are in the $value variable.
Code: Select all
<select name="Choices[]">
<option value="1">One</option>
<option value="2">Two</option>
</select>
Code: Select all
<?php
if (is_array($_POST['Choices'])) {
foreach ($_POST['Choices as $value) {
//
}
}
?>“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
-
jimgym1989
- Forum Newbie
- Posts: 10
- Joined: Thu May 14, 2009 8:31 am
Re: Store Combo Box values in an Array
Hi! Thank you for your reply.
I tried your code inserting into my code, I have two PHP files. Let me show you.
member.php
As you can see here, I have a database. It has a member table that has a name & access fields.
This is my view.php
I'm getting too many values in the $_POST['access'] even if I only put just one value. Its seems I'm getting all the values in the access field in my database.
What's wrong with this?
Thanks!
I tried your code inserting into my code, I have two PHP files. Let me show you.
member.php
Code: Select all
$strSQL = mysql_query("SELECT * FROM member");
while($row = mysql_fetch_array($strSQL)){
$data .="<td>";
$data .= "<select name='access[]'>";
$data .="<option selected value='".$row['access']."'>".$row['access']."</option>";
if($row['access'] != 'member'){
$data .="<option value='member'>member</option>";
}
else{
$data .="<option value='admin'>admin</option>";
}
$data .="</select>";
$data .="</td>";
$data .= "</tr>";
}
This is my view.php
Code: Select all
foreach ($_POST['access'] as $value){
echo "Access: ".$value;
}What's wrong with this?
Thanks!
-
Starkid225
- Forum Newbie
- Posts: 9
- Joined: Mon Feb 07, 2011 10:28 pm
Re: Store Combo Box values in an Array
In your query you have told it to display all results
This is your problem....
Code: Select all
$strSQL = mysql_query("SELECT * FROM member");
-
jimgym1989
- Forum Newbie
- Posts: 10
- Joined: Thu May 14, 2009 8:31 am
Re: Store Combo Box values in an Array
Umm,
Let me show you the GUI. Let me attach an image.

The reason why I have this so that it will display the member's access status and the name.
On the GUI part, User's is allowed to change the access status via the combo box and it will be process by clicking on the Update Button. And if the status will be changed, it should be put to an array.
Example: Elson's Access is an admin, I want to change it as a Member, The member value will now be stored into an array. Next is I want to change Juan's Access into Admin, the Admin will now again be stored in the array.
My problem here is that when I get to size of the array, it displays a wrong size, It displays a size of the total row count of my database. That's my problem.
Thank you for your patience!
Let me show you the GUI. Let me attach an image.

The reason why I have this
Code: Select all
$strSQL = mysql_query("SELECT * FROM member");On the GUI part, User's is allowed to change the access status via the combo box and it will be process by clicking on the Update Button. And if the status will be changed, it should be put to an array.
Example: Elson's Access is an admin, I want to change it as a Member, The member value will now be stored into an array. Next is I want to change Juan's Access into Admin, the Admin will now again be stored in the array.
My problem here is that when I get to size of the array, it displays a wrong size, It displays a size of the total row count of my database. That's my problem.
Thank you for your patience!
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Store Combo Box values in an Array
Code: Select all
$data .= "<select name='access[]'>";“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
-
jimgym1989
- Forum Newbie
- Posts: 10
- Joined: Thu May 14, 2009 8:31 am
Re: Store Combo Box values in an Array
What should I do inorder for the values of the combo box that were check will be only stored in the array?
Thank you!
Thank you!