不确定在 codeigniter 中将 where 子句放在哪里

Not sure where to put the where clause In codeigniter

我一直在查看 codeigniters 网站上的活动记录指南,我想弄清楚在模型或控制器中放置 where 子句的位置?我认为它在模型中,但不确定如何实现它。 $哪里="EmpName='Donny"; $this->db->where($where);

控制器调用 Home.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Home extends CI_Controller 
{
    public function index() 
    {

        $this->load->model('HomeModel');
        $data['records'] = $this->HomeModel->getData();
        $this->load->view('HomeView',$data);
    }
}

HomeModel.php

<?php 

class HomeModel extends CI_Model{

public function getData(){

    $query = $this->db->get('requests');
    return $query->result();
}

}

HomeView.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home View</title>
</head>
<body>
    <h1>Our DB Results:</h1>
    <?php 

foreach($records as $r){

echo $r->EmpName."   ".$r->Department."<br>";

};?>

</body>
</html>

试试这个

$query = $this->db->get_where('requests', array(  'EmpName' => 'Donny'  ));
return $query->result();

试试这个

$query = $this->db->get_where('requests', array('id' => $id));

你可以使用

$this->db->where('empname', 'Donny');