Basically, we are using 2 SELECT statements and use UNION to combine them into one result set.
SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10
The above Query will return 10 random rows of type mathnocalc. That you will be sure about. I am using the above query as nested one to UNION with another one.
SELECT * FROM (SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10) as cat1
UNION
SELECT * FROM (SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathcalc' ORDER BY RAND() LIMIT 0,10) as cat2
Try the above query and let me know the result.
For displaying the results based on categories, you can simply split the query into two:
Example Logic:
$queryNoCalc = 'SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathnocalc' ORDER BY RAND() LIMIT 0,10';
$noCalcResult variable will store the result set of above query.
Similarly,
$queryCalc = 'SELECT question, type, Topic, Skill, imagename, answerA, answerB, answerC, answerD, correctanswer FROM goodquestions WHERE type = 'mathcalc' ORDER BY RAND() LIMIT 0,10';
$calcResult variable will store the result set of above query.
Now, you can use the seperate results in different DIV.
<div class="no-calc">
<h3>NO CALULATOR ALLOWED</h3>
while ($noCalcResult):
Do the stuff;
endwhile;
</div>
<div class="calc">
<h3>CALULATOR ALLOWED</h3>
while ($queryCalc):
Do the stuff;
endwhile;
</div>
Hope you understand the concept.
Problem :
I have the code below. Essentially, it takes math questions from a database and lays them out for an user to answer. There are two sections: mathcalc and mathnocalc. Right now, the program outputs both of them randomly.
Sometimes there can be 15 mathcalc and only 5 mathnocalc. This is a problem to me. I need to be able to specify that the program should only output 10 mathcalc and only 10 mathnocalc.
Furthermore, I want to be able to split up the output so that the first page is only mathcalc (it can be more than 1 page. I just need to print out all the mathnocalc first and then print out all the mathcalc after. essentially two groups).
I was wondering how I would incorporate that with my existing code. This is definitely challenging but I was wondering if anyone would be able to help me.
What is the problem with the current output? And how your expected output look like?
Comment posted by Rushi M
@tcadidot0 the current output spews out both mathcalc and mathnocalc without any structure. I would like my output to print out ALL the mathcalc questions first and THEN the mathnocalc questions
Comment posted by stackoverflow.com/questions/28857920/…
This should be what you are looking for.
Comment posted by stackoverflow.com/questions/31495446/…
As an aside, seriously consider whether your design is optimal. ‘Normal’ly, we’d have a separate table for answers, with a row for each answer, a column indicating if a given answer is correct, and a column indicating to which of ‘A’, ‘B’, ‘C’, or ‘D’ the answer belongs.
Comment posted by Rushi M
check out my comment
Comment posted by Harish ST
If you are having trouble following nested queries then why not try seperate query for each type and dispaly it inside corresponding Divs? Like Create Two Divs with Headings on it, then store the query results in seperate variables and loop through the result seperately. I know this is not optimized solution, but it would make it simple and easier to understand, so that you can build on top of it. Please let me know if you have any queries.
Comment posted by Rushi M
not quite sure how I would do this. Do you mind editing?