2015-02-05 28 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