variable variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

variable variables

Post 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
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

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

Code: Select all

$answer1=$answers['answer1'];
-Nay
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;-)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

waoh! ${$answer1} is actually correct? I see :P. Sorry, my bad.

Where's Derfel? hehe

-Nay
mathewvp
Forum Commoner
Posts: 28
Joined: Wed Apr 23, 2003 10:28 am

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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