使用Codeigniter加入查询

时间:2015-08-10 06:05:16

标签: php mysql codeigniter

我想在codeigniter中加入两个表。我有两个名为名称员工的表格。我创建了使用下拉列表选择人员职称,但这会使用外键值显示数据表中的数据。现在我想在指定表中显示标题而不是外键值。请任何人可以帮助我?

控制器

request.testDevices = @[kGADSimulatorID];

模型

 public function index()
{  

    $data['designation'] = $this->staff_model->get_designation();

    $this->load->view('admin_include/header');
    $this->load->view('admin_pages/staff_list',$data);
}

    public function fillgrid(){
        $this->staff_model->fillgrid();
    }

查看

class Staff_model extends CI_Model {

//get designation table to populate the designation dropdown
function get_designation()     
{ 
    $this->db->select('designation_id');
    $this->db->select('title');
    $this->db->from('designation');
    $query = $this->db->get();
    $result = $query->result();

    $designation_id = array('-SELECT-');
    $title = array('-SELECT-');

    for ($i = 0; $i < count($result); $i++)
    {
        array_push($designation_id, $result[$i]->designation_id);
        array_push($title, $result[$i]->title);
    }
    return $designation_result = array_combine($designation_id, $title);
}


public function fillgrid(){
    $this->db->order_by("id", "desc"); 
    $data = $this->db->get('staff');

    foreach ($data->result() as $row){
        $edit = base_url().'index.php/staff_controller/edit/';
        $delete = base_url().'index.php/staff_controller/delete/';
        echo "<tr>
                    <td>$row->first_name</td>
                    <td>$row->last_name</td> 
                    <td>$row->designation_id</td>
                    <td>$row->email</td>
                    <td>$row->address</td>
                    <td>$row->contact_no</td>
                    <td>$row->work_experience</td>    
                    <td>$row->qualifications</td>    
                    <td>$row->created</td>
                    <td><a href='$edit' data-id='$row->id' class='btnedit' title='edit'><i class='glyphicon glyphicon-pencil' title='edit'></i></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='$delete' data-id='$row->id' class='btndelete' title='delete'><i class='glyphicon glyphicon-remove'></i></a></td>    
                </tr>";

    }
    exit;
}

1 个答案:

答案 0 :(得分:0)

您可以像这样加入表:

$data = $this->db->select('*')->from('staff')
  ->join('designation', 'staff.designation_id = designation.designation_id');
  ->order_by("id", "desc")->get();
$result = $data->result(); // every result now has 'title'

此外,我强烈建议从模型中删除“echo html”,然后返回一个数组。你可以在视图中做任何你喜欢的事情。看看这个:PHP simple foreach loop with HTML