Page 1 of 1

Help with loop code(MySQLdb)

Posted: Mon Oct 21, 2002 9:45 pm
by anomaly
I'm creating a chemistry quiz on Elements and Oxidation numbers (mainly for programming practice). Here's what I have so far:

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)
&#123;

	$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>&nbsp;
		Oxidation Numbers:<input type="text" name="oxid"></input>&nbsp;
		<input type="submit" value="click" name="check"></input>
		</form>';
	
if ($ename == "" && $oxid == "")
	echo 'Please enter the answers<br><br>';
else
&#123;
	if ($ename == $answer_name && $oxid == $answer_oxid)
	&#123;
		echo 'Congagulations, you got it right<br><br>';
		
	&#125;
	else
		echo 'Try again!<br><br>';

&#125;

++$i;

&#125;
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!

Posted: Tue Oct 22, 2002 1:12 am
by volka
try it with

Code: Select all

$sql = "SELECT Abbreviation, Name, Oxidation FROM elements ORDER BY RAND() LIMIT 0,2";
mysql will provide two random entries of your table - maybe the same as last time (but that's random ;) )

Posted: Tue Oct 22, 2002 7:24 am
by anomaly
I added that, and changed limit to 1.

It now only shows one, but the element doesn't change after the user has got it right.

Posted: Tue Oct 22, 2002 7:30 am
by anomaly
Ok, i now added

$result = mysql_query( $sql );

inside the while loop, and it will switch the elements if i refresh the page. Pretty close to what I want...