Jump to content

Recommended Posts

I've been trying to learn php and mysql so I can make a quiz for my web site

So far its going quit well

My quiz has 10 different categories each category  issue's a random questions from db

But I also need sub categories i.e.  you can chose quiz No10 called example easy or you may choose quiz No9 called say harder  now a sub category No9a would include random question from quiz 10 and 9 this would go on all the way to quiz no1 example ultra hard with sub category No1a including random question from all 10 quiz's

 

Where I am

I made 10 different tables  columns questions and answers and I used

SELECT * from ten  ORDER BY RAND() LIMIT 1 ;

which worked fine for individual category's

I assumed I would then just use

SELECT * from ten seven  ORDER BY RAND() LIMIT 1 ;

or

SELECT * from ten , seven  ORDER BY RAND() LIMIT 1 ;

But this does not work how I want

I then thought maybe I should have just one table and give each quiz and id

 

CREATE TABLE test (
  
  cat char(20) NOT NULL,
  question char(30) NOT NULL,
  answer char(30) NOT NULL,
 
  );
  
  
INSERT INTO test ( cat, question, answer) VALUES
       ('ten','foo1', ' bar1'),
('ten','foo2', ' bar2'),
('ten','foo3 ', ' bar3'),
('seven','foo1',' bar1'),
('seven','foo2',' bar2'),
('seven','foo3',' bar3'),
('one','foo1','bar1'),
('one','foo2','bar2'),
('one','foo3','bar3');

 

Hoping I could use a query like

SELECT * from test  WHERE cat = ten ORDER BY RAND() LIMIT 1 ;

 

I guess I need to use different create table command and query

A I said noobie who is so happy to have what I have working

Thanks for any help

I no I could just made loads more tables adding extra data from each cat orgy but that would not be the correct way to go about it

 

Link to comment
https://forums.phpfreaks.com/topic/48435-mysql-help-please/
Share on other sites

You have questions.. and each question is unique so start with a table of questions something like:

 

CREATE TABLE question (
    id int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'unique identifier',
    content VARCHAR(255) NOT NULL DEFAULT 'This question is undefined' COMMENT 'unique question',
    PRIMARY KEY (id),
    UNIQUE KEY (content)
) TYPE=MyISAM;

INSERT IGNORE INTO question (content) 
    VALUES ('What is your favourite color?')
    , ('What is your favourite number?')
    , ('What is your favourite movie?')
    , ('What is todays day')
    , ('What is the current temperature');

 

now you also want categories that should also be unique, so create a categories table:

 

CREATE TABLE category (
    id int(10) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'category unique identifier',
    content VARCHAR(255) NOT NULL DEFAULT 'This category is undefined' COMMENT 'unique category',
    PRIMARY KEY (id),
    UNIQUE KEY (content)
) TYPE=MyISAM;

INSERT IGNORE INTO category (content)
    VALUES ('Favourite Things')
    , ('About Today');

 

Now you need to create a table to link a question with a category:

 

CREATE TABLE question_in_category (
category_id INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'category.id foreign key reference',
question_id INT(10) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'question.id foreign key reference',
primary key (category_id, question_id)
) TYPE=MyISAM;

INSERT IGNORE INTO question_in_category (category_id, question_id)
   VALUE ((SELECT id FROM category WHERE content = 'Favourite Things'), (SELECT id FROM question WHERE content = 'What is your favourite color?'))
   , ((SELECT id FROM category WHERE content = 'Favourite Things'), (SELECT id FROM question WHERE content = 'What is your favourite number?'))
   , ((SELECT id FROM category WHERE content = 'Favourite Things'), (SELECT id FROM question WHERE content = 'What is your favourite movie?'))
   , ((SELECT id FROM category WHERE content = 'About Today'), (SELECT id FROM question WHERE content = 'What is todays day?'))
   , ((SELECT id FROM category WHERE content = 'About Today'), (SELECT id FROM question WHERE content = 'What is the current temperature?'));

 

Now to select a random question from a particular category:

 

SELECT question.* FROM question 
INNER JOIN question_in_category ON question.id = question_id
INNER JOIN category ON category_id = category.id AND category.content = 'Favourite Things'
ORDER BY RAND() LIMIT 1;

 

This is a complex table structure.

Link to comment
https://forums.phpfreaks.com/topic/48435-mysql-help-please/#findComment-236858
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.