Show data from database based on user input - php

I'm creating a way that a "teacher" could make an exam and the "student" can take an exam.
First off, it is also possible for a teacher to make new questions, using this query:
INSERT INTO questions (question, type) VALUES ('$question', '$type')
In the database, I set questions to also have question_id which is auto incremented after each entry. Then on a separate page, they can pick which questions they would like to add to the exam. So I just:
SELECT * FROM questions
Then there is a checkbox for them to check which questions to add the use this query:
INSERT INTO exams (question_id) VALUES ('$question_id')
The table exams also has an auto incremented exam_id.
So now I would like to display the questions the teacher picked, but I don't even know what type I should store question_id in exams (right now it is INT) so I can loop through them.
ie. Teacher picks questions 1,2,4,10 and query for getting the question would look like
SELECT question FROM questions WHERE question_id='1,2,4,10'

Assuming you are getting the question id by POST or GET, Try this:
$selected = implode(',', $_REQUEST['selectedquestionids']);
SELECT question FROM questions
WHERE question_id IN ($selected)
GROUP BY question_id;
Hope this may help.

Related

How to count in 1 sql query the number of rows from an other table

i have a problem. I have a table on my website with 3 columns like this:
This data is out of 2 database tables, the title and yes/no columns are from the table quizzes and the Number of questions is out of the questions table.
I want that users can sort per column, so that you can sort the title asc and desc. I already have this with title and yes/no but not with questions because i dont know how to do that in 1 sql query.
The outcome of that sql query should be the Title, the number of questions that quiz has and if it is yes or no.
If this is to vague please tell me so i will try to write a better explanation. if you need an example of the database please tell me too.
The query could be something like :
SELECT quizes.title, quizes.id, count(q.id) AS aantal
FROM quizes
LEFT JOIN questions AS q ON q.quizid= quizes.id
GROUP BY quizes.title, quizes.id
ORDER BY aantal DESC
edit
changed names of table and columns as comments reveilled them

Count the amount of values in a Mysql table with php

I made a school voting with 10 questions. There you can check one teacher for every question (in total there are 80 teachers). When someone has voted for all questions, a new row in my mysql table will be inserted.
My Mysql Table has 10 fields (for every question one(q1,q2,q3)). The name of the teacher for one question will be an integer (every teachers has an id from 1 - 82).
That is what the table looks like
Now I want to find out for which teacher is how often voted for each question. That should do a php script. It should echo something like this:
The result should be something like this
How to do this?
This query will give number of times each teacher is voted for question q1.
$sql = "SELECT q1, COUNT(*) AS votes FROM table1 GROUP BY q1 ORDER BY votes";
$ref = $result->query($sql);
while($row = mysqli_fetch_assoc($ref))
{
echo "$row[votes] times $row[q1]</br>";
}
You can do the same for other questions, just by changing the column name for question number

Storing more than one string in a MySQL Database Table

Before I ask my question, I just wanted to thank everyone that replied to my question yesterday -> Countdown using javascript.
Idea:
I created a quiz using php, but I would like to create a MySQL Database and having a table with all the questions, answers and multiple choice stored inside.
Issue:
Since the quiz is multiple choice, I don't know how to go about storing the multiple choice options in the table. Could I store the options and have each answer separated by a special character and let php get the string and separate the options?
Ex: Question: What is your favorite color? Options: Blue=Red=Purple=Yellow.(Database View)
What do you folks think is the best practice for something like this?
I think the best practice would be to use multiple tables. One for the question and one for answers. The answer table would contain question_id as well as a flag whether or not it is the correct answer
It could look like this
TABLE questions
FIELDS: id, text
TABLE answers
FIELDS: id, question_id, text, correct
The problem with using one field for all the answers is that you could accidently use the character you use for splitting inside the text of an answer
simply, create two tables:
questions
question_id, question
answers
answer_id, question_id, answer
Now, you can link these two tables using question_id
There are a couple of ways around this :
The "proper" way is to create another table ( so that you have a table called "questions", each of which have a unique id, and another called "answers", where each has the question id )
The "simple" way, which is to use JSON ( see json_encode and json_decode ) which takes care of using special characters etc in a field
You'd have a questions table, like this:
id | question
0 Do you even lift?
id would be INT(11) PRIMARY_KEY AUTO_INCREMENT while question would just be TEXT. Then, you would have an answers table:
id | question_id | answer
0 0 Yes
1 0 No
2 0 Maybe
Here, question_id refers to the ID of the question in the questions table. These answers all belong to one question. This is called a Has many relationship, as one question has many answers.
This is how its usually done. Implementing it is not that hard, even if you're not using a framework (most of them do the work for you).
Hope this helps
Tables:
quiz
quiz id (pk)
quiz info (other columns)
questions
question id(pk)
quiz id
question text
answers
answer id(pk)
question id
answer text
To display a given question do a join on the quiz, question and answers.
EDIT: You could either add a column for 'right/wrong answer (0/1)' or have another table:
solutions
solution id(pk)
question id
answer id
I didn't put the 'correct answer' in the answers table as that's not good normalization.
There could be many possible schema designs for this but my suggestion is like this:
Don't ever store values separated by comma on the tables.
This table holds the quizzes.
Quiz Table
QuizID (PK)
other columns..
This holds the questions for every quiz.
Question Table
QuestionID (PK)
QuestionDetail
QuizID (FK)
other columns...
This holds the answers.
Answer Table
AnswerID (PK)
AnswerDetail
This holds the correct answer for every question on each quiz or in other words, this is the answer key.
Question_Answer_Correct Table
QuestionID (FK) -- also a compound primary key with AnswerID
AnswerID (FK)
This contains list of users.
User Table
UserID (PK)
UserName
other columns...
This contains answer of users on a specified question. There is no QuizID here since the questions are already connected on the quiz table.
User_Answers Table
UserID (FK)
QuestionID (FK)
AnswerID (FK)

MySQL - Show questions not answered before

I am trying to make a quiz that should show questions not answered before by the same user.
Therefore, a session is created when a user starts the quiz.
Here is the situation:
There is a table "Questions"
This table contains all questions. There is a unique field QuestionId.
The questions always have a CategoryId from 1 to 5
There is a table "Answers"
This table contains all answers given by users. The session id is stored in SessionId, the answered question id is stored in QuestionId.
I now am looking for a query that
Fetches a question from Questions
Where Category Id = X
That has not been answered before (so the QuestionId should not be listed in Answers with the same SessionId)
My own trials and some Googling did not help. Hope somebody here can help me out.
Thanks in advance!
Try this:
select
*
from
question
where
categoryid = x and
not exists (
select
'1'
from
answers
where
sessionid = y and
answers.questionid = question.questionid
)
It will list all questions that don't have an answer from the user. You can put a "limit" on the results to get back only 1 row if that's what you need.

Get the sum of a column value for all rows with same value in different column

I need to know how to handle a pretty complex situation.
I have a system that allows users to vote up or down on comments that others make. I want to create a report of those with the most up votes based on all of their comments. The upvotes were not tracked in the users table, only in the comments table so it needs to go through the comments table and get the value in the vote column and output the sum of all of the vote column values for each userid. It then needs to order these and output the top 10.
Thanks in advance for help
If you post your users and comments table structure I could make a query. But it would be something like this:
SELECT SUM(votes) total, user_id FROM comments GROUP BY user_id ORDER BY total LIMIT 10

Categories