2016-07-15 151 views
1

我已經創建了一個控制器的所有視圖和一個模型的所有操作現在我想顯示數據庫中的數據。我怎樣才能達到這個使用codeignitor?如何使用CodeIgnitor從數據庫中檢索數據?

這是我MyController.php

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 

    class MyController extends CI_Controller { 
     public function __construct() { 
      parent::__construct();   
    } 
    public function idex(){ 
     $this->load->helper(array('form', 'url', 'email')); 
     $this->load->model('mymodel'); 
} 
/*Load View Files Controller*/ 
public function home(){ 
    $this->load->view("index"); 
} 
public function postjob(){ 
    $this->load->view('postjobs');  
} 
public function talents(){ 
    $this->load->view('talents');   
} 
public function profile(){ 
    $this->load->view('submitprofile');  
} 
public function jobdetails(){ 
    $this->load->view('jobdetails');   
} 
public function aboutus(){ 
    $this->load->view('aboutus');  
} 

/*Call POSTJob Controller*/ 
public function post(){ 
    $this->load->library('form_validation'); 
    $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email', 
     array(
      'required' => 'enter email id')); 

    $this->form_validation->set_rules('JTitle', 'Job Title', 'trim|required|min_length[6]', 
     array(
      'required' => 'enter JOB TITLE')); 

    $this->form_validation->set_rules('JType', 'Job Type', 'trim|required', 
     array(
      'required' => 'select job type')); 

    $this->form_validation->set_rules('Salary', 'Salary', 'trim|required', 
     array(
      'required' => 'enter salary')); 

    $this->form_validation->set_rules('Exp', 'Experience', 'trim|required', 
     array(
      'required' => 'select Experience')); 

    $this->form_validation->set_rules('Skills', 'Skills', 'trim|required', 
     array(
      'required' => 'enter skills')); 

    $this->form_validation->set_rules('EduReq', 'Education required', 'trim|required', 
     array(
      'required' => 'enter EDUCATION')); 

    $this->form_validation->set_rules('JLocation', 'Location', 'trim|required', 
     array(
      'required' => 'enter LOCATION')); 

    $this->form_validation->set_rules('CName', 'Company Name', 'trim|required', 
     array(
      'required' => 'enter COMPANY NAME')); 

    $this->form_validation->set_rules('CAdd', 'Company Address', 'trim|required', 
     array(
      'required' => 'enter COMPANY ADDRESS')); 

    $this->form_validation->set_rules('JDesc', 'Job Description', 'trim|required', 
     array(
      'required' => 'enter JOB DESCRIPTION')); 

    if($this->form_validation->run() ==FALSE){ 
     $this->load->view('postjobs');   
    } 
    else{   
     $data = array(
      'email' => $this->input->post('email'), 
      'JTitle' => $this->input->post('JTitle'), 
      'JType' => $this->input->post('JType'), 
      'Salary' => $this->input->post('Salary'), 
      'Exp' => $this->input->post('Exp'), 
      'Skills' => $this->input->post('Skills'), 
      'EduReq' => $this->input->post('EduReq'), 
      'JLocation' => $this->input->post('JLocation'), 
      'CName' => $this->input->post('CName'), 
      'CWeb' => $this->input->post('CWeb'), 
      'CAdd' => $this->input->post('CAdd'), 
      'JDesc' => $this->input->post('JDesc') 
     ); 
     $this->mymodel->insert_job($data); 
     redirect(site_url()); 
    } 
} 
public function get_Records(){ 
    $data = array();  
    $data['result'] = $this->mymodel->get_AllJobs();   
    $this->load->view('index/get_Records',$data); 
}  
} 
?> 

這是我MyModel.php

<?php 
    defined('BASEPATH') OR exit('No direct script access allowed'); 

    class MyModel extends CI_Model { 
    public function __construct() { 
     parent::__construct();   
    } 

public function insert_job($data){  
    $this->db->insert('jobs', $data);  
} 

public function get_AllJobs(){ 
    $this->db->select('*'); 
    $this->db->from('jobs'); 
    $this->db->order_by("JPostID", "DESC"); 
    $query = $this->db->get(); 
    return $query->result();   
} 
} 

這是我的index.php

