Welcome Guest, Not a member yet? Register   Sign In
selecting data from two tables
#1

[eluser]Unknown[/eluser]
Hi
I'm kinda new to codeigniter and I have a problem Smile I'm making online testing site, which uses already existant database with questions, answers etc.

SQL sample structure: http://pastebin.com/jFag1QdU

I need to generatr form that will look something like that:

1. Question 1
a) answer A
b) answer B
c) answer C

2. Question 2
a) answer A
b) answer B
c) answer C


I made it this way:

Model:
Code:
<?php

class Test_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    function get_questions($cat_id)
    {
        $this->db->select('questions.questioncontent,
                           answers.answercontent');
        $this->db->from('questions', 'answers');
        $this->db->join('answers', 'answers.questionid = questions.id ');
        $this->db->where('questions.categoryid', $cat_id);
        $query = $this->db->get();
        return $query->result();  
    }

}
Controller
Code:
<?php

class Test extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('test_model');
    }
    
    function index()
    {
        // Welcome site
        $this->load->view('index');
        
    }
    
    function show($cat_id)
    {
        $data['questions'] = $this->test_model->get_questions($cat_id);
        $this->load->view('show', $data);
    }
}

View:
Code:
<!doctype html>
&lt;head&gt;

  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Online Testing&lt;/title&gt;

  
&lt;/head&gt;

&lt;body&gt;

<h1>Test Started</h1>

&lt;?php foreach($questions as $question): ?&gt;
<p><strong>&lt;?php echo $question->questioncontent ?&gt;</strong></p>
&lt;?php echo $question->answercontent ?&gt;
&lt;?php endforeach; ?&gt;

    

&lt;/body&gt;
&lt;/html&gt;

And my problem is that I get output that looks like that:

Quote:Test Started

How many objects can you construct at once?
Seven

How many objects can you construct at once?
One

How many objects can you construct at once?
Two

How many objects can you construct at once?
Four

Which programming language is the best?
Java

Which programming language is the best?
Python

Which programming language is the best?
Turbo Pascal

Which programming language is the best?
PHP

How many objects can you construct at once?
Seven

How many objects can you construct at once?
One

How many objects can you construct at once?
Two

How many objects can you construct at once?
Four

Is there any way to get rid of questions appearing for each answer and make it look like I showed you at the beginning ?

Thx in advance Smile
#2

[eluser]mr lister[/eluser]
A nested loop is the way I would achieve what you are trying to achieve.

Using your 'id' from your 'questions' table as the primary key you need another, (nested), loop to obtain the answers from your 'answers' table - as your 'questionid' is a 'foreign key' - you select the rows from the 'answers' table where 'questionid' = 'id'.

Probably not the most efficient way, but, this is how I would do it:
* In your controller, query the 'questions' table to obtain returned array, which will include the 'id'
* In your view page, the outer loop contains the questions, with a loop nested in that loop which will obtain the answers from your 'answers' table with another query where 'questionid' = 'id'.

Hope that makes sense to you and helps you.
#3

[eluser]jmadsen[/eluser]
that would be horribly inefficient.

run the single query, loop through the results, but only echo the question when the id has changed.
#4

[eluser]mr lister[/eluser]
Ah yes, I see how that works.

I am still getting over the Dreamweaver / Developer Toolbox way of doing things - using their nested loops.

The M-V-C concept is far different to what I was used too, but I think is it a far better way of doing things.

[Edit]
Actually, the more I think about it, the more confused I am about it... in the foreach loop you would have to have some conditional statement that checks the 'id' value.

Something along the lines of?:
Code:
&lt;?php
// declare a value outside the loop to check question id
// i think you have to declare the variable in the controller
// and pass it to the view??
$id_value = 0;
?&gt;

&lt;?php foreach($questions as $question) ?&gt;

&lt;?php
if ( $id_value != $question->id )
{ // initially $id_value = 0, so condition is true
  // so show the question.
?&gt;
<p><strong>&lt;?php echo $question->questioncontent ?&gt;</strong></p>
&lt;?php
  // since the 'if' statement is true and the question
  // shown the first time, put the $question->id into
  // the $id_value
$id_value = $question->id;
}
?&gt;

&lt;?php echo $question->answercontent ?&gt;

&lt;?php endforeach; ?&gt;

That should work?




Theme © iAndrew 2016 - Forum software by © MyBB