简体   繁体   English

使用PHP在Codeigniter中联接两个表

[英]Joining two tables in Codeigniter using PHP

I am trying to join two tables and return an array in my Model method in CodeIgniter with php. 我试图连接两个表,并在PHP的CodeIgniter中的Model方法中返回一个数组。 I've gone through a few previously posted similar questions on stackoverflow and modified my code accordingly. 我经历了一些以前在stackoverflow上发布过类似问题的问题,并相应地修改了我的代码。 But they don't seem to work. 但是它们似乎不起作用。 Hence would love to know what's wrong with the following. 因此,很想知道以下内容有什么问题。

I'm using the following method but am currently getting exceptions. 我正在使用以下方法,但当前正在获取异常。 Would appreciate suggestions in this regard. 希望在这方面提出建议。

Model Method 模型方法

public function getUserDetails($username)
{
    $uid = $this->getUserUid($username);
    $this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age');
    $this->db->from('sysuser as s');
    $this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left');
    $this->db->where(array('s.uid' => $uid));
    $query = $this->db->get();
    return $query->result();
}

Controller 控制者

$data1['details'] = $this->userModel->getUserDetails($username);
$this->load->view('studentDashboard/viewProfile',$data1);

View 视图

...
<h2>
   <?php foreach($details as $detail){?>
        <?php echo $detail->s.name;?>
   <?php }?>
</h2>
...

In the view, I've also tried just echoing $detail->name but this doesn't work either. 在视图中,我还尝试仅回显$detail->name但这也不起作用。

  1. At first, use print_r($details) for checking your data. 首先,使用print_r($details)检查数据。 If it's returning anything or not. 是否返回任何东西。

  2. Then echo your value like this $detail['name'] 然后像这样$ detail ['name']回显您的值

Fixed Code: 固定代码:

public function getUserDetails($username)
{
    $uid = $this->getUserUid($username);
    $this->db->select("*");
    $this->db->from('sysuser');

    $this->db->join('studentprofile', 'studentprofile.uid = sysuser.uid');

    $this->db->where('sysuser.uid',$uid);
    $query = $this->db->get();
    return $query->result();
}

Look, i´m not sure that i understood your code, what means this line 看,我不确定我是否理解您的代码,这行是什么意思

$uid = $this->getUserUid($username);

You´re calling a method and sending the name to retrieve the userid, right? 您正在调用方法并发送名称以检索用户ID,对吗? I´ll write that method like you should have it: 我将像您应该的那样编写该方法:

public function getUserid($user){
    $this->where->id($user);
    return $this->get('whatever table')->row();
    //i think you forgot this ->row()
}

then 然后

public function getUserDetails($username)
{
$uid = $this->getUserUid($username); 
//here already you should bring with ->row() already

//you can use this var_dump here to confirm too
//var_dump($uid); 
//exit;

$this->db->select('s.uid, s.name,s.phone, s.studentId, s.type, sp.profiledesc, sp.age'); 
//line´s right

//the from method is disposable, so i put it into the get but here it´s right too

$this->db->join('studentprofile as sp', 's.uid = sp.uid', 'left'); //ok

$this->db->where($uid); //this line is the wronger, i´ve made the fix
//as this is an where, you´d bring only one value with ->row() from that other method

$query = $this->db->get('sysuser as s'); 
//the 'from' i putted here, just to write a line less
return $query->result();

when you need to test what you´re returng do a var_dump here
//commenting the return above
//$test = $query->result();
//var_dump($test);

} }

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

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