<?php foreach($result as $row){?>     
       <div class="item" id="joblist"> 
       <div class="row">       
        <div class="col-xs-12" itemscope itemtype="http://schema.org/JobPosting"> 
         <h3 class="no-margin-top" style="margin-bottom:0px;"><a itemprop="title" style="text-transform: capitalize;color:#29aafe" href="#"><?=$row->JTitle;?></a></h3> 
         <h5 style="margin-top:0px;text-transform: capitalize;" itemprop="hiringOrganization" itemscope itemtype="http://schema.org/Organization" itemprop="name" ><?=$row->CName;?> On <?=$row->JPDate;?></h6> 
         <h5><span class="color-black" itemprop="employmentType"></span><?=$row->JType;?> - <?=$row->JLocation;?><span class="color-white-mute" style="text-transform: capitalize;" itemprop="jobLocation" itemscope itemtype="http://schema.org/Place"></span></h6> 
        </div>       
       </div> 
       </div> 
       <?php }?> 

當我運行在服務器上的文件它顯示錯誤

A PHP Error was encountered 
Severity: Notice 

Message: Undefined variable: result 

Filename: views/index.php 

Line Number: 70 

Backtrace: 

File: D:\Xampp\htdocs\KDO\application\views\index.php 
Line: 70 
Function: _error_handler 

File: D:\Xampp\htdocs\KDO\application\controllers\MyController.php 
Line: 14 
Function: view 

File: D:\Xampp\htdocs\KDO\index.php 
Line: 315 
Function: require_once 

任何人都知道爲什麼我的數據不顯示??

+0

使用'$這 - >負載>視圖( '索引',$數據); '因爲視圖文件名是index.php文件 – Saty

+0

我沒有,但同樣的錯誤:( – theromie

+0

指數是不是一個視圖,它的一個PHP路由Page – Brad

回答

0

在你的控制器:指數法:

// change it from idex to index 

    public function index(){ 
    $data = array(); 
    $this->load->model('MyModel'); 
    $this->load->helper(array('form', 'url', 'email')); 
    $data['result'] = $this->MyModel->get_AllJobs(); 
    $this->load->view('index',$data);  
    } 
    // make sure the your index in not in any subdirectory 

// __Construct() in controller : 

    public function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('url','text','html','form')); 
    $this->load->library(array('session','form_validation','pagination','upload','email')); 
    $this->load->database(); // if not autoload 
    $this->load->model('MyModel'); 

    } 
    // your edit method 

    public function edit ($id) { 
    $data['title'] = 'edit'; 
    if($id) { 
     $data['users'] = $this->MyModel->getRowByUser($id); // it is model method to fetch the record of that users having id = $id 
    } 

    $this->load->view('edit',$data);  
    } 

模型:

public function get_AllJobs() 
{ 
    $data = array(); 
    $this->db->select('*'); 
    $this->db->from('jobs'); 
    $this->db->order_by("JPostID", "DESC"); 
    $query = $this->db->get(); 
    foreach ($query->result() as $row) { 
     $data[] = $row; 
    } 
    return $query->data;   
} 
+0

OK我沒有,但在錯誤 – theromie

+0

沒有變化包括這在你的控制器__construct():$此 - > load-> database(); – pradeep

+0

數據庫是自動加載的 – theromie

0

您將需要修改MyModel.php ...

public function get_AllJobs(){ 
    $this->db->select('*'); 
    $this->db->from('jobs'); 
    $this->db->order_by("JPostID", "DESC"); 
    $query = $this->db->get(); 
    $result= array(); 
    foreach ($query->result() as $row){ 
      $result[] = $row; 
    } 
    return $result;   
} 

public function get_Records(){ 
    $this->load->model('mymodel'); 
    $data = array(); 
    $data['result'] = $this->mymodel->get_AllJobs(); 
    $this->load->view('index',$data); 
} 
+0

好吧,我改變了模型,但沒有改變 – theromie

+0

我必須改變index.php嗎? – theromie

+0

公共函數get_Records(){$ 數據=陣列(); $ data ['result'] = $ this-> mymodel-> get_AllJobs(); $ this-> load-> view('index',$ data); } – rajeev

0

這是我的一個例子,不使用模型。當然,你會使其適應你的

$this->db->distinct(); 
    $fam = "Name"; 
    $this->db->where("family", $fam); 
    $query = $this->db->get('names'); 
    if ($query->result()) { 
     $data1['name'] = $query->result(); 
    } 

    $data['title'] = "Family Names"; 
    $this->load->view('head/headfam',$data); 
    $this->load->view('include/nav2'); 
    $this->load->view("pages/data", $data1); 
    $this->load->view("foot/footer"); 
0
public function get_Records(){ 
    $data = array();  
    $data['result'] = $this->mymodel->get_AllJobs();   
    $this->load->view('index/get_Records',$data); 
} 
在這個函數

您使用的模型方法,也是你的視圖文件是get_Records但在您的文章您正在使用$result可變進index.php文件我想你正在加載錯誤的文件,請檢查它。