Bluehost.com Web Hosting $6.95

Variable name as a string

This is a discussion on Variable name as a string within the PHP General forums, part of the PHP Programming Forums category; Could someone tell me how to get the name of a variable as a string. This would be useful in ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-27-2008
ioannes
 
Posts: n/a
Default Variable name as a string

Could someone tell me how to get the name of a variable as a string.
This would be useful in form submission with multiple check-boxes to
match against database records. At the moment I use ${"var".$ID[$x]} or
someting like that to go through all the possible matches, but it would
be quicker to explode the name of a checkbox like a string like cb_1 to
match to record 1 etc.

There is some reason I do not use the value of the check box, but I can't
remember it now!

John


Reply With Quote
  #2 (permalink)  
Old 08-27-2008
Hélio Rocha
 
Posts: n/a
Default Re: [PHP] Variable name as a string

U could use an array to keep all of your fieldnames as keys and the values
would be the values of the form inputs.
That would be very easy and with no programagic.

On Wed, Aug 27, 2008 at 3:00 PM, ioannes <ioannes@btinternet.com> wrote:

> Could someone tell me how to get the name of a variable as a string. This
> would be useful in form submission with multiple check-boxes to match
> against database records. At the moment I use ${"var".$ID[$x]} or someting
> like that to go through all the possible matches, but it would be quicker to
> explode the name of a checkbox like a string like cb_1 to match to record 1
> etc.
>
> There is some reason I do not use the value of the check box, but I can't
> remember it now!
>
> John
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


Reply With Quote
  #3 (permalink)  
Old 08-27-2008
Shawn McKenzie
 
Posts: n/a
Default Re: Variable name as a string

ioannes wrote:
> Could someone tell me how to get the name of a variable as a string.
> This would be useful in form submission with multiple check-boxes to
> match against database records. At the moment I use ${"var".$ID[$x]} or
> someting like that to go through all the possible matches, but it would
> be quicker to explode the name of a checkbox like a string like cb_1 to
> match to record 1 etc.
>
> There is some reason I do not use the value of the check box, but I
> can't remember it now!
>
> John
>
>


For database use I do the following in forms:

name="data[field_name]"

Or for multiple table that you update together:

name="data[table_name][field_name]"

-Shawn
Reply With Quote
  #4 (permalink)  
Old 08-27-2008
JOHN DILLON
 
Posts: n/a
Default Re: [PHP] Re: Variable name as a string

Perhaps this example may help. Eg: a form with checkboxes and submit
button, a few are checked and I want to delete the corresponding records
from the database. The database table has an ID column:

for each ($argv as $key=>$value) {
//$key is named cb_1 $value is "checked"
//to get 1 from the key name, then
//needed: function like nameof()
$var=nameof($key);
$ID=substr($var,3);
$query="delete * from dbtable where ID='$ID'";
//etc
}



>
> For database use I do the following in forms:
>
> name="data[field_name]"
>
> Or for multiple table that you update together:
>
> name="data[table_name][field_name]"
>
> -Shawn
>



Reply With Quote
  #5 (permalink)  
Old 08-27-2008
tedd
 
Posts: n/a
Default Re: [PHP] Re: Variable name as a string

At 4:55 PM +0100 8/27/08, JOHN DILLON wrote:
>Perhaps this example may help. Eg: a form with checkboxes and
>submit button, a few are checked and I want to delete the
>corresponding records from the database. The database table has an
>ID column:
>
>for each ($argv as $key=>$value) {
> //$key is named cb_1 $value is "checked"
> //to get 1 from the key name, then
> //needed: function like nameof()
> $var=nameof($key);
> $ID=substr($var,3);
> $query="delete * from dbtable where ID='$ID'";
> //etc
>}



Okay, your data is coming in from a form and you want to translate
that data to a php array -- here's how to do it:

In your form you use:

<input type="checkbox" name="a1">
<input type="hidden" name="whatever1" value="103">

<input type="checkbox" name="a2">
<input type="hidden" name="whatever2" value="206">

<input type="checkbox" name="a3">
<input type="hidden" name="whatever3" value="1187">

<input type="checkbox" name="a4">
<input type="hidden" name="whatever4" value="6101">

In your receiving php script, you use:

for ($i = 1; $i <= 4; $i++)
{
$a = 'a' . $i;
$b = 'whatever' . $i;
if($_POST[$a] == 'on')
{
my_array[] = $_POST[$b]
}
}

If a user clicks any/all of the checkboxes, then those checkboxes
will be turned 'on' and the values associated with the hidden fields
will come into play and be recorded in the my_array[].

A "count(my_array)" will provide you with the number of checkboxes
that were actually checked.

Sure you can do this in while statements if you wish, but the idea of
how to translate checkboxes to a php array is here.

The hidden values above could just as easily be values taken from a
database corresponding to record deletions, such as:

<input type="hidden" name="whatever4" value="<?php echo($record_ID");?>">

The point is that you can determine what the user clicked and tied it
to whatever you presented.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 02:41 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0