简体   繁体   English

Codeigniter:如何通过user_id获取用户数据

[英]Codeigniter : how can i get user data by user_id

I'm still very new to this codeigniter. So i have a tables in my database called users table and t_lowongan table.我对这个 codeigniter 还是很陌生。所以我的数据库中有一个名为users表和t_lowongan表的表。 The t_lowongan table have user_id field as a foreign key for the id_user in the users table. t_lowongan表将user_id字段作为users表中id_user的外键。 So when the user login and create a job posting, the user_id will be filled by the id_user from that user.因此,当用户登录并创建职位发布时, user_id将由该用户的id_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: 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);
    }

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 ).首先,您必须了解数据库对象的result()方法以$row->field (或者在您的情况下$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']).另一方面,数据库对象的result_array()方法返回 arrays 的数组(在您的例子中为 $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.由于您已经使用了前者,我希望您已经在随后显示的lowongan视图中相应地处理了这些行对象。 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.假设您已经处理了视图,接下来要做的第二件事是放置一个error_log()语句并查看它返回的值。 You can place something like this just after the get_where() statement and then observe your HTTP server logs what value it's getting:您可以在get_where()语句之后放置类似这样的内容,然后观察您的 HTTP 服务器记录它获得的值:

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.根据这是否打印了正确的值,您可以进一步排查问题的确切位置。

You must create controller and model and call model from construct您必须创建 controller 和 model 并从构造中调用 model

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: 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();
    }
}

So what i did is that.所以我所做的就是那样。 I create a function called get_data on my model我在我的 model 上创建了一个名为 get_data 的 function

The model: model:

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

And in the controller i just do this:在 controller 中,我只是这样做:

The Controller: 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.它有效:D。 But please let me know if this is like a wrong thing to do, Thank you:D但是请让我知道这是否是错误的做法,谢谢:D

Try this尝试这个

 public function lowongan() { $this_id = $this->session->userdata('id_user'); $data['t_lowongan'] = $this->db->get_where('t_lowongan', ['user_id', $this_id])->row(); $this->load-view('lowongan', $data); }

in your views在你看来

<?php print_r($t_lowongan)?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM