I’m still very new to this codeigniter. So i have a tables in my database called users
table and t_lowongan
table. The t_lowongan
table have user_id
field as a foreign key for the id_user
in the users
table. So when the user login and create a job posting, the user_id
will be filled by the id_user
from that user. I want to get the user data so that this user know which job posting that he has created by that user, but the data isn’t showing, how can i fix it??
The Controller Lowongan :
public function lowongan()
{
$this_id = $this->session->userdata('id_user');
$data['t_lowongan'] = $this->db->get_where('t_lowongan', ['user_id', $this_id])->result();
$this->load-view('lowongan', $data);
}
3
Answers
So what i did is that. I create a function called get_data on my model
The model :
And in the controller i just do this :
The Controller :
And it works :D. But please let me know if this is like a wrong thing to do, Thank you :D
Your approach to debugging isn’t quite right here. Firstly, you must understand that the database object’s
result()
method returns an array of row objects in the form of$row->field
(or in your case,$row->user_id
). On the other hand, the database object’sresult_array()
method returns an array of arrays (in your case, $row[‘user_id’]). Since you’ve used the former, I hope you’ve dealt with those row objects accordingly in thelowongan
view which you then show. Failure to do so might result in the problem you’ve described in the question.Assuming you’ve taken care of the view, the second thing to do then is to place an
error_log()
statement and see what value it’s returning. You can place something like this just after theget_where()
statement and then observe your HTTP server logs what value it’s getting:Depending on whether or not this is printing the correct value, you can then further troubleshoot where exactly the problem lies.
You must create controller and model and call model from construct
constroller :
model: