samedi 21 février 2015

php mysql return variable amounts

Let me start off first by saying I code php as a hobby and don't know a whole lot. That be said I'm having trouble with coming up with a clever way to perform a particular type of query.


Say I have dynamic quiz application that uses a database of test questions of three different subjects; 200 algebra, 100 history, 50 science. The user can select which and how many subjects they wish to have in their quiz and also choose how many questions that will be in the quiz.


Below is the working code I'm using now...



$sql = "SELECT *
FROM quiz
WHERE keyword LIKE '$algebra'
OR keyword LIKE '$history'
OR keyword LIKE '$science'
ORDER BY RAND() LIMIT $amount";


However the only issue with this code is that if I select 10 questions on my test I will have mostly algebra questions, a few history and no science questions. I'd like it so each subject selected is represented equally.


I think I might have bitten off more than I can chew with this project.





Aucun commentaire:

Enregistrer un commentaire