我已經創建了一個控制器的所有視圖和一個模型的所有操作現在我想顯示數據庫中的數據。我怎樣才能達到這個使用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
任何人都知道爲什麼我的數據不顯示??
使用'$這 - >負載>視圖( '索引',$數據); '因爲視圖文件名是index.php文件 – Saty
我沒有,但同樣的錯誤:( – theromie
指數是不是一個視圖,它的一個PHP路由Page – Brad