I asked random question to student & stored his answers using his session id into mysql & than extracted the same way.
I used Order By RAND() function in my query while asking the question
$query = "SELECT * FROM question ORDER BY RAND() LIMIT 0,1";
But Now I Want to Store answers in an arranged sequence for such purpose i can use Q_ID but i don't want to show q_id to user. So how can i store q_id into another table without showing it to user.
Secondly i want to show correct answers to my students too.
but i really don't know how to tackle these thing :(
query for storing answers
$order= "INSERT INTO radio (Option1,Option2,Option3,user) VALUES ('".$Option['Option1']."','".$Option['Option2']."','".$Option['Option3']."','".session_id()."')";
Query For Extracting Data From mysql
$qry=mysql_query("SELECT * FROM radio where user='".session_id()."'", $con);
Without your actual database schema/more information it is difficult to give you the best advice possible.
Add a field to the table radio so that it looks something like this:
RADIO (*user*, *questionId*, Option1, Option2, Option3)
(due to stack overflow formatting stars represent the primary key(s))
where questionID is a foreign key that references QUESTION(id). If you don't have an id field in your question table add that too.
Then when you display the question to the user, save the question ID as a hidden input field inside the tag like so:
<input type='hidden' name='questionId' value='".$data['id']."' />
When the form is submitted you will have the question ID available in your $_POST array at $_POST['questionId']
Then you can modify your INSERT query to insert the question ID as well and simply not display it when you display the data later.
--
As for displaying the correct answers to the students, you will need to store the correct answer in your table somehow. One way would be to add a field to your question table indicating which of the options is the correct answer, I.E. a TINYINT(1) which will contain a 1, 2, or 3 depending on which answer is correct. You can then use that to generate a page with the correct answer to the question.
Related
i try to development exam management system. I do not want same
question or same id not showing second time or other any time exam a
user. How can this condition be given?
my function is:
public function qustionShow($question, $limit=4){
$show = $this->conn->query("select * from question where cat_id ='$question' ORDER BY RAND() LIMIT $limit");
while ($row=$show->fetch_array(MYSQLI_ASSOC)){
$this->qus[]=$row;
}
return $this->qus;
}
You would need to keep track of which questions have already been asked. you can save the question id's to the Session if it's you don't want the questions to be selected again only for the session.
you could initialize an array to the session
$_SESSION['questions_asked'] = array();
and then once a question is asked you would
array_push([THE QUESTION ID], $_SESSION['questions_asked']);
of course you need to replace [THE QUESTION ID] with the sql id for whatever question was asked
keep in mind you would need to modify your query to account for anything saved in the session.
If you don't want them to ever be shown again you would need to record which questions a user has seen and would need to store that persistently in the database probably.
possibly you can have a table to store those in for each user
user_question_asked
with at least these 2 columns
[user_id][question_id]
so each time a question is asked you insert the current user id and question id
then your query could be
SELECT * FROM question where cat_id ='$question' AND [QUESTION_ID] NOT IN
(SELECT question_id from user_question_asked where user_id [CURRENT USER's ID]
) ORDER BY RAND() LIMIT $limit;`
Hope that helps, I'm not sure what your table / column structure is, but those are a couple suggestions I have for addressing this problem.
Please I need help with this problem I'm facing. I'm building an examination system and I'm using the Rand() function to select questions from the "question" table. The user's answers are also saved in the "user_answer" table.
Now my problem is a question sometimes gets selected twice or thrice so I need a query that will check that if a question has already been answered in the "user_answer" table, it should reselect another question from the "question" table.
You cannot exclude with Rand() directly.
If you query from a database, you could add something like
select *
from question q
left join user_answer ua on ua.question_id = q.id
where ua.id is null
group by q.id
This will try to connect to an answer (ANY answer, you probably want to add some user selection into that), and only give back the questions where it FAILS (ua.id is null) to do so.
If you cannot do it by query and have it it all in some PHP array, what you could do is keep track of the available question ID's in an array. Each time you pick a question random, you remove that item (value!) from the array, and reindex the array (keeping the values, which are the question ID's, and ordering the keys from 0 to the number of questions - 1).
That way you can do a rand(0, count($questionIds)) again to pick the next one.
Another way would be to use a loop, and continue as long as the picked question is already in the used questions array.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
i have database tables for Posts and comments.
i want to allow users to put a like or dislike for each comments and posts.
so..i have few ideas of doing this. please tell me either i am correct or not.
create two additional columns in comments table.
likes | liked_uids
if a person clicks on like button then add +1 for the current value in likes field, else add -1 for current value.
and add user's id to liked_uids field as a sting separated by "-" dashes.
then next time i can get that string to an array and check that,
either current user id has recorded or not. if user id is, then can decide that user have participated for liking.
but i have little problem on this structure, that what will if more than one user going to like at once for a post ? then i may lose some data from liked_uids string (one last uid).
so please tell me what is the correct way of doing this?
You can create like this->
id type ('comment/like') uid comment post_id
1 comment 1 good post 100
2 like 2 null 101
3 like 1 null 102
4 comment 3 bad post 104
It is not recommended to store like count.If you want to count the likes for a particular post:
select count(*) from tableName where post_id = 100
Storing user id separted by any delimiter will land you on problems, Hence not recommended. It will be tidies job to update or retrieve if your store user id using delimiter.
If you want to see if particular user is liked a particular post or not, use below query:
select count(*) from tableName where post_id = 100 AND uid =1
One way is to use a separate Likes table with columns Likes, DisLikes, Likes_UID, DisLikes_UID and mapping table for comments and likes ex: Comments_Likes and posts and likes Posts_Likes
I'm typing way too slow on my mobile ^^ Everything already answered.
I never did anything that is similar, but I wouldn't add the two columns in the comment table. I would rather create a new table like "votes" and it would have following columns.
comment_ref | like | user_ref
Every time someone likes a comment you insert a new line there. You could also make the combination comment_ref and user_ref as a key, so you can't insert it twice.
In the end you would just make a query as such to get the votes of a single comment.
SELECT COUNT(*) FROM votes WHERE comment_ref = 123
I have a game. In the game, people make many choices out of 2 options.
The choice can be either right or wrong and I am storing the result of their run through the game (which can be a very large length) as a string with 1 for a right answer and 0 for wrong answers.
So for example, player 128937 will have stored in his run column the string 00010101010010001010111 as a varchar(5000).
Is there a better way I can store this information in MYSQL? (I am using PHP too if that can help)
I would create a new table (say it's called 'answers') with three columns:
question_id,user_id and answer (which will hold values of 0/1 )
every time the player answers a question you INSERT a new entry to this table.
This way it'll be easier to maintain the sum of right/wrong answers
Why not use a tinyint(1) for each option rather than using strings?
I would make multiple tables
choices
id
scenario (or other title)
options
id
choice_id
title (example: "go left" or "turn around and go home"
correct (0 or 1)
user_choices
user_id
option_id
choice_id (optional since choice_id is already in options table)
i was wondering if any one can help me with my php-mysql design
my current app. (is a more or less survey app) it let users store questions about targeting specific features in other products also saved in other table in database !
for example , a user can post a car: and then ask users about there opion in safty elements of his car.
car db : Id,brand,safety
brand = Fast
saftety = ABS=ABS (Anti lock braking System),DriverAirBag=Air bags
questions db: ID,Question,Answer,Target,type
eg of data:
Question:safety options you like
Answer:ABS=ABS (Anti lock braking System),DriverAirBag=Air bags"
target:saftey
type=checkbox
problem is that to display questions stored, i have to .
1) loop through all questions, echo Question and echo target in hidden input,
2) explode Answer field twice(1st w/ "," to get each answer and other with "=" to differ > between whats inside the database[0] and a user friendly text[1]
3) check type to chose display type (3 options checkbox,select,text)
4) set this display type of [0] and show [1] for user !!! (stupid i know:()
eg:
< checkbox
value=$expolde[0]>$explode[1]
All these steps make it very hard to maintain, not flexable by any mean cause display is embeded in code :(,
any ideas :) ?
I would separate the tables into a one-to-many type design like:
CarTable
ID
Brand
Model
CarInfo
CarID # Foreign key to Car Table
Category # Optional: Safety, Performance, Looks, etc...
Value # Specific Info Value: ABS, Air Bags, etc...
In this design you can have 0 to many CarInfo records for each Car making it easier to add/remove info records for a car without having to parse a potentially complex field like in your original design.
Your question table design could be similar depending on what your ultimate goal is:
Question
ID
Description
QuestionInfo
QuestionID
Category
Value
Some other things you should be considering and questions you should be asking yourself:
How are you handling custom user inputs? If user1 enters "Air Bags" and user2 requests "Driver Side AirBag" how are you going to match the two?
Make sure you understand the problem before you attempt to solve it. It was not clear to me from your question what you are trying to do (which could be just me or limited size of the question here).
Be careful when outputting raw database values (like the type field in your question table). This is fine as long as the database values cannot be input by the user or are properly sanitized. Search for "SQL Injection" if you are not familiar with it.
If you want a survey PHP application, I suppose, to be clear, that you need something where:
one user can add a subject (a car in your example).
there can be an arbitrary number of questions attached to a subject by that user
each question can accept several types of answers: yes/no (checkbox input), a number (text input, or say 10 radiobuttons with values 1 to 10 attached etc), single or multiple choice (select with or without multi attribute), arbitrary data (textarea). Moreover, some questions may accept comments / "other, please explain" field.
any other user can answer all the questions, and all of them are stored.
A more sophisticated version will require different sets of questions based on what was replied previously, but it's out of the scope of this question, I hope.
For that I think you need several tables:
Subjects
id int pri_key and anything that can come to mind: brand, type etc.
Questions
id int pri_key, text varchar, subject int f_key, type int/enum/?
QuestionOptions
id int pri_key, question int f_key, option varchar
Users
id int pri_key + whatever your authentication structure is
UserReplies
user int f_key, question int f_key, answer varchar, comments varchar
The user-creator sets up a subject and attaches several questions to it. Each question knows which type it is - the field 'type' may be an integer, or an enum value, I myself prefer storing such data as integer and defining constants in php, using something like QTYPE_MULTISELECT or QTYPE_BOOLEAN for readability.
For single/multiselect questions a user-creator also populates the QuestionOptions table, where the options for select-tag are stored.
To display all the questions there'll be something like
SELECT Questions.id, Questions.text, Questions.type, GROUP_CONCAT(CONCAT(questionOptions.id, questionOptions.option)) AS options
FROM Questions LEFT JOIN QuestionsOptions ON (Questions.type = $select AND Questions.id = QuestionsOptions.question)
WHERE Questions.subject = $subject
GROUP BY Questions.id
The GROUP_CONCAT and CONCAT here should be modified to return something like 5:Option_One;6:Option_Two etc, so that exploding the data won't be much hassle.
I realize this is not the cleanest approach in terms of performance and optimization, but It should do for a non-large-scale project.
There is also a drawback in in the above design in that the answers to the "multiple answer question" are stored imploded in the "answer" field of the UserReplies table. Better add another table, where every record holds an option value the user selected for this or that question. That way there will be no unnecessary denormalization in the database and queries for statistics will be much easier (i.e. querying which options were most popular in a single question)