我正在尝试在php codeigniter中建立一个项目。但是我一直坚持从数据库中获取数据。
我正在使用mysql,codeigniter-3.1.10和php-7.0.33。
数据库配置文件:
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '221b',
'database' => 'car',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
我的模特是
if (defined('BASEPATH') OR exit ('No direct script access allowed'));
class Homepage_model extends CI_Model{
public $result;
public $rs;
public function __construct(){
parent::__construct();
$this->load->database();
}
public function get_data($slug = FALSE){
if ($slug === FALSE) {
$query = $this->db->get('cars');
return $query->result_array();
}
$query = $this->db->get_where('cars', array('status' => "Available"));
return $query->row_array();
}
}
在模型文件中,当数据库字段“状态”的值为“可用”时,我想从桌车中获取所有数据。 控制器文件:
defined('BASEPATH') OR exit('No direct script access allowed');
class Homepage extends CI_Controller{
public function __construct(){
parent::__construct();
}
public function index(){
$this->load->model('Homepage_model');
$rs = $this->Homepage_model->get_data();
$this->load->view('homepage_view', $rs);
}
}
我要显示数据的视图部分是:
foreach($rs as $rws){
//echo $rws['car_id'];
?>
<li>
<?php echo $rws['car_id'];?>
<a href="book_car.php?id=<?php echo $rws['car_id'] ?>">
<img class="thumb" src="cars/<?php echo $rws['image'];?>" width="300" height="200">
</a>
<span class="price"><?php echo 'Kshs.'.$rws['hire_cost'];?></span>
<div class="property_details">
<h1>
<a href="book_car.php?id=<?php echo $rws['car_id'] ?>"><?php echo 'Car Make>'.$rws['car_type'];?></a>
</h1>
<h2>Car Name/Model: <span class="property_size"><?php echo $rws['car_name'];?></span></h2>
</div>
</li>
<?php
}
?>
我已经做到了。但是当我浏览网页时,这些数据都没有显示。请帮助解决此问题。
答案 0 :(得分:1)
在您的控制器功能中
传递$data
变量,例如$this->load->view('homepage_view', $data);
public function index(){
$this->load->model('Homepage_model');
$result = $this->Homepage_model->get_data();
$data['rs'] = $result;
$this->load->view('homepage_view', $data);
}
答案 1 :(得分:0)
查询中的内容已更改
$query = $this->db->get_where('cars', array('status' => "Available"))->result_array();
return $query;
答案 2 :(得分:0)
在模型中
public function get_data($slug = FALSE){
if ($slug === FALSE) {
$query = $this->db->get('cars');
return $query->result_array();
}
$query = $this->db->get_where('cars', array('status' => "Available"));
$result = $query->result_array(); # change
return $result; # change
}
在控制器中
public function index(){
$this->load->model('Homepage_model');
$result = $this->Homepage_model->get_data();
$data['rs'] = $result; # or $data['rs'] = $this->Homepage_model->get_data(); assign directly
$this->load->view('homepage_view', $rs);
}
在视图中 foreach($rs as $rws){
将有效
确保表格不为空。
答案 3 :(得分:0)
只需将$row_array()
更改为$result_array()
return $this->db->get_where('cars', array('status' => "Available"))->result_array();
row_array()
仅用于获取一个$result_array()
关联数组形式的所有记录