Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:45:02+00:00 2026-05-23T15:45:02+00:00

I have three database tables, question_set , question and answer . question_set contains set_name

  • 0

I have three database tables, question_set, question and answer.
question_set contains set_name and set_id
question contains question and also question_set_id
answer contains answer , question_id

I can not work out a way to show all the data in one view file,
I tried joins but then question data is repeating with it prints because every question has three or four answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:45:03+00:00Added an answer on May 23, 2026 at 3:45 pm

    Just thought I would point out.

    Your database design is horrible.

    It will not scale well, and infact I do not think it would actually. work.

    The way I would do it is

    Three tables

    sets => id, name
    questions => id, set_id, question
    answers => id, set_id, question_id, answer, points

    Note that in my above example I am taking it one step further, rather than each answer being true or false, it can have an assigned point value.
    hence, an answer that you deemed half correct could be given 5 points, where as a correct answer could be given 10 points, it also works as a boolean true, just have a correct answer as 1 point, and an incorrect answer as 0 points

    Anyway.

    With the above table design in mind.

    You could do

    $this->db->select('s.id as set, s.name as name, q.id as qid, q.question as qu, a.id as aid, a.answer as an, a.points as p')
             ->from('sets s')
             ->join('questions q', 'q.set_id = s.id')
             ->join('answers a', 's.set_id = s.id')
             ->where('s.id', 'SET ID');
    
    $questions = $this->db->get();
    $set = array('questions' => array());
    
    foreach($questions as $s){
      $set['id'] = $s->set;
      $set['name'] = $s->name;
      $set['questions'][$s->qid]['id'] = $q->qid;
      $set['questions'][$s->qid]['question'] = $q->qu;
      if(!isset($set['questions'][$s->qid]['answers']))
        $set['questions'][$s->qid]['answers'] = array();
      $set['questions'][$s->qid]['answers'][] = array(
        'id' => $q->aid,
        'answer' => $q->an',
        'points' => $q->p
      );
    }
    

    So then you end up with an array that looks something like

    array(
      'id' => 1,
      'name' => 'My first quiz',
      'questions' = array(
        array(
          'id' => 1,
          'question' => 'What is 1+1+1?',
          'answers' => array(
            array(
              'id' => 1,
              'answer' => 1,
              'points' => 0
            ),
            array(
              'id' => 2,
              'answer' => 2,
              'points' => 0
            ),
            array(
              'id' => 3,
              'answer' => 3,
              'points' => 1
            )
          )
        ),
        array(
          'id' => 2,
          'question' => 'What is 2+2+2?',
          'answers' => array(
            array(
              'id' => 4,
              'answer' => 6,
              'points' => 1
            ),
            array(
              'id' => 5,
              'answer' => 2,
              'points' => 0
            ),
            array(
              'id' => 6,
              'answer' => 3,
              'points' => 0
            )
          )
        )
      )
    );
    

    Then you can do.

    echo '<h2>'.$set['name'].'</h2>';
    foreach($set['questions'] as $q){
      echo '<div class="question">';
      echo '<h3>'.$q['question'].'</h3>';
      echo '<div class="answers">';
      foreach($q['answers'] as $a){
        echo '<label for="a'.$a['id'].'">'.$a['answer'].'<input type="checkbox value="'.$a['id'].'" name="q'.$q['id'].'" /></label><br />';
      }
      echo '</div>';
      echo '</div>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question regarding updating a MySQL database. I have three tables: Match,
I have three database tables: users emails invitations Emails are linked to users by
I have a database with three tables: user_table country_table city_table I want to write
I have a MySql database with three tables I need to combine in a
This question would be a bit childish, I have three tables, users, usergroups and
I have an existing database with tables which each have 4 partitions (there are
We have two tables in a SQL Server 2005 database, A and B.There is
This is a study project. I have three database classes A,B,C. There is a
We have three developers and one tester all working against the same database. We
I have some xml data contained in three files (Database.xml, Participants.xml, and ConditionTokens.xml). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.