gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:15:38 25 4
gpt4 key购买 nike

我已经为所有 View 创建了一个 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

谁能知道为什么我的数据不显示??

最佳答案

您需要修改 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);
}

关于php - 如何使用 CodeIgniter 从数据库中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com