Welcome Guest, Not a member yet? Register   Sign In
Getting DB Result in controller
#1

[eluser]Lockzi[/eluser]
Hello,

here's a problem that's been bothering me for a while now...

In the model I've always been using
Code:
return $query->result();
And it works like a charm for the views where I can loop through the results.
The problem is that I can't get any information out of that query in the controller.

Anyone has any ideas? If you don't understand I can post some code to show even further what I mean if necessary.

Cheers,
Lockzi
#2

[eluser]Jatinder Thind[/eluser]
[quote author="Lockzi" date="1193387082"]If you don't understand I can post some code to show even further what I mean if necessary.
[/quote]

Yes, some code will help.
#3

[eluser]Martín M.[/eluser]
Maybe you are not loading the database library? Maybe you did it in the model but forget to do it in the controller.

I can confirm that doing queries to the DB from a controller works perfectly.
#4

[eluser]Lockzi[/eluser]
Okay, here we go! Smile

Code:
function listTopics($f = null)
    {        
        $this->db->select('*');
        $this->db->where(array('topics.f_id' => htmlspecialchars($f, ENT_QUOTES)));
        
        $this->db->join('members', 'topics.m_id = members.m_id');
        $this->db->join('forums', 'topics.f_id = forums.f_id');
        
        $this->db->orderby('topics.t_time', 'DESC');
        
        $query = $this->db->get('topics');
        
        return $query->result();
    }

And a controller for demonstration

Code:
$data['topics'] = $this->forum->listTopics($data['forum']->f_id);

echo ($data['topics']->f_name);

To explain even further.
The basic problem is that it's returning a looong array since I'm using return result(). If I instead would use return row() it would work perfectly fine, but then I would only get one row.

And there's nothing wrong with the code, so don bother with asking questions about that Wink Since it works fine in the views when I use a foreachloop.

Cheers, Lockzi
#5

[eluser]Lockzi[/eluser]
Loads of views of this topic since my last, more explaining, post and still no replies.

If you need further explaining/examples please let me know. Smile

Cheers,
Lockzi
#6

[eluser]mistere[/eluser]
What happens if you change your last line

FROM:
Code:
echo ($data['topics']->f_name);
TO:
Code:
echo ($data['topics'][0]->f_name);

?

Regards,
Eric
#7

[eluser]ELRafael[/eluser]
ran into many wall but I know I'll get the answer, here, here, here we go again

Why don't you use like that?

Model file
Code:
//
function listTopics(...)
{
  $query = $this->db->get('tabela');
  return $query;
}

Controller file
Code:
//
$result_query = $this->model_file->listTopics();
$data['resultado'] = $result_query;
$this->load->view('view_file', $data);

View file
Code:
<?php foreach ($resultado->result() as $dados) : ?>
<?=$dados->field_from_table;?>. And that is!
<?php endforeach; ?>

If you need to get some value or other thing, use in controller

Code:
if ($result_query->num_rows() > 0) //blah blah blah!
$one = $result_query->row();
echo $one->field_from_table;

Got it?
#8

[eluser]Lockzi[/eluser]
That actually works!

Thanks a lot mistere.

ELRafael, that's what I've been doing for all this time. But that would put a strain on my database, since it will do the query twice, and that's something that I would like to avoid if possible, thanks for the reply though!

Cheers,
Lockzi




Theme © iAndrew 2016 - Forum software by © MyBB