Welcome Guest, Not a member yet? Register   Sign In
[question - solved] Join - active record
#1

[eluser]wemago[/eluser]
oks i couldnt find any awnser for this in the forum.

Code:
$this->db->select('username');
        $this->db->from('users');
        $this->db->join('articles', 'articles.author_id = users.user_id');
        
        return $this->db->get();

I'm trying to get a username and all i get is this ->
Quote:Object id #19

any idea why?it should give me the name.

thanx
#2

[eluser]Michael Wales[/eluser]
It's doing what it should - it's returning the result object that you requested.

Now you need to do something with that result object, like:

Code:
if ($query->num_rows() > 0) {
  $row = $query->row();
  echo $row->username;
}

User Guide: Generating Query Results
#3

[eluser]wemago[/eluser]
...ok...to me -> "DUH!" lol
stupid me xD

thanx for the tip wales Smile
#4

[eluser]wemago[/eluser]
oks, i'm doing something really stupid.

what I'm trying to get is the username,

I've got a table for articles where we can find a author_id,
i wanna list all articles showing title, author name and publish date.

to get the article info is everything ok and shows it all.

how should i get the username?

my first idea was to do a 2nd function in articles model where i would get
the username but i forgot that i do a foreach in the view, what happens is that
it gives the same username for all of them.

so probably i should merge the get articles function with the get author
do a join and foreach one time right?

any tip?

p.s- I'm really slippy so sorry for the confusion
#5

[eluser]wemago[/eluser]
ok...and again...DUH... lol
i really need a sleep.

for those who need an answer for this,
this is what i did and works Smile

Code:
$this->db->select('lab_users.username,lab_articles_categories.category_title, lab_articles.*');
        $this->db->join('users', 'articles.author_id = users.user_id');
        $this->db->join('articles_categories', 'articles.category_id = articles_categories.category_id');
        $this->db->orderby('article_id', 'asc');
        $this->db->groupby('lab_articles.article_id');
        $this->db->offset($offset);
        $this->db->limit($num);
        
        return $this->db->get('articles');
#6

[eluser]awpti[/eluser]
One thing I do in my models:

$this->db->.....

$data = $this->db->get('table');

return $data->result();

No sense in returning the result object when you can just return the data and start manipulating it right off the bat.

Example:

Code:
function countup($link_id) {
        $this->db->query('UPDATE awLinks SET link_clicks = (link_clicks + 1) WHERE link_id = '.$link_id);
        $this->db->select('link_url')->where('link_id', $link_id);
        $link = $this->db->get('awLinks');
        return ($link->num_rows() > 0) ? $link->row() : FALSE;
    }




Theme © iAndrew 2016 - Forum software by © MyBB