Re: [PHP] Re: Variable name as a string

This is a discussion on Re: [PHP] Re: Variable name as a string within the PHP forums in Programming Languages category; Micah Gersten wrote: > You cannot have anything in the brackets for the name in a checkbox > group. The brackets specify that it is an array. The name of the array > is the key in $_POST that contains the values of the checkbox group that > were checked. You can have as many groups as you like. Eh? Course you can. <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <input type="checkbox" name="data[field_name][]" id="my_checkbox_1" value="1" > Val 1 <input type="checkbox" name="data[field_name][]" id="my_checkbox_2" value="2" > Val 2 <input type="checkbox" name="data[field2][]" id="field1" value="1" > Val 1 (Field 1) <input type="checkbox" name="data[field2][]" id="field2" value="2" ...

Go Back   Application Development Forum > Programming Languages > PHP

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #11  
Old 08-27-2008, 11:51 PM
Chris
Guest
 
Default Re: [PHP] Re: Variable name as a string

Micah Gersten wrote:
> You cannot have anything in the brackets for the name in a checkbox
> group. The brackets specify that it is an array. The name of the array
> is the key in $_POST that contains the values of the checkbox group that
> were checked. You can have as many groups as you like.


Eh? Course you can.

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="checkbox" name="data[field_name][]" id="my_checkbox_1"
value="1" > Val 1
<input type="checkbox" name="data[field_name][]" id="my_checkbox_2"
value="2" > Val 2

<input type="checkbox" name="data[field2][]" id="field1" value="1" > Val
1 (Field 1)
<input type="checkbox" name="data[field2][]" id="field2" value="2" > Val
2 (Field 2)
<input type="submit">
</form>
<pre>
<?php
print_r($_POST);


You end up with a multi-dimensional array.


Array
(
[data] => Array
(
[field_name] => Array
(
[0] => 1
)

[field2] => Array
(
[0] => 1
[1] => 2
)

)

)

--
Postgresql & php tutorials
http://www.designmagick.com/

Reply With Quote
  #12  
Old 08-28-2008, 12:03 AM
Ross McKay
Guest
 
Default Re: [PHP] Re: Variable name as a string

On Wed, 27 Aug 2008 22:25:44 -0500, Micah Gersten wrote:

>You cannot have anything in the brackets for the name in a checkbox
>group. [...]


Bollocks.

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<p>option 1 - colour: <input type="text" name="options[colour]"/></p>
<p>option 2 - flavour: <input type="text" name="options[flavour]"/></p>
<p>option 3 - size: <input type="text" name="options[size]"/></p>
<p><input type="submit"/></p>
</form>

<?php
function test() {
$colour = $_POST['options']['colour'];
echo "<p>colour: $colour</p>\n";

$flavour = $_POST['options']['flavour'];
echo "<p>flavour: $flavour</p>\n";

$size = $_POST['options']['size'];
echo "<p>size: $size</p>\n";
}
?>

NB: no quotes around array key!

I found this very handy for having variable product options on a simple
shopping cart.
--
Ross McKay, Toronto, NSW Australia
"The lawn could stand another mowing; funny, I don't even care"
- Elvis Costello
Reply With Quote
  #13  
Old 08-28-2008, 12:13 AM
Ross McKay
Guest
 
Default Re: [PHP] Re: Variable name as a string

More specifically:

<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
<p>option 1 - colour:<br/>
# <input type="checkbox" name="options[colour][]" value="red"/> red<br/>
# <input type="checkbox" name="options[colour][]" value="green"/>
green<br/>
# <input type="checkbox" name="options[colour][]" value="blue"/>
blue</p>
<p><input type="submit"/></p>
</form>

<?php
foreach($_POST['options']['colour'] as $colour)
echo "<p>colour: $colour</p>\n";
?>
--
Ross McKay, Toronto, NSW Australia
"If ye cannae see the bottom, dinnae complain if ye droon"
- The Wee Book of Calvin
Reply With Quote
  #14  
Old 08-28-2008, 11:22 PM
ioannes
Guest
 
Default Re: [PHP] Re: Variable name as a string

In writing the script, though, there are two points. I don't always use
checkboxes, sometimes I just want to update all the records on the
form. Eg I have a series of images and related text each with their ID
in the database table, on clicking update all the form elements get
submitted and I need to relate the relevant image, text, etc with the
table ID. So I can't rely on checkboxes which only get posted if
ticked. So again I have to iterate through all possible IDs. Which
normally can be done but it is longer, eg because the images on the page
in the first place may not be the result of a simple select query. So I
suppose the solution there is to have a hidden field with all the IDs as
a string, explode that and then iterate through that. Eg

<input type=hidden value="1_2_3" name="all_IDs">

$IDs=explode("_",$_POST['all_IDs']);

and that gives me the table IDs to do update queries on etc.

John

ioannes wrote:
> Actually, you are right, as you just put the checkbox index in the
> POST and get the value from there. So you just need the number of
> checkboxes...sorry.
>
> ioannes wrote:
>> Yes, Tedd, this does however incur the overhead of find out what i
>> is, because it could be a range of IDs from the database, not
>> necessarily a count of the checkboxes on the page:
>>
>> "
>> for ($i = 1; $i <= 4; $i++)
>> {
>> $a = 'a' . $i;
>> $b = 'whatever' . $i;
>> if($_POST[$a] == 'on')
>> {
>> my_array[] = $_POST[$b]
>> }
>> }
>> "
>>
>> John
>>

>

Reply With Quote
  #15  
Old 08-29-2008, 03:45 AM
Bren Norris
Guest
 
Default RE: [PHP] Re: Variable name as a string

That solution would probably work just nicely.

That being said however, If you want to be sophisticated about this you
would make good use of JSON and php's corresponding functions
json_decode/encode which is common way to transport arrays as strings - and
much more.



-----Original Message-----
From: ioannes [mailto:ioannes@btinternet.com]
Sent: Friday, 29 August 2008 1:22 PM
To: php-general@lists.php.net
Subject: Re: [php] Re: Variable name as a string

In writing the script, though, there are two points. I don't always use
checkboxes, sometimes I just want to update all the records on the
form. Eg I have a series of images and related text each with their ID
in the database table, on clicking update all the form elements get
submitted and I need to relate the relevant image, text, etc with the
table ID. So I can't rely on checkboxes which only get posted if
ticked. So again I have to iterate through all possible IDs. Which
normally can be done but it is longer, eg because the images on the page
in the first place may not be the result of a simple select query. So I
suppose the solution there is to have a hidden field with all the IDs as
a string, explode that and then iterate through that. Eg

<input type=hidden value="1_2_3" name="all_IDs">

$IDs=explode("_",$_POST['all_IDs']);

and that gives me the table IDs to do update queries on etc.

John

ioannes wrote:
> Actually, you are right, as you just put the checkbox index in the
> POST and get the value from there. So you just need the number of
> checkboxes...sorry.
>
> ioannes wrote:
>> Yes, Tedd, this does however incur the overhead of find out what i
>> is, because it could be a range of IDs from the database, not
>> necessarily a count of the checkboxes on the page:
>>
>> "
>> for ($i = 1; $i <= 4; $i++)
>> {
>> $a = 'a' . $i;
>> $b = 'whatever' . $i;
>> if($_POST[$a] == 'on')
>> {
>> my_array[] = $_POST[$b]
>> }
>> }
>> "
>>
>> John
>>

>


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 07:50 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.