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; 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...

Go Back   Application Development Forum > Programming Languages > PHP

Object Mix

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

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
  #2  
Old 08-27-2008, 02:20 PM
tedd
Guest
 
Default Re: [PHP] Re: Variable name as a string

At 7:08 PM +0100 8/27/08, 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


John:

Yes, and I thought that I showed you how to handle that -- that an
easy thing to do.

You simply list all the items you want to expose to the user for the
user's consideration to delete. Then you accept what the user has
selected and delete them accordingly. (However, you should work out a
way to clean this information before doing anything).

I only added the count thing IF you wanted to know how many deletions
the user selected.

Please review what I said and consider.

Cheers,

tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #3  
Old 08-27-2008, 02:31 PM
ioannes
Guest
 
Default Re: [PHP] Re: Variable name as a string

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
  #4  
Old 08-27-2008, 02:58 PM
Shawn McKenzie
Guest
 
Default Re: [PHP] Re: Variable name as a string

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
>>


Either I'm missing what you're trying to do, or this has become
incredibly over complicated!

-Shawn
Reply With Quote
  #5  
Old 08-27-2008, 05:41 PM
tedd
Guest
 
Default Re: [PHP] Re: Variable name as a string

At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote:
>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.
>>>
>>>for ($i = 1; $i <= 4; $i++)
>>> {
>>> $a = 'a' . $i;
>>> $b = 'whatever' . $i;
>>> if($_POST[$a] == 'on')
>>> {
>>> my_array[] = $_POST[$b]
>>> }
>>> }
>>>"
>>>
>>>John
>>>

>
>Either I'm missing what you're trying to do, or this has become
>incredibly over complicated!
>
>-Shawn


It's not over complicated, but just a method of passing checked
checkbox values to a php array. Do you have something better?

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #6  
Old 08-27-2008, 06:07 PM
Maciek Sokolewicz
Guest
 
Default Re: [PHP] Re: Variable name as a string

tedd wrote:
> At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote:
>> 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.
>>>>
>>>> for ($i = 1; $i <= 4; $i++)
>>>> {
>>>> $a = 'a' . $i;
>>>> $b = 'whatever' . $i;
>>>> if($_POST[$a] == 'on')
>>>> {
>>>> my_array[] = $_POST[$b]
>>>> }
>>>> }
>>>> "
>>>>
>>>> John
>>>>

>>
>> Either I'm missing what you're trying to do, or this has become
>> incredibly over complicated!
>>
>> -Shawn

>
> It's not over complicated, but just a method of passing checked checkbox
> values to a php array. Do you have something better?
>
> Cheers,
>
> tedd
>


Well, this seems easier/cleaner to me:

<input type="check" name="my_checkboxes[1]" value="1" /> 1<br />
<input type="check" name="my_checkboxes[2]" value="1" /> 1<br />
<input type="check" name="my_checkboxes[3]" value="1" /> 1<br />
<input type="check" name="my_checkboxes[4]" value="1" /> 1<br />

$my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever you
wish, $_GET or $_POST, I don't care right now; you choose.
Reply With Quote
  #7  
Old 08-27-2008, 06:34 PM
Micah Gersten
Guest
 
Default Re: [PHP] Re: Variable name as a string

First, the type is checkbox, not check. Second, you cannot put a value
in the brackets for a checkbox group. A checkbox group is passed to
PHP automatically as an array.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



Maciek Sokolewicz wrote:
> Well, this seems easier/cleaner to me:
>
> <input type="check" name="my_checkboxes[1]" value="1" /> 1<br />
> <input type="check" name="my_checkboxes[2]" value="1" /> 1<br />
> <input type="check" name="my_checkboxes[3]" value="1" /> 1<br />
> <input type="check" name="my_checkboxes[4]" value="1" /> 1<br />
>
> $my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever
> you wish, $_GET or $_POST, I don't care right now; you choose.
>

Reply With Quote
  #8  
