Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/56176-php-control-statement-help-needed/
Share on other sites

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'.

 

 

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.

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/>";
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.