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
Related
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
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.
I have a PHP system that allows users to vote photos on a scale of 1 - 5, what I want to do is highlight where two people give each other the same vote/score. I can't figure the SQL out at the moment for my PHP function.
The database looks like this
id, user_uid, voted_uid, score
As someone votes the id is auto incremental, the user_id is inserted from the session uid and the voted_uid comes from the image the user is viewing, then the score is the ranking from 1-5.
In theory we are therefore looking for two similar rows like this:
uid user_uid voted_uid score
7 3 5 3
38 5 3 3
At this point I want my php function to take the current users session and then match their votes and scores with other users.
In the example above I'd have the session id of 3 and I want it to return these two records as matches.
If I understand you correctly, what you want is to find pairs of rows where user_uid of the first row equals voted_uid in the second row and vice versa. But only, if score is the same in both rows.
In that case, this should do the trick:
SELECT a.*
FROM table AS a
JOIN table AS b
ON a.user_uid = b.voted_uid
AND a.voted_uid = b.user_uid
AND a.score = b.score;
If you only want rows that "mention" a specific uid, you of course have to add a WHERE user_uid = 3 OR voted_uid = 3.
I have voting system in my site and i want to check that user has upvoted,downvoted or not.
To reduce the no. of queries i selected complete table containing id equals to user id from voting table
table schema for vote table is
table schema for answer table is
$query=SELECT answer_id,user_id,vote FROM vote WHERE user_id='{$_SESSION["id"]}'
$result1=mysql_query($query);
$array=mysql_fetch_array($result1);
now when i fetch the answers of question i want to check that if this user has voted or not
if answer_id has found in this array than than i want to find the value of vote ?
code is
$query1="SELECT * FROM answers WHERE question_id='{$question_id}'";//$question_id is id of question
$result=mysql_query($query1);
if($result){
While ($row=mysql_fetch_arrar($result)){
if(in_array($row["id"],$array){
echo $array["vote"];
}
}
}
I am confused how to fetch two array together...?This query only works for first value because i am not using while loop in $array,Can anybody help me..?
This can be done in one query to determine if a user has voted for an answer. There is no need for a preliminary query to get user votes, or an array comparison inside the fetch loop.
Use a LEFT JOIN against a subquery on votes and supply the userid in its WHERE clause. If the value of uservotes.vote is NULL, the user has not voted for this answer. Otherwise, uservotes.vote will contain the user's vote.
SELECT
answers.*,
uservotes.vote
FROM
answers
LEFT JOIN (
SELECT answer_id, vote FROM votes WHERE user_id = '{$_SESSION["id"]}'
) uservotes ON answers.id = uservotes.answer_id
WHERE answers.question_id = {$question_id}
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