jhl84 Posted June 18, 2007 Share Posted June 18, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/ Share on other sites More sharing options...
dsaba Posted June 18, 2007 Share Posted June 18, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276618 Share on other sites More sharing options...
jhl84 Posted June 18, 2007 Author Share Posted June 18, 2007 i hv a set of questions n each questions has a different choice of 4 answers. how do i group the question n answers together? Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276627 Share on other sites More sharing options...
Grego Posted June 18, 2007 Share Posted June 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276641 Share on other sites More sharing options...
jhl84 Posted June 18, 2007 Author Share Posted June 18, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276653 Share on other sites More sharing options...
Grego Posted June 18, 2007 Share Posted June 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276656 Share on other sites More sharing options...
jhl84 Posted June 18, 2007 Author Share Posted June 18, 2007 i'm sorry but i dont understand wat the caption is? thanks for ur help. btw, i would like to know wat u think of the sql i wrote? do u think it will work as nicely as urs? Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276664 Share on other sites More sharing options...
Grego Posted June 18, 2007 Share Posted June 18, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-276667 Share on other sites More sharing options...
jhl84 Posted June 19, 2007 Author Share Posted June 19, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/56002-generate-random-questions/#findComment-277292 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.