Welcome Guest, Not a member yet? Register   Sign In
Displaying Single Results -- Stumped, but I know the answer is probably right in front of me =)
#1

[eluser]Greg Aker[/eluser]
I'm trying to build a REALLY simple backend for a "practice test" table that resides in my Expression Engine database.

I have watched the video tutorials & Derek Allard's tutorial (killin' by the way).

I want the administrators of the test to be able to look at & update questions that have been put in the db and I want to display them by the third URI segment, which is the ID of the question.

I am not dealing with models in this, as I want to get the bare-bones basics of CI for this really simple app.

I have the following controller, but, much to my dismay, the view file only "echo's" the word "OBJECT". Can anyone tell me what I'm doing wrong??

Thanks in advance! =)

-greg

Code:
function viewQuestion()
    {
        $data['title'] = "Question View";
        $data['heading'] = "Question View";
        
        $urlSegment = $this->uri->segment(3);

        $data['cat_id'] = $this->db->query('SELECT cat_id FROM test WHERE ques_num = "$urlSegment"');
        $data['question'] = $this->db->query('SELECT question FROM test WHERE ques_num = "$urlSegment"');
        $data['ans_1'] = $this->db->query('SELECT ans_1 FROM test WHERE ques_num = "$urlSegment"');
        $data['ans_2'] = $this->db->query('SELECT ans_2 FROM test WHERE ques_num = "$urlSegment"');
        $data['ans_3'] = $this->db->query('SELECT ans_3 FROM test WHERE ques_num = "$urlSegment"');
        $data['ans_4'] = $this->db->query('SELECT ans_4 FROM test WHERE ques_num = "$urlSegment"');
        $data['real_ans'] = $this->db->query('SELECT real_ans FROM test WHERE ques_num = "$urlSegment"');
        $data['extra'] = $this->db->query('SELECT extra FROM test WHERE ques_num = "$urlSegment"');
        $data['img'] = $this->db->query('SELECT img FROM test WHERE ques_num = "$urlSegment"');

        $this->load->view('viewQuestion_view', $data);
    }
#2

[eluser]wiredesignz[/eluser]
Controller:
Code:
$query = $this->db->query('SELECT * FROM test WHERE ques_num = "$urlSegment"');

$data['question'] = $query->result() // or result_array()

$this->load->view('viewQuestion_view', $data);

viewQuestion_view:
Code:
echo question->cat_id;
...
#3

[eluser]Kemik[/eluser]
That's a lot of queries!

How come you haven't made them in to one?
Code:
$this->db->select('cat_id, question, ans_1, ans_2, ans_3, ans_4, real_ans, extra, img');
$this->db->where('ques_num', $urlSegment);
$this->db->from('test');
#4

[eluser]Greg Aker[/eluser]
Wow, fast responses! Many thanks.

I have tried both things you have suggested, and I am afraid I'm still banging my head against my desk.

in order to keep the code short, my controller is:
Code:
function viewQuestion()
    {
        $data['title'] = "Question View";
        $data['heading'] = "Question View";
        
        $urlSegment = $this->uri->segment(3);

        $query = $this->db->query('SELECT * FROM test WHERE ques_num = "$urlSegment"');
        $data['question'] = $query->result(); // or result_array()

        $this->load->view('viewQuestion_view', $data);
    }


and a bit of my view file looks like this:

Code:
<p>&lt;?=$question->question?&gt;</p>
<ol>
  <li>&lt;?=$question->ans_1?&gt;</li>
  <li>&lt;?=$question->ans_2?&gt;</li>
  <li>&lt;?=$question->ans_3?&gt;</li>
  <li>&lt;?=$question->ans_4?&gt;</li>
</ol>

I have tried taking the uri segment variable out, and making it a "hard number." Yes, the number I entered is in the database =) but, I'm not getting anything. No errors, and no dynamic info. The "static" code in my view file shows up, but nothing the controller is trying to feed to it from the db.

Thanks again,

-greg
#5

[eluser]Rick Jolly[/eluser]
You have your quotes mixed up:
Code:
// this:
$query = $this->db->query('SELECT * FROM test WHERE ques_num = "$urlSegment"');
// should be this:
$query = $this->db->query("SELECT * FROM test WHERE ques_num = '$urlSegment'");
You should use query bindings to prevent sql injection.
#6

[eluser]Kemik[/eluser]
I believe $query->result() returns an object and should be used as
Code:
foreach ($query->result() as $row) {

If you just want to return the first row try
Code:
$row = $query->row();

Or in your case
Code:
$data['question'] = $query->row();
#7

[eluser]Greg Aker[/eluser]
That did it! :-)

Thanks so much to everyone for responding so fast! What a great community!

-greg




Theme © iAndrew 2016 - Forum software by © MyBB