Page 1 of 1

Post a loop generated variable

Posted: Fri Dec 05, 2003 5:42 am
by Bamatick
I have a php file that generates 18*6 (108) text boxes. On submit, it goes to another page that evaluates what was put in the text box as matching items in a db. The problem is that the second page only gets the last item in the list. I need ALL the items to be passed, not just the last one. In advance, I appreciate your help! Here is the first page (page 2 is down below):


<?
$db_name = "whatever";
$connection = @mysql_connect("server","user","password")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$i=0;
$j=0;
$k=0;
$m=0;
$n=0;
$x=1;
$submitButton="";

for ($j=1; $j<=6; $j++){
for ($i=1; $i<=18; $i++){
if ($j==1){
$k='ch1Key';
$table_name = 'ch1';
$m='ch1Column';
$n='ch1English';
}
else if ($j==2){
$k='ch2Key';
$table_name = 'ch2';
$m='ch2Column';
$n='ch2English';
}
else if ($j==3){
$k='ch3Key';
$table_name = 'ch3';
$m='ch3Column';
$n='ch3English';
}
else if ($j==4){
$k='ch4Key';
$table_name = 'ch4';
$m='ch4Column';
$n='ch4English';
}
else if ($j==5){
$k='ch5Key';
$table_name = 'ch5';
$m='ch5Column';
$n='ch5English';
}
else if ($j==6){
$k='ch6Key';
$table_name = 'ch6';
$m='ch6Column';
$n='ch6English';
}

$sql = "SELECT * FROM $table_name";
$result = @mysql_query($sql,$connection) or die(mysql_error());
$num_rows = mysql_num_rows($result);

$myRandomNumber= ((rand()% $num_rows)+1);

$sql = "SELECT * FROM $table_name WHERE $k= $myRandomNumber";

$result = @mysql_query($sql,$connection) or die(mysql_error());

$row = mysql_fetch_array($result);
$primeKey=($row[$k]);
$spanishWord = stripslashes($row[$m]);
$englishWord = stripslashes($row[$n]);

if ($x==108){
$submitButton="<center><input type=\"submit\" value=\"Submit Test\"></center>";
}
$form_block = "
<form method=\"post\" action=\"fileX.php\">
$x). $spanishWord<br>
<input type=\"hidden\" size=\"50\" value=\"$englishWord\" name=\"eWord\">
<input type=\"hidden\" size=\"5\" value=\"$x\" name=\"xx\">
English:
<input type=\"text\" size=\"50\" value=\"\" name=\"sWord\"><br><br>
$submitButton
</form>
";
$x=$x+1;
echo "$form_block";

}}

?>


Here is the second page:
<?
$alpha=$_POST['eWord'];
$beta=$_POST['sWord'];
$xx=$_POST['xx'];
$message="";

if ($alpha==$beta){
$message="Correct!";}

else if($alpha!=$beta){
$message="Wrong!";}

$form_block = "
<center><h1>Results:</h1></center>
<form>
$xx).<b>$message</b> (Your Answer: <b>$beta</b> The Correct Answer: <b>$alpha</b>)<br>
</form>

";

echo "$form_block";


?>

Posted: Fri Dec 05, 2003 5:51 am
by JayBird
won't each of your text fields need a unique name? at the moment, they are all called sWord.

Mark

Precisely

Posted: Fri Dec 05, 2003 8:29 am
by Bamatick
So, how do I give each one a unique name? I do NOT want to name each one by hand. Any suggestions?

Posted: Fri Dec 05, 2003 8:31 am
by Draco_03
Use regular expression to change the others
http://www.amk.ca/python/howto/regex/

Posted: Fri Dec 05, 2003 9:31 am
by Chambrln
in your loop when you display the text box just add a number to the end of the name. so you would have sWord1 - sWord108. Then when you evaluate them on the next page you already know the range of names.