如何使用CodeIgniter从数据库中检索数据?

时间:2016-07-15 08:48:40

标签: php mysql codeigniter

我已为所有视图创建了一个Controller,现在为所有操作创建了一个模型我想要显示数据库中的数据。如何使用codeigniter实现这一目标?

这是我的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

任何人都知道为什么我的数据没有显示??

3 个答案:

答案 0 :(得分: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); 
}

答案 1 :(得分: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");

答案 2 :(得分: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文件中我认为你正在加载错误的文件,请检查它。