Welcome Guest, Not a member yet? Register   Sign In
Finding out my users name by their ID in a comment..
#1

[eluser]Medikal[/eluser]
Alright so I have an article that lists comments just fine, however it pulls it from a comments table, so it doesn't say posted by their username, it says posted by their userID. I made a function in my user model that requires the id as the only argument, and returns the row with all their information.

I output the page like so:
$viewDeal['thisDeal'] = $this->deals_model->viewdeal();
$viewDeal['comments'] = $this->deals_model->grabcomments();
$viewDeal['main_content'] = 'viewdeal_view';
$this->load->view('includes/template', $viewDeal);


So on the view file I can do $comments->userID and it will display the ID, I'm trying to find the username which is in a different table. I can do this in an inefficient manner, I'm just trying to learn the traditional MVC nature of where I would do it.

Thank you.
#2

[eluser]techgnome[/eluser]
pull the user name when you pull the comments... make it part of the data when you pull it.

-tg
#3

[eluser]Medikal[/eluser]
Right, but I don't understand how since a models function can only return ne thing, and it currently returns the comment information. My grabcomments function is as follows:

Code:
$this->db->select('*');
                $this->db->from('comments');
                $this->db->order_by('time', 'desc');
                $this->db->where('dealID', $this->uri->segment(3));
                $query = $this->db->get();

if ($query->num_rows() > 0)
                    {
                        foreach ($query->result() as $row)
                            {
                                $data[] = $row;
                            }
                            return $data;
                    }

So I thought maybe do something like this:
Code:
$authorInfo = $this->db->select('*');
                $authorInfo = $this->db->from('users');
                $authorInfo = $this->db->where('userID', $query['userID']);
                $authorInfo = $this->db->get();
but I'm unsure how I make the function return it as well as the comment information, and then how I would pull it in the controller and display in the view.
#4

[eluser]TechWench[/eluser]
Techgnome is saying to pull it all in at the same time, in the same query.
Code:
$this->db->select('*')
     ->from('comments')
     ->join('users', 'comments.userID = users.userID')
     ->order_by('time', 'desc')
     ->where('dealID', $this->uri->segment(3));
#5

[eluser]Developer13[/eluser]
@Medikal - TechWench is right - have a look into how to use joins in your queries. Also, you may want to rethink looping through your results and assigning them to an array and returning that array. Instead, just return the result object as it is, or return $query->result_array() if you'd like it in an array instead.
#6

[eluser]techgnome[/eluser]
That's a common theme I seem to notice among newish people when it comes to MVC (in general, not just with CI) ... there's this misguided notion that a model can only represent a single table, when in fact, the reality is that models should represent a dataview... which can come from multiple sources (heck, it doesn't even need to actually come from a database).

Models are called that for a reason... they should MODEL the data that you want to display (or need).

-tg
#7

[eluser]Brandon Beasley[/eluser]
[quote author="techgnome" date="1290025075"]That's a common theme I seem to notice among newish people when it comes to MVC (in general, not just with CI) ... there's this misguided notion that a model can only represent a single table, when in fact, the reality is that models should represent a dataview... which can come from multiple sources (heck, it doesn't even need to actually come from a database).

Models are called that for a reason... they should MODEL the data that you want to display (or need).

-tg[/quote]

Very good explanation.

I mostly stack my returned data and then pass back a multidimensional $data array:

Code:
public function getDataFromTwoTables(){
$queryOne = $this->db->select('*')->from('users')->where('userID', $query['userID']);
$queryOne = $this->db->get();  

$queryTwo = $this->db->select('*')->from('comments')->where('dealID',$this->uri->segment(3));
$queryTwo = $this->db->get();

$data['tableOneData'] = $queryOne->result;
$data['tableTwoData'] = $queryTwo->result;

return $data;
}




Theme © iAndrew 2016 - Forum software by © MyBB