Re: [PHP4BETA] how to get the name of a variable?
| From: | Simon Roberts | Date: | Tue, 15 Feb 2000 04:37:10 +0000 |
| Subject: | Re: [PHP4BETA] how to get the name of a variable? | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-10284@lists.php.net to get a copy of this message | ||
The main problem with your approach is that PHP variables cannot start with a digit. Prefix the name
attribute with (say) a letter:
<input type="radio" name="a1" value="pos">
<input type="radio" name="a1" value="neg">
Then in the PHP code, the variable $a1 will contain either "pos" or "neg" (if
the radio button is selected, obviously).
To iterate through a1 to a10 use something like :
for ( $i=1; $i<=10; $i++ )
{
$varname = "a$i";
echo "a$i = " . $$varname . "<br>";
}
There are slightly nicer ways to do that in PHP4, like foreach() and while( list($k,$v) = each(
$HTTP_POST_VARS ) ) { ... }, but I'll leave that as an excercise for the reader :)
Cheers, Simon
----- Original Message -----
From: Juan Luis Baptiste
To: php4beta@lists.php.net
Sent: Tuesday, February 15, 2000 5:18 PM
Subject: [PHP4BETA] how to get the name of a variable?
Hi, I got varius radio buttons on a form like this:
<input type="radio" name="1" value="pos">
<input type="radio" name="1" value="neg">
<input type="radio" name="2" value="pos">
<input type="radio" name="2" value="neg">
<input type="radio" name="3" value="pos">
<input type="radio" name="3" value="neg">
.......
They are a lot of them so I want to do something like a loop for getting the radio buttons values
into a database.
Is there a way of get the names of the variables?
For example, I got this reult from the form:
$1="pos";
$2="neg";
$3="pos";
.... and so on...
the table have three columns--> id,exam_number, value. The values of the radio buttons have to
be on the table like this:
ID | exam_number | value
----------------------------------------
101 | 1 | pos
102 | 2 | neg
103 | 3 | pos
......
'ID' is an auto increment value, 'exam_number' is the name of the radio button
variable and 'value' is the value of the radio button selected.
Any ideas?
Thanks,