Old 08-27-2008, 07:03 PM
tedd
Guest
 
Default Re: [PHP] Re: Variable name as a string

At 12:07 AM +0200 8/28/08, Maciek Sokolewicz wrote:
>tedd wrote:
>>At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote:
>>>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.
>>>>>
>>>>>for ($i = 1; $i <= 4; $i++)
>>>>> {
>>>>> $a = 'a' . $i;
>>>>> $b = 'whatever' . $i;
>>>>> if($_POST[$a] == 'on')
>>>>> {
>>>>> my_array[] = $_POST[$b]
>>>>> }
>>>>> }
>>>>>
>>>
>>>Either I'm missing what you're trying to do, or this has become
>>>incredibly over complicated!
>>>
>>>-Shawn

>>
>>It's not over complicated, but just a method of passing checked
>>checkbox values to a php array. Do you have something better?
>>
>>Cheers,
>>
>>tedd
>>

>
>Well, this seems easier/cleaner to me:
>
><input type="check" name="my_checkboxes[1]" value="1" /> 1<br />
><input type="check" name="my_checkboxes[2]" value="1" /> 1<br />
><input type="check" name="my_checkboxes[3]" value="1" /> 1<br />
><input type="check" name="my_checkboxes[4]" value="1" /> 1<br />
>
>$my_checked_checkboxes = $_REQUEST['my_checkboxes']; // whichever
>you wish, $_GET or $_POST, I don't care right now; you choose.


Yeah, I remember that -- but a bit different.

Don't use indexes, but rather just my_checkboxes[]

and on the php side

$my_checked_checkboxes = $_REQUEST['my_checkboxes'];

The array $my_checked_checkboxes equals the $_REQUEST$_/$_POST/$_GET
array -- all the indexes will match (i.e., $my_checked_checkboxes[3]
is the same as $_POST[3]).

The only problem I have with that method is that the [] becomes
confusing with dealing with javascript that can also handles the form.

One of the ways to get around this is to:

<input type="checkbox" name="my_checkboxes[]" id="my_checkbox_1" value="1" >

That way php will use "name" and javascript will use "id".

But, there are lot's of ways to do this.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #9  
Old 08-27-2008, 10:40 PM
Shawn McKenzie
Guest
 
Default Re: [PHP] Re: Variable name as a string

tedd wrote:
> At 1:58 PM -0500 8/27/08, Shawn McKenzie wrote:
>> 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.
>>>>
>>>> for ($i = 1; $i <= 4; $i++)
>>>> {
>>>> $a = 'a' . $i;
>>>> $b = 'whatever' . $i;
>>>> if($_POST[$a] == 'on')
>>>> {
>>>> my_array[] = $_POST[$b]
>>>> }
>>>> }
>>>> "
>>>>
>>>> John
>>>>

>>
>> Either I'm missing what you're trying to do, or this has become
>> incredibly over complicated!
>>
>> -Shawn

>
> It's not over complicated, but just a method of passing checked checkbox
> values to a php array. Do you have something better?
>
> Cheers,
>
> tedd
>


I guess you missed my last post, it was maybe the first or second reply
to this thread:

<input type="checkbox" name="data[field_name]" id="my_checkbox_1"
value="1" >

Then on post you have a $_POST['data'] array that contains the
field_names as keys and the checkbox values as values.

That's why I was wondering if I was missing something.

-Shawn
Reply With Quote
  #10  
Old 08-27-2008, 11:25 PM
Micah Gersten
Guest
 
Default Re: [PHP] Re: Variable name as a string

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.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



Shawn McKenzie wrote:
> I guess you missed my last post, it was maybe the first or second
> reply to this thread:
>
> <input type="checkbox" name="data[field_name]" id="my_checkbox_1"
> value="1" >
>
> Then on post you have a $_POST['data'] array that contains the
> field_names as keys and the checkbox values as values.
>
> That's why I was wondering if I was missing something.
>
> -Shawn
>

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:29 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.