skip to Main Content

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


  1. Chosen as BEST ANSWER

    So what i did is that. I create a function called get_data on my model

    The model :

    public function get_data($where, $table){
            return $this->db->get_where($table, $where);
        }
    

    And in the controller i just do this :

    The Controller :

    public function lowongan()
        {
            $this_id = $this->session->userdata('id_user');
            $data['t_lowongan'] = $this->m_lowongan->get_data($where,'t_lowongan')->result();
            $this->load-view('lowongan', $data);
        }
    

    And it works :D. But please let me know if this is like a wrong thing to do, Thank you :D


  2. 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’s result_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 the lowongan 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 the get_where() statement and then observe your HTTP server logs what value it’s getting:

    error_log("THE RESULT IS: " . print_r($data, true));
    

    Depending on whether or not this is printing the correct value, you can then further troubleshoot where exactly the problem lies.

    Login or Signup to reply.
  3. You must create controller and model and call model from construct

    constroller :

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class LowonganController extends CI_Controller {
        public function __construct()
        {
            parent::__construct();
            $this->load->model(array('m_lowongan'));
        }
    
        public function lowongan() {
            $id = $this->session->userdata('id_user');
            $data['t_lowongan']  = $this->m_lowongan->get_user_by_id($id);
            $this->load-view('lowongan', $data);
        }
    }
    

    model:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    class M_Lowongan extends CI_Model {
        public function __construct()
        {
            parent::__construct();
        }
        public function get_user_by_id($id)
        {
            $this->db->where('user_id', $id);
            $query  = $this->db->get('your_table_name');
            return $query->row_array();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search