Page 1 of 1
Store Combo Box values in an Array
Posted: Mon Feb 07, 2011 10:17 am
by jimgym1989
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!
Re: Store Combo Box values in an Array
Posted: Mon Feb 07, 2011 10:26 am
by social_experiment
If you are refering to a dropdown list you have to modify your html
Code: Select all
<select name="Choices[]">
<option value="1">One</option>
<option value="2">Two</option>
</select>
Your php will be something like
Code: Select all
<?php
if (is_array($_POST['Choices'])) {
foreach ($_POST['Choices as $value) {
//
}
}
?>
The values are in the
$value variable.
Re: Store Combo Box values in an Array
Posted: Mon Feb 07, 2011 10:39 pm
by jimgym1989
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
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>";
}
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
Code: Select all
foreach ($_POST['access'] as $value){
echo "Access: ".$value;
}
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!

Re: Store Combo Box values in an Array
Posted: Mon Feb 07, 2011 11:02 pm
by Starkid225
In your query you have told it to display all results
Code: Select all
$strSQL = mysql_query("SELECT * FROM member");
This is your problem....
Re: Store Combo Box values in an Array
Posted: Mon Feb 07, 2011 11:19 pm
by jimgym1989
Umm,
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");
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!

Re: Store Combo Box values in an Array
Posted: Tue Feb 08, 2011 12:15 am
by social_experiment
Code: Select all
$data .= "<select name='access[]'>";
You are giving all the select boxes the same name, when you check the $_POST['access'] value, it picks up all values for $_POST['access'].
Re: Store Combo Box values in an Array
Posted: Tue Feb 08, 2011 2:58 am
by jimgym1989
What should I do inorder for the values of the combo box that were check will be only stored in the array?
Thank you!
