Jump to content

Recommended Posts

Hi, I would like to generate ONE question for a user whenever he visits my page. The questions r stored in the database. And the user will hv to enter his email which is stored in the database. If the email is entered the second time, a different question will be given.

 

so my question is, how do i make sure that the same question will not b repeated if the same email is entered? any suggestions? do advice pls...thanks!

Link to comment
https://forums.phpfreaks.com/topic/56002-generate-random-questions/
Share on other sites

you can read the questions from teh db into an array, then pick a random value of of that array

 

on keeping questions from being repeated, you will have to think about what time frame do you consider a question repeated, if one day? one page visit? or never?

 

Once you answer that, you can construct an algorithim to achieve "not repeating", but storying or keeping track of "viewed" questions etc...

I think basically you want to use relationships. Create seperate tables for questions and answers. Give 'questions' a field called "Qid". Give answers a field "Qlink". Then for each answer enter the Qid of the question it belongs to into Qlink. Then you can lookup both questions and answers from 1 ID variable.

hey grego, thanks for the idea! it sounds great!

 

what do u think of this?

 

SELECT q.quest, answ FROM questions q, answers a WHERE q.id = a.id ORDER BY rand() LIMIT 1;

 

from this sql, i hope it can select only one set of question n answer randomly. is it correct?

 

*modified code

 

I would do them seperately, just to space it all out. My personal way would be:

Qid=rand(0,5) //where 5 is the highest Qid
$resQ=mysql_fetch_array(mysql_query("SELECT * FROM questions WHERE Qid='$Qid'"));
$resA=mysql_query("SELECT * FROM answers WHERE Qlink='$Qid'");

echo "<strong>" . $resQ['caption'] . "</strong><br />";
while ($resA=mysql_fetch_array($resAt) {
echo $resA['caption'] . "<br />";
}

 

This produces:

Question

Answer1

Answer2

Answer3

 

And this assumes that "caption" in both tables contains the text to be displayed.

I think you're SQL may well be fine. Though I'm not sure you can address two tables at once (I'm quite new to this)

 

The "caption" thing: In the 'questions' table, there should be a column called "caption" which contains the question itself, eg: "How many sides does a pentagon have?". The "caption" column in 'answers' should contain the answer, eg "5".

 

Alternatively, you could store all the answers in a field in the 'question' table as comma-seperated-values ("5,8,9,I don't know") and then explode() them. In retrospect, that could work much better.

just curious, say i want to enter a really long text into mysql such as this

 

Then you’ll surely be able to spot the missing 3rd line for this stanza from Material Girl – Madonna:

 

Some boys romance, some boys slow dance

That’s all right with me

Have to let them be

 

i think it is not possible to enter it in that form right? u need to format it using php after extracting it?

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.