Complicated (Well I think so) Loop!

I am currently phping a site to test people on latin (not that this really matters!). I have programmed it so far so that words are stored in a mysql database and that a function test() displays a(depending on it’s id, starting at 1) latin word with a question mark next to a field. If you type in the correct english word (the translation) then it echos ’ RIGHT!’ and ‘WRONG!’ if not. The problem is I cannot work out how to move it onto the next word in the database. It needs a loop of some kind, I have tried and failed though:
This is the index page:

[code]<? include('header.php'); ?>

<?

$id = 1;
{
test($id);
if ($_POST)
{

if ($alex == 1)
{
echo ‘RIGHT!’;
}
else
{
echo’ WRONG!’;
}
}
}

?>

<input type="text" name="eng" size="40" class="field" value=""
[/code]

This is the function (test)(in header.php):

[code]<?
include “config.php”;

function test($r)
{

include(‘config.php’);

$sql = “SELECT * FROM tb_vb1 WHERE id=’$r’”;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$lat = $row[“lat”];
mysql_close($con);
echo “What is the English for $lat?”;

include(‘config.php’);

	$sql = "SELECT * FROM tb_vb1 WHERE id='$r'";
	$result = mysql_query($sql);        
	$row = mysql_fetch_array($result);
	$eng = $row["eng"]; 
	mysql_close($con);

global $alex;
if ($_POST[‘eng’] == $eng)

{
$alex = true;
}
else
{
$alex = false;
}
return;
}
?>[/code]

Thank you, you can actually find the site if you go here: http://www.teapay.com if you want to see what I mean!

Hello,
The problem is you need to increment the argument to test with every call to the page. I suggest going this route

where you have $id = 1;
replace it with

if ($_POST) { $id = $_POST['id']+1; } else { $id = 1; }
You’ll also need to add a hidden element to your form. Something like:

<input type=hidden value='<? echo $id; ?>'>

good luck!

Thanks, that did help a bit. The first bit of the code you gave me is very good, but the hidden form doesn’t work, (I had to correct your syntax anyway) but for some reason it messes up the whole system even when the syntax is correct. It just says WRONG everytime! Or it doesn’t move on to the next id! I also made it this (I think you just forgot) :

<input type=hidden value="<? echo "$id"; ?>"name="id">

Please help me, I still can’t get it to work!

First are you doing anything with the id? You must increment it to then use that to get the next data.

Next, if the line above is what you have in your code… it too is wrong. lets say for example your $id was equal to 11 your outputted line would be <input type=hidden value="11"name=“id”>

Note there is NO GAP/SPACE between the 11 and NAME this could cause a problem.
You also don’t need to quote $id in the expression as well.

Try it like this:

<input type=hidden value="<? echo $id; ?>" name="id">

And again, make sure you are using the ID to (in conjunction with the rest of your code) to decide how or when and where to increment/decrement your values.

I don’t understand, I have tried to take your suggestions into account but they have not worked, I have changed the php, it hasn’t got a function now, its all one text. Why does it always just say “WRONG!”?

Well, here it is:

[code]<? include('header.php'); ?>

<? if ($_POST) { $id = $_POST['id']+1; } else { $id = 1; } include('config.php'); $sql = "SELECT * FROM tb_vb1 WHERE id='$id'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $lat = $row["lat"]; mysql_close($con); echo "What is the English for $lat?"; include('config.php'); $sql = "SELECT * FROM tb_vb1 WHERE id='$id'"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $eng = $row["eng"]; mysql_close($con); if ($_POST['eng'] == $eng) { $alex = true; echo 'Right!'; } else { $alex = false; echo 'Wrong!'; } ?>

" name="id">
<input type="text" name="eng" size="40" class="field"
[/code]

Part of the problem is that you are incrementing ID before you check the answer against the database.

You need to pass the ID of the word you are “Checking”. Once it’s succesfully checked and you are going to present the user with the NEXT word.

Your logic should go something like follows (this assumes that you are submitting the form to itself.)

[ul]- Present the form If no ID “Posted” then ID =1

  • If “Submitted” Get the “English” word out of the database for ID that was POSTED during the SUBMIT
  • If the POSTED English word matches the Database English word (THIS IS CASE SENSITIVE, if you don’t want this… Make them all upper or all lower and then compare) Then echo success
    [list]- Increment the ID
  • query the Database for the NEXT word based on the new id
  • present the form[/list]
  • If the POSTED English word does NOT match the Database English word (AGAIN, THIS IS CASE SENSITIVE) Then Echo failure
    [list]- Re-present the “Errored” word for another attempt[/list][/ul]
Sponsor our Newsletter | Privacy Policy | Terms of Service