jhl84 Posted June 19, 2007 Share Posted June 19, 2007 Hi...I am building a page that displays random questions to visitors. At the first page, the user will enter his email n be directed to another page where a random question from the database will b given. if the same email address is entered, the question that was given will not be repeated n other question will b given instead. all the questions n answers r stored in the database with a unique id. so far this is what i hv done. extract ($_POST); $id = rand(1, 14); $query = ("SELECT * FROM email WHERE email = $email"); if (($query['email'] == $email) && ($query['no'] == $id)) { } else { $query = ("INSERT INTO email VALUES ('$id', '$email')"); $result = mysql_query($query); $resQ = mysql_fetch_array(mysql_query("SELECT * FROM question WHERE qid = $id")); $resA = mysql_fetch_array(mysql_query("SELECT * FROM answer WHERE aid = $id")); echo $resQ['quest']."<br/>"; } i hope i'm not confusing u guys. do advice Quote Link to comment https://forums.phpfreaks.com/topic/56176-php-control-statement-help-needed/ Share on other sites More sharing options...
MasterACE14 Posted June 19, 2007 Share Posted June 19, 2007 I don't understand ??? , are you recieving an error? or do you just need help finishing the code? Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/56176-php-control-statement-help-needed/#findComment-277512 Share on other sites More sharing options...
jhl84 Posted June 19, 2007 Author Share Posted June 19, 2007 yea i need help finishing the code.... u see, i need that query the database for the email whether it exists, if it does, then checks the 'no' field in the email table. if the 'no' is same with the random $id generated, then it will regenerates another $id until it is different. if (($query['email'] == $email) && ($query['no'] == $id)) { //the problem is here } i wan it to continue regenerating $id until it is different. the email table in db consists of 'no' and 'email'. Quote Link to comment https://forums.phpfreaks.com/topic/56176-php-control-statement-help-needed/#findComment-277585 Share on other sites More sharing options...
thefortrees Posted June 19, 2007 Share Posted June 19, 2007 The first problem I see is with the if statement: $query = ("SELECT * FROM email WHERE email = $email"); if (($query['email'] == $email) && ($query['no'] == $id)) { } That will not work. You haven't queried the database to check those fields in the table. You need to do like you did in the else statement. Read up on mysql queries. To keep generating another random $id, you can use a loop . Read up on those. http://hudzilla.org/phpwiki/index.php?title=Loops $id = rand(1, 14); $query = "SELECT * FROM email WHERE email = '$email'"; $result = mysql_query($query); $row = mysql_fetch_array($result); if ($row['email'] == $email){ while ($row['no'] == $id){ $id = rand(1, 14); } } Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/56176-php-control-statement-help-needed/#findComment-277599 Share on other sites More sharing options...
jhl84 Posted June 19, 2007 Author Share Posted June 19, 2007 what do u guys think of this? extract ($_POST); $query = ("SELECT * FROM email WHERE email = $email"); $result = mysql_query($query); if (mysql_num_rows($result) == 14) { // Already answered all questions } else { // generate random no until found question do { $id = rand(1, 14); $query = ("SELECT * FROM email WHERE email = $email AND qid = $id"); $result = mysql_query($query); } while (mysql_num_rows($result) <= 0); $id = rand(1, 14); $query = ("INSERT INTO email VALUES ('$id', '$email')"); $result = mysql_query($query); $resQ = mysql_fetch_array(mysql_query("SELECT * FROM question WHERE qid = $id")); $resA = mysql_fetch_array(mysql_query("SELECT * FROM answer WHERE aid = $id")); echo $resQ['quest']."<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/56176-php-control-statement-help-needed/#findComment-277670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.