2015-02-05 92 views
0

我一直在查看codeigniters网站上的活动记录指南,我想知道哪里把where子句放在模型或控制器中?我认为它在模型中,但不知道如何实现它。 $ where =“EmpName ='Donny”; $这 - > DB->其中($其中);不知道在哪里把where子句在codeigniter

控制器称为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> 
+0

我留下了评论,有人留下的answere,它已被删除?那就更奇怪了 – Donny 2015-02-05 23:41:25

+0

是的..只在模型文件中输入你的where子句... – 2015-02-06 12:14:45

回答

0

试试这个

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

虽然这段代码可以解决问题,[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely基于代码的答案)真的有助于提高您的帖子的质量。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – msrd0 2015-02-06 13:49:01

+0

@Kamlesh你工作得很好 – Donny 2015-02-06 21:47:03

1

试试这个

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

可以使用

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

db中没有ID,它被称为RequestNumber,但是当我添加说变量没有定义 – Donny 2015-02-06 21:45:38

+0

@ Donny..yes agree ..我只给出了解决方案的例子..hope用正确的表字段它将工作..谢谢 – 2015-02-09 05:23:40