General directions wanted please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

General directions wanted please!

Post by Noobie »

Hi all

I'm almost brand new to PHP, just getting the hang of includes etc and I'm totally new to MSQL. I tend to learn best by trying to achieve an end goal and figuring it out along the way. So I was hoping you could point me in the general direction that I should be following to achieve this:

I want to build a quiz which has photos and the player has to identify the movie - typing the answer into a text box and NOT multi-choice, before moving on to the next question. I'd like scores to be kept and I'd like the questions to be a random selection of say 10 from a larger collection of questions.

Would I be right in saying that I'd need a db for the questions and another for the answers (rather than just the one) and a php file with the code to generate the questions?

Obviously also some sort of call from the HTML page too.

I'd really apreciate it if you could let me know if this sounds about right so I don't go charging off in the wrong direction!

Thank you in advance
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

alright, how about this. I'll give you a bit of pseudocode and explain what needs to happen. :)

First, you'll want to store the data in a MySQL table. If you use phpMyAdmin, you shouldn't have any problem. I'd store the data as so:
"id" : int (auto-increment) (primary key)
"question" : text or varchar
"answer" : text or varchar

To get 10 random questions, I'd pull the data into an array and use a loop using array_rand() to get the questions.
Something like this:

Code: Select all

// SELECT id FROM table
loop (fetch row) {
  // add row to $rows array
}
loop (10) {
  $tmp = array_rand($rows);
  loop (count($questions)) {
    // if $tmp is in $questions somewhere,
    // keep choosing a random index until it isn't
  }
}
// by now, you should have your $questions array filled with 10 IDs.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Skara

Thank you - that's absolutely brilliant! It'll take me a while to try it out - but thanks, you've probably saved me a week of fiddling around!
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Slight change of plan - I'll get to the photo quiz as mentioned above but first I'd like to convert a Javascript short answer quiz that I've already written into Mysql and PHP.

I've just written my very first MYSQL database this morning so you'll understand if my syntax is WAY out and I apologise for it now - I'll figure out the correct syntax as I go along, I just wanted to ask 2 general questions about what I'm doing.

I'll be creating a db with the questions and relevant answers in (obviously) but I was wondering - in my JS quiz I worked it so that there was an either/or answer to allow for different interpretations of the correct answer e.g. (it's a film quiz) Answer= "Hunt for Red October" or "The Hunt for Red October". This made life easier for those doing the quiz.

In my DB version, should I do an extra column for any alternate anwers and have the script check either or for the correct answer?

Also, if there really is only one interpretation of the answer e.g. "Bugs" then should I put the single correct answer in all the columns of that row or have Null instead in the alternate columns?

Final question I promise. In the JS script I transformed everything to lowercase and had the answers in lowercase to avoid problems with people using capitals here there and everywhere. In the DB version should I enter the answers just in lowercase and use the PHP script to transform the users' answers or is there a better way?

I'm not trying to get someone else to code this for me - just a general idea of what's right and wrong.

Thank you again!
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

First:

I'd put one column for all the answers. Divide them as such:

Code: Select all

+---------+--------------------
|Questions|Answers
+---------+--------------------
|meh      |answer1|answer 2|third answer
|blah     |first answer|second
|etc      |only one answer
+---------+--------------------
Then do

Code: Select all

$answers = explode('|',$answers);
then loop through them all. ;)

Second:

Yeah, I'd just enter them in lowercase. Either that or strtolower() both the db and the form entered and then check.

Hope that helps. ^_^
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thanks again Skara - that looks excellent!

I'm off to start entering all the data...
Post Reply