Help with loop code(MySQLdb)
Posted: Mon Oct 21, 2002 9:45 pm
I'm creating a chemistry quiz on Elements and Oxidation numbers (mainly for programming practice). Here's what I have so far:
view the output here: http://24.116.249.60/adam/chemistry.php
I'm using a mysql database to store the correct values(obviously)and checking them against what the user enters.
It works but there are a few bugs. I would like it to display just two input boxes(instead of two for each element in the mysql table), then switch the elements each time the user gets it right. I know this has something to do with the while loop but I can't seem to figure it out.
Thanks for your help!
Code: Select all
$dbconn = mysql_connect("localhost", "$dbuser", "$dbpass");
$result = mysql_select_db("Chemistry", $dbconn) or die(mysql_error());
$sql = "SELECT Abbreviation, Name, Oxidation FROM elements";
$result = mysql_query( $sql );
if ( $result == false)
echo mysql_error();
$num = mysql_numrows($result);
$i = 0;
while ($i < $num)
{
$abbrev = mysql_result($result,$i,"Abbreviation");
$answer_name = mysql_result($result,$i,"Name");
$answer_oxid = mysql_result($result,$i,"Oxidation");
echo '<br><br>Abbreviation: <b>'.
$abbrev.
'</b><form method="post" action="chemistry.php">
Name:<input type="text" name="ename"></input>
Oxidation Numbers:<input type="text" name="oxid"></input>
<input type="submit" value="click" name="check"></input>
</form>';
if ($ename == "" && $oxid == "")
echo 'Please enter the answers<br><br>';
else
{
if ($ename == $answer_name && $oxid == $answer_oxid)
{
echo 'Congagulations, you got it right<br><br>';
}
else
echo 'Try again!<br><br>';
}
++$i;
}I'm using a mysql database to store the correct values(obviously)and checking them against what the user enters.
It works but there are a few bugs. I would like it to display just two input boxes(instead of two for each element in the mysql table), then switch the elements each time the user gets it right. I know this has something to do with the while loop but I can't seem to figure it out.
Thanks for your help!