need mySQL/php advice how to build specific mysql db - php

The following description is a simple example with questions and answers. But the logic of my site is similar.
Lets say tables are:
USERS table: USER_ID, etc
QUESTIONS table: QUESTION_ID, TEXT, CATEGORY, CORRECT_RESPONSE, AVAILABLE
RESPONSES table: QUESTION_ID, USER_ID, RESPONSE_VALUE
PROFILE table: USER_ID, CATEGORY_Questions, YEAR, NUMBER_OF_ANSWERED, Number_OF_CORRECT, POINTS
The questions will be available to be answered by users for few hours. Every question has the same 3 choices for answers YES/NO/DEPENDS.
Noone knows the answer(even admin) before some time passes. In the meantime USERS can choose their response which is stored to RESPONSES table.
After some time I will manually add to mysql table the CORRECT_RESPONSE and mark the question as AVAILABLE="NO". So users can't answer this question anymore.
Then I want to automatically check users' RESPONSE_VALUE for each question(ID) that isn't AVAILABLE to see if they chose correct or not. And then add them points for every correct answer.
I made another table called Profile that stores Userid , category of questions, how many answered, how many were correct, timeperiod and how many points got for that category of questions. I do this in order to get statistics faster for every user on every category etc.
So after I "mark" a question as finished(AVAILABLE="NO") and I have submitted the CORRECT_RESPONSE what is the best way to check for users' correct RESPONSE_VALUEs and then update the profile table?

To add on to what Galen said you would then want to add a variable
$num_correct++; while looping through the query
update PROFILE SET Number_OF_CORRECT = $num_correct WHERE USER_ID=responses.user_id

Loop through the users and run these queries:
Get total questions answered
select responses.question_id from responses where responses.user_id=USER_ID
Get amount of correct answers
select responses.question_id from responses where responses.user_id=USER_ID and responses.question_id = questions.question_id and responses.response_value=questions.correct_response
EDIT
I'm assuming you have an admin interface of some sort. I would just add a button "Correct Answers" that runs the code to correct all the answers. If you dont have an admin interface the easiest way would be to put a single script in a protected folder that would run the corrections.

Related

How to build a relationship between a 'user' and 'his comment'?

I'm currently coding a blog to get experience with php(I've made an MVC-Framework), so I am still new to this.
I have two tables important for this question:
user(id, username, password, registrated)
comments(id, content, post_id, comment_author, date, editedAt, editedBy)
In the comments-table comment_author is yet not linked to the id of the user, because I was unsure how to actually do this.
A user can write as many comments as he likes, but a comment can only have one author.
comment_author has the username in it at the moment, but I know I need the id(if the user gets deleted and someone else would registrate with this username, the comment would be his).
How should I now structure the tables?
1.) comments_author_id in comments-table, id in user as foreign key:
In this case I would have the id of the Comment author in the comments-table, but the user would not know about the comments he has written. If I want to show the recent comments of the user on it's profile, could I get them with an inner-join query then?
2.) make a new table user_comments(id, user_id, comment_id)
In this case user and comments wouldn't know about it's author/comments.
Also I have 'editedBy' in which the username of the last editing user is. Can I somehow link it with the username in the users-table or should I also link it with the id?
I am really lost with this question since I don't know much about databases; So I appreciate every help and advice I can get.
Please also let me know if I need to give any further information or change something for a better understanding.
It make sense that you go with the first option.
As you said comment has only one author so just use a foreignkey to user table.
In the second option you're creating a hole new table for a data that doesn't need a new table so you're storing data's that you don't need. also you have to write in two different tables which is two different operations for adding a comment and it gives you no extra feature that you can use.
Best way is create a new table user_comments(id, user_id, comment_id). And if you want to track every changed/edit the comment or post it's will better if you create another table for that and if user can only edit then i think it's better to editedBy fields not generate. The structure totally upto you what kind of tracking you want to be .

(MySQL) How do I add an item id to all users?

