I am not sure where to begin with this code or how to go about it. I was wondering if anyone could give me some ideas as to where to start or ideas on how to accomplish this (If you want to help with sample codes I'd love that too, but I'm really looking for ideas on how to accomplish this. From there I can search for how.)

First I already have a system up that requires users to log in in order to access the play page.

When they get to the play page, there will be one question. When they correctly answer this question, it will move them on to the second question. There will be a total of about 50 questions, but when we start, we may only have about 10 - 15 questions ready to go. (I don't think it will be an issue to keep moving forward and adding more questions in once I get everything established.)


Point 1: Ideally what I would like to be able to do is something like this:

Option A: The first person to answer the question correctly gets 100 points. The next person 95. The third person 90. I would like to do this down to 55 points. At this point, anyone who gets it correct will receive 50 points.

Option B: If this is not achievable, perhaps some kind of internal deduction. What I mean by this is the first time Userx logs in and tries to answer the question, he or she would get 100 points if correct on the first try. If it takes Userx 2 tries, they would be awarded 95 points and so on.

I would much rather do the first scoring system than the second one.


Point 2: I want to be able to display the user's total score. Is it better to calculate it in the database (MySQL) or is it possible to do this is through PHP with hidden output fields or something?

Please let me know what the best way to go about this is or if there are any tutorials that I might be able to use or coding I could use I would really appreciate it.

This can be tricky but its doable.

The best way would be to use a mysql database and store the users answers.

Each answer will need to be stored with a timestamp, so you can determine who answered first so you can distribute points correctly.

Kkeith29,

I planned on storing the information in the already existing database.

When you say that I have to use a timestamp (which I am assuming I can find the code to collect with an invisible field when the answer is submitted) are you saying I would have to manually award the points? Or is it going to be programming on the database?

I'm thinking I'm going to have to have to do it this way with a table for each question where it looks up the member name (in plain English here):

If the answer is "x" make Field1 in the database for this member be yes and then Field2 would be increments and indexed. This would start at 1 and go up until out of players. Then make Field3 be a value so that "if field2 = 1 then this is 100, if field2 = 2 this is 95, etc...) Then in the usersysterm table, have a total value field that adds Field3 from Q1 to Field3 from Q2 and so on. Then I can pull this when the user logs in to be displayed on their page.

Does this sound doable and the best method of approaching it?

Sorry, I was really vague in my last post.

This is how I would do it:

There would be a table for questions with the columns id, question, and answer. That way adding and retrieving questions would be easy.

There would be a table for users (you probably already have this) with the columns id, name, and points.

There would also be a third table that would hold the answers. This would consist of the columns id, question_id, user_id, and time.

The php would be set up to display a question when they log in (or however you are wanting this to work). Once they submit, the system would check if the answer is correct. If it is, then it would run a query on the database to see if anyone has answered the question before (ex. SELECT COUNT(*) FROM `answers` WHERE `question_id` = [QUESTION_ID] WHERE time < [CURRENT_TIME]).

Using that we can calculate the score. For example you can start out with 100 points and the subtract that by the number of people that have already answered times 5 points (or however you are wanting the score to work). If 3 people answered before than they would get a score of 85. You can then update the users table by adding their new score to their old one.

Is that what you are looking for?

it's not complex.it's like js code or asp...

@Kkeith. Thanks. I am going to check that out and work on it. I will let you know how I make out in the next few days.

@Mushget. Sorry, I didn't mean to mislead you. I thought putting the ??? after my title would let you know I was unsure of how complex it really was. I guess I should have used the word overwhelming instead, since I just started working with PHP and MySQL two days ago. Aside from that the only programing experience I have is ActionScript in Flash, and that is minimal and all self-taught.

Member Avatar for diafol

As a trivial solution, I'd do this:

questions:

quest_id, quests, ans, correct_sofar, active_on

(active_on = time at which the question will become active for users).

The impt field here is 'correct_so_far'. For every correct answer, the correct_sofar increments by 1 or -1, OR by -5 (default value = 100).

Judge whether the answer is correct:if correct
Just read the orig value, increment the correct_so_far and add it to the user's total score. OR the correct_so_far value can be processed to give a value and placed in a delimited list as well if you want a breakdown question by question for an user?

Dunno, loads of ways to do it, depends on the level of complexity and the outputs that you need.

This to me sounds like something best handled by the database using a trigger.

Questions (Table):

QuestionId   int
  Question     varchar
  Answer       varchar
  DateActive   datetime
  Value        tinyint    default 100

QuestionUserAnswers (Table):

QuestionId   int
  UserId       int
  Answer       varchar
  Score        tinyint  (min 50, max 100)

With your trigger being something like this:

CREATE TRIGGER Trig_QuestionScore AFTER INSERT ON Questions
  FOR EACH ROW BEGIN
	IF Questions.`Value` > 50 THEN 
		UPDATE Questions SET Questions.`Value` = Questions.`Value` - 5;
	END IF
  END;

**This is NOT tested so use it just for the general concept.
But what you would do is when the user gets the question right, you would insert a record into the QuestionUserAnswers table. AFTER that insert occurs the trigger would fire automatically and look at the Questions table and subtract 5 from the Questions value unless the value was already at 50.

This only works on mysql > 5.0.2
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html
http://dev.mysql.com/doc/refman/5.0/en/stored-program-restrictions.html

commented: show off! +7
Member Avatar for diafol

:* Oh MS, love the trigger - nice. You just had to go one step further didn't you? Ho ho.

MSchroeder,

Thank you so much. I really appreciate it. I'm going to be working on this for the next few days, so hopefully by Monday I can close this thread out. If I get stuck or need more help I will come back.

Thanks again. I'm so glad I found this site you all rock!

@ardav
It just screamed trigger to me...its Friday, I'm pulling out all the stops haha.

@CEVGames
Like I said that is not tested but it looks mostly correct as I typed it out. Hope it works out for you. If not plenty of other ways to skin a cat.

Point 1:

If you record who has supplied a correct answer to the db, then when the person submits the answer and it is the correct answer simply do a count of the number of correct answers submitted up to that time and then subtract from 100.

IE: If I answer the question and I am the 5th person to answer the question then

total points - (5 points x 5th answer)
=75

You could then keep a score card showing which questions they answered and their score.

In response to the second part of this point, if the person tries to answer the question a second time, they could still be the 5th person but could receive a
penalty for guessing wrong

total points - (5 points x 5th answer) -(5points x (2 answers -1 guess))
=70

Point 2

When the user first enters the game you can query the db for the users points, as you save the users score each time they submit an answer you can record the points.

In general, each time you have to execute a query, it takes time, might only be milliseconds, but time either way and depending on the size of the program, this can add up. Try to keep things in memory.

Just wanted to let you know I got it working! Thanks everyone for your ideas and input!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.