Store Combo Box values 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
jimgym1989
Forum Newbie
Posts: 10
Joined: Thu May 14, 2009 8:31 am

Store Combo Box values in an Array

Post 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!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Store Combo Box values in an Array

Post 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.
“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

Post 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! :D
Starkid225
Forum Newbie
Posts: 9
Joined: Mon Feb 07, 2011 10:28 pm

Re: Store Combo Box values in an Array

Post 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....
jimgym1989
Forum Newbie
Posts: 10
Joined: Thu May 14, 2009 8:31 am

Re: Store Combo Box values in an Array

Post by jimgym1989 »

Umm,
Let me show you the GUI. Let me attach an image.
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! :D
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Store Combo Box values in an Array

Post 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'].
“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

Post 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! :)
Post Reply