Forum Moderators: coopster

Message Too Old, No Replies

loops?

help with loops

         

penguinpage

1:26 am on Jul 18, 2003 (gmt 0)

10+ Year Member



I need a script that will write....

<!-- 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

DrDoc

1:42 am on Jul 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo "<!-- begin

";

for($i=1; $i<=$x; $i++) {
echo "if (\$q$i==\$a$i) {
\$c=(\$c+1);
\$v$i="1";
}

";
}

echo "end -->
";

penguinpage

1:57 am on Jul 18, 2003 (gmt 0)

10+ Year Member



this needs to be in a php file and use php functions so that doesn't work

Glacai

2:53 am on Jul 18, 2003 (gmt 0)

10+ Year Member



My php is not too good but in c you would need to use an array, php maybe..

for($i=0; $i<$x; $i++) {
if($q[i] == $a[i]) {
$c++;
$v[i] = "1";
}
}

jamesa

7:11 am on Jul 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like a job for variable variables [php.net]

jatar_k

4:46 pm on Jul 18, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member




$counter = 0;
while (isset(${"q" . $counter})) {
if (${"q" . $counter}==${"a" . $counter}) {
$c=($c+1);
$v1="1";
}
$counter++;
}

the test in the while loop may be wrong but the method is correct.

DrDoc

6:28 am on Jul 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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?

vincevincevince

1:32 pm on Jul 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



drdoc, i believe what penguinpage means is that the code that he wants returned must be run - he's not outputting code for people to look at - he wants to generate that code to run in php

drdoc's code will work if you buffer the output and write it to a file.php then include("file.php");

;-)

hakre

2:58 pm on Jul 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



... or you use eval() [php.net] instead. then you won't need to save a file at all or buffer the output:

<?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.

DrDoc

4:17 pm on Jul 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's a really nice solution, hakre. And thanks for spotting the problem with the quotation marks :)

hakre

4:36 pm on Jul 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



nice solution maybe but, the eval on that place is really - ehm let's say - nuts.

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).

penguinpage

8:30 pm on Jul 23, 2003 (gmt 0)

10+ Year Member



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&nbsp;&nbsp;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.htm

the php answer file
/formal/ex1.php

the check.php file
/check.php

I know this is a huge problem but I know very little about php