Forum Moderators: coopster
<!-- begin
if ($q#==$a#) {
$c=($c+1);
$v#="1";
}
end -->
X amount of times where # represents the number of the loop... if X=3 for example it would act like this
<!-- begin
if ($q1==$a1) {
$c=($c+1);
$v1="1";
}
if ($q2==$a2) {
$c=($c+1);
$v2="1";
}
if ($q3==$a3) {
$c=($c+1);
$v3="1";
}
END -->
This would be of great help because the full script is used to validate exercises on a school study site
this needs to be in a php file and use php functions so that doesn't work
Ehm, yes... And the code I wrote was meant to be in that PHP script...
<?php
for($i=1; $i<=$x; $i++) {
echo "if (\$q$i==\$a$i) {
\$c=(\$c+1);
\$v$i="1";
}
";
}
?>
If $x is 4 it will print:
if ($q1==$a1) {
$c=($c+1);
$v1="1";
}
if ($q2==$a2) {
$c=($c+1);
$v2="1";
}
if ($q3==$a3) {
$c=($c+1);
$v3="1";
}
if ($q4==$a4) {
$c=($c+1);
$v4="1";
}
So, I don't understand why it doesn't work?
<?php
for($i=1; $i<=$x; $i++) {
[b]eval([/b]"if (\$q$i==\$a$i) { \$c=(\$c+1); \$v$i=\"1\";}"[b])[/b];
}
?> (just added it to drdocs code and escaped the last two ", because they weren't.
my example is just a overhead for the script. even the php-developers optimized the eval function, it could be done direct instead:
<?php
for($i=1; $i<=$x; $i++) {
$v_q = '$q'.$i;
$v_a = '$a'.$i;
$v_v = '$v'.$i;
if ($$v_q==$$v_a) { $c++; $$v_v="1"; }
}
?> the previously posted code was just to point out, that eval might be used and is powerfull (like variable variables, too).
thanks but i've tried all of these snippets of code and each one returns a Parse Error. I've never worked with loops before so this is all really new to me and i cant debug it. here are copies of all the files used...<!-- begin a sample form that points to check.php
<form name="ex" method="post" action="../check.php">
<input type=hidden name=fldr value=formal>
<input type=hidden name=ex value=ex1>
<p>Du, ihr, oder Sie?: which word would be used to properly address each of the following?</p>
<br>
1)Your mom:<br>
<input type="radio" name="q1" value="Du">Du<br>
<input type="radio" name="q1" value="Ihr">Ihr<br>
<input type="radio" name="q1" value="Sie">Sie<br>
<br>
2)The school's German teacher:<br>
<input type="radio" name="q2" value="Du">Du<br>
<input type="radio" name="q2" value="Ihr">Ihr<br>
<input type="radio" name="q2" value="Sie">Sie<br>
<br>
3)Rufus the fat llama:<br>
<input type="radio" name="q3" value="Du">Du<br>
<input type="radio" name="q3" value="Ihr">Ihr<br>
<input type="radio" name="q3" value="Sie">Sie<br>
<input type=submit value="Check my answers already!"><input type=reset value="I messed up...reset it">
--><!-- begin answer file called by include("$fldr/$ex.php");
<?
$x=3
// x is the number of questions
$tut="Formal/Familiar: exercise 1";
// just a name
$a1="Du";
// question 1's answer
$a2="Sie";
// question 2's answer
$a3="Du";
// question 3's answer
?>
--><!-- begin file: check.php
<?
if ($fldr==""){
echo "<body onload=\"(window.location='index.htm')\">";
}
$c=0;
include("$fldr/$ex.php");//////////////////////////////////////////////////////////
//
// THIS IS WHERE THE CODE I'M LOOKING FOR GOES
//
/////////////////////////////////////////////////////////?>
<html><head><title><? echo $tut;?></title></head>
<body bgcolor=black text="d0d0d0">
<center><h1>Results</h1><br><h2><? echo $tut;?></h2><hr width=80%>
You scored <b><font color="00cccc"><? echo $c;?> out of 7</font></b>.</center><hr width=80%>
<?
///////////////////////////////////////////////////////////
//
// A MODIFIED VERSION OF THE CODE WOULD GO HERE
// IT WOULD ECHO THE RESULTS OF EVERY QUESTION
// EX: echo "you got question 1 right/wrong"
// I can write this part of the code...
// After i see how the other code was written
//
//////////////////////////////////////////////////////////
?>
-->the form would be in
/formal/ex1.htmthe php answer file
/formal/ex1.phpthe check.php file
/check.phpI know this is a huge problem but I know very little about php