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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 > > |
|
|||
|
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 |
|
|||
|
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 > |
|
|||
|
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 |