I'm new on programming, sorry if I can't explain my doubt very well...
My friend have a online game and I help him to manage it. The php has a table for the users (meh_users), and a table for the items that the users have (meh_users_items). Each item has a id, (column itemid) and also the users (column userid), and in the table of the items that the users have, there is a id for the combination of the user and the item. (just id, and sorry, I don't understand this so much).
There is an image, if this make more easy to understand me:
phpMyAdmin printscreen
I want to add an item (let's think that it's a sword and this item have the id 3454) to all the users, but I don't want to delete the other items that the users have. Also I don't want to insert the data on the table one by one, because it's more than a hundred users.
Hope I have said enough details!
Remember: I'm new and I already search soooo much in the internet before asking here.
I suppose you want to give that sword to all users, including ones that already have it. You don't need PHP for this, SQL is enought.
INSERT INTO meh_users_items (userid, itemid, equipped)
SELECT userid, 3454, 0
FROM meh_users
This is just an example. You need to add all field to SQL.
Three steps:
If it isn't there already, add the new item (e.g., the Sword of Power) to your meh_items table.
Get a list of all your users from the meh_user table.
Write a foreach statement creating an entry in the meh_users_items table for each user.
It also occurs to me that your database might have some setup issues. E.g., meh_users_items has a column indicating item type (e.g., weapon). That could end up being problematic. Better to keep that information with the items in the meh_items table.

Laravel 5 Quiz Project

This is probably more of a conceptual question but I am trying to find the best way to make a view that displays one quiz question at a time and checks the answer.
Currently my MySQL table has the following columns: id, category_id, quiz_question, answer_one, answer_two, answer_three, correct_answer.
My controller uses the following to get the quiz questions:
$quizzes = Quiz::where('category_id',$category_id)->simplePaginate(1);
In my view, I go through the process of doing the asnwer ordering manually
{{$quiz->quiz_question }}
<a onclick="this.innerHTML='Wrong'">{{ $quiz->answer_one }}</a>
<a onclick="this.innerHTML='Right'">{{ $quiz->correct_answer }}</a>
<a onclick="this.innerHTML='Wrong'">{{ $quiz->answer_two }}</a>...etc.
and was probably just going to use JavaScript/JQuery to check for the right answer.
Conceptually this feels like a pretty bad way to do this and I am still not sure how to randomize the order of the answers if they are from the same DB table like this. Overall is there a better way to go about this (Display 1 quiz question, and display answers in random order, check without a DB query)? Thanks in advance.
Do not keep the answers on the client side in any form.
Instead, just get the questions and paginate it on the client side. If you absolutely need to validate answers one by one, then fire up AJAX requests.
Or you can just let the person attempt the whole quiz and post the questions with users' answers and validate them on the server side. Then you return the result/score/answers.
Regarding your query about checking without DB query, you shouldn't do it. If you absolutely need it then save answers in an object, rather than in the DOM. Again, it can't be any more "not recommended".
Edit:
You should first divide it into these tables. You can't randomize the order of display of the options without a hack from the first table.
And don't name the columns like answer_one, answer_two, answer_three, correct_answer. In this manner the only way to recognize the answer in by the column name correct_answer. Column name shouldn't give a hint about the correct answer. It should be stored somewhere else.
questions
id
question - the question body
categories
id
category - name of the category
question_categories (one question may belong to multiple categories)
id
question_id
category_id
options
id
question_id
option - option text
answers
id
question_id
option_id
Now what you do is you randomize the order of options
$questions = Question::where('category_id', $category_id)->
with(['options' => function ($query) {
$query->orderBy(DB::raw('RAND()'));
}])
->get();
To randomly order the options we used ORDER BY RAND().
Now you just send the option_id and can check if it is the correct answer on the server side.
Edit - 27th March
You can use the following models.
Question
QuestionCategory
Category
Option
Answer
You can use the following relationships.
Question belongsTo QuestionCategory
QuestionCategory belongsTo Category
Question hasMany Option
Question hasOne Answer
But creating a QuestionCategory model doesn't really scale well. So if you don't want to do that you can use Polymorphic relationships. You can use category table to store basically all types of categories, not just question categories. In that case, you need to modify the categories tables to add the type of taxonomy. If you don't understand any of this polymorphic relationship thing, please find it in the official Laravel documentation https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations

User Reviews: Implementation of Comments - What technologies to use?

I have set up a company intranet website built with PHP/MySQL and allow users to post reviews. After joining up on this website I have grown to like the "comment" function and would like to add that same functionality to allow users to "comment" directly to other users reviews.
Currently all reviews are stored in a single table in the DB.
1) Should I create another table to then store all the comments since there can be many comments per review?
2) Once I figure out where to store these values can the rest of this functionality be built out in PHP or will other programming need to also be introduced?
Sounds like a good plan. You can have a table like Comments(commentID, reviewID, comment_body, ...). You can then insert a new entry when adding a new comment, or select all comments with a given reviewID to display comments for a given review.
Yes, you will almost certainly implement this in PHP (the same language you use in the rest of your application). You'll also have to edit some HTML, and maybe javascript as well.
Yes and yes.
Comments should be a seperate table, because they're comments, not reviews. They are two different things, therefore, they should not go in the same table.
Once you've created that table with the appropriate references to other tables, it's just a matter of constructing a query which pulls out all of the information you need (e.g. SELECT user.user_name, comment.comment_text, comment.post_time FROM comment, user WHERE comment.user_id=user.user_id AND comment.review_id = 123, where 123 is the ID of the review you're getting comments for).
The exact layout for your comment table will depend on your specific needs, but as a minimum, you'll want to know which review it's a comment for, who posted it, when they posted it, and what they actually posted.
To insert comments, create a form on the page that displays the individual review, and when filled in, create an INSERT query which inserts into your comment table.

How to save user data obtained from a single form into various Tables within one MySQL Database?

What's the correct way to implement the below concept via MySQL and PHP? Say a website has two groups/communities, both of which allow its users to ask Questions, answer Questions, and start Discussions, pertaining to the respective community. A user will choose which community he/she wishes to enter via below code(main.php):
<h2>Group A</h2>
<h2>Group B</h2>
After the user clicks on say Group B, he/she will of course be directed to groupB.php, where he/she will be able to view the data related to the Group B community (Questions, Discussions posted by the users etc. ). Of course, one will also be able to do the same for the Group A community.
Now my question is: How can I do this efficiently in MySQL? As of now, say if the user wants to create a discussion/question in Group B, which is located on groupB.php, he is directed to a form(discussion.php). Tiny code below:
<form action='savedisc.php' method='post'>
The above form will be available in both communities, i.e. groupA.php and groupB.php. Therefore, of course, savedisc.php will save the content of the form to a database. Meaning, discussion.php and savedisc.php will help in saving both groupA.php and groupB.php's contents. Some of the savedisc.php is below:
$sql="INSERT INTO GroupA (Message, Title, Type)
VALUES
('$message','$title','$represents')";
Where GroupA is the Table's name. The Table name will change of course respective to which group the user may be interacting in. So for group B, the above code will look the same but GroupA will be replaced by GroupB. How can I do this in savedisc.php? Will I have to use numerous IF statements for various groups?
Given the above steps, is this even possible? Can discussion.php be located on all community pages? Because then, savedisc.php's job would be to save contents of groups A, B, C, D in their respective tables within the same database, but the respective group's table. Am I even on the right track?
Thank you.
You will want to spend some time research relational databases; however, here is a quick high-level answer.
You want to have a few tables: users, groups, groupmems, questions, answers.
users (id, username, group*) -- just an idea and not needed necessarily
groups (id, name)
groupmems (id, groupid, userid)
questions (id, Title, Message, Userid, groupid, date)
answers (id, questionid, title, message, userid, date)
What you do is make it where these tables relate to each other through the existence of auto-incrementing primary keys. If you choose to have users login then you will need the users and groups tables for greater flexibility. If you choose not to, then when a user wants to post a new question or answer you will use the PHP POST method to send the value of the group id to the form, which will be bound to a hidden text field.
that's very easy
there should be only one table with group field.
so, you have to have only one set - one form, one script, one table. just pass group identifier using query string and hidden input field

Categories