Page 1 of 1
variable variables
Posted: Mon Oct 06, 2003 10:08 am
by mathewvp
Hi,
I am migrating from an older php version which is causing me a lot of problems.
I have a question answer form where the admin can enter five questions and 4 answers each and a radio group for each question for selecting the correct answer
For eg
Question1-textbox for question
Choice1 -text box with name choice1
Choice2 -text box with name choice2
Choice3 -text box with name choice3
Choice4 -text box with name choice4
Choice5 -text box with name choice5
Select the correct answer-radio box with name answer1 and these values
1)choice1 2)choice2 3)choice3 4)choice4 5)choice5
And once the form is submitted,I could find the answer that the admin selected using
$ans1=${$answer1};
and similarly for the answers for remaining questions
Now this doesn't work with newer version of php.So can anybody tell me how to solve this.
Thanks
Posted: Mon Oct 06, 2003 10:15 am
by Nay
$ans1=${$answer1};
I don't really get what your trying to do but if that's an array, it should be something like:
-Nay
Posted: Mon Oct 06, 2003 10:20 am
by volka
Nay:
Code: Select all
<?php
$a = 'lalelu';
$answer1 = 'a';
echo ${$answer1};
?>
http://www.php.net/manual/en/language.variables.variable.php
mathewvp: Could you post the previously working code? I'm a bit confused about
$answer1 
Posted: Mon Oct 06, 2003 10:26 am
by Nay
waoh! ${$answer1} is actually correct? I see

. Sorry, my bad.
Where's Derfel? hehe
-Nay
Posted: Mon Oct 06, 2003 11:18 am
by mathewvp
ok guys here's the code
<b>Enter question1 :</b></td><td><input type="text" name="question1" size="60"></td>
<tr><td> Enter choice 1:</td><td><input type="text" name="choice1" size="60"></td>
<tr><td> Enter choice 2:</td><td><input type="text" name="choice2" size="60"></td>
<tr><td>Enter choice 3:</td><td><input type="text" name="choice3" size="60"></td>
<tr><td>Enter choice 4:</td><td><input type="text" name="choice4" size="60"></td>
</tr></table>
Answer
<input type="radio" name="answer1" value="choice1">Choice1
<input type="radio" name="answer1" value="choice2">Choice2
<input type="radio" name="answer1" value="choice3">Choice3
<input type="radio" name="answer1" value="choice4">Choice4
Now if the admin chooses Choice1 an answer,then $answer1 contains "choice1" and ${$answer1} gives the correct answer,got that?
Now the problem is in newer php versions regiser golbals is set to off in the PHP.ini file.I cannot make any changes to the php.ini file coz its not my server and I cannot ask the admin of that server to do that.
I am looking for a solution for implementing that code in some other way.Any help?
Posted: Mon Oct 06, 2003 11:37 am
by volka
Only was curious what
$answer1 exactly was

try
Code: Select all
<html>
<head></head>
<body>
<?php
if(!isset($_POST['set']) || !is_array($_POST['set']))
{ ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
Enter question1 :<input type="text" name="set[0][question]" size="60" /><br />
Enter choice 1:<input type="text" name="set[0][choice][0]" size="60" /><br />
Enter choice 2:<input type="text" name="set[0][choice][1]" size="60" /><br />
Enter choice 3:<input type="text" name="set[0][choice][2]" size="60" /><br />
Enter choice 4:<input type="text" name="set[0][choice][2]" size="60" /><br />
Answer
<input type="radio" name="set[0][answer]" value="0" />Choice1
<input type="radio" name="set[0][answer]" value="1" />Choice2
<input type="radio" name="set[0][answer]" value="2" />Choice3
<input type="radio" name="set[0][answer]" value="2" />Choice4
<hr />
Enter question1 :<input type="text" name="set[1][question]" size="60" /><br />
Enter choice 1:<input type="text" name="set[1][choice][0]" size="60" /><br />
Enter choice 2:<input type="text" name="set[1][choice][1]" size="60" /><br />
Enter choice 3:<input type="text" name="set[1][choice][2]" size="60" /><br />
Enter choice 4:<input type="text" name="set[1][choice][2]" size="60" /><br />
Answer
<input type="radio" name="set[1][answer]" value="0" />Choice1
<input type="radio" name="set[1][answer]" value="1" />Choice2
<input type="radio" name="set[1][answer]" value="2" />Choice3
<input type="radio" name="set[1][answer]" value="2" />Choice4
<hr />
<input type="submit" />
</form>
<?php
}
else
{
foreach($_POST['set'] as $set)
{
?>
<fieldset><legend><?php echo $set['question']; ?></legend>
<?php foreach($set['choice'] as $num=>$choice) { ?>
<div style="border: 1px solid <?php echo ($num==$set['answer'])? 'red':'silver'; ?>" >
<?php echo htmlentities($choice); ?>
</div>
<?php } ?>
</fieldset>
<?php
}
}
?>
</body>
</html>