【发布时间】:2018-06-09 14:49:24
【问题描述】:
我正在尝试使用 CodeIgniter 通过键 'series' 和 'id' 拉取照片来设置路线。它会遵循这个逻辑:
photos/ :返回数据库中的所有表“照片”。
photos/series :返回与某个系列匹配的所有照片。 'photos/nature' 将返回所有带有 'series' 键的照片 'nature'。
photos/series/id:返回单张照片。 'photos/nature/1' 将返回一张照片,其中键 'series' 等于 'nature' 和 'id' 等于 '1'
调用 'http://localhost/public_html/photos/nature/1' 我收到以下错误:
遇到 PHP 错误
严重性:错误
消息:调用未定义的方法 CI_DB_mysqli_result::where()
文件名:models/Photos_model.php
行号:14
回溯:
我的“照片”表结构:
CREATE TABLE `photos` (
`filename` varchar(45) NOT NULL,
`series` varchar(45) NOT NULL,
`id` int(11) NOT NULL,
`caption` varchar(45) NOT NULL,
`description` varchar(45) NOT NULL,
PRIMARY KEY (`filename`))
'photos' 表的值:
INSERT INTO `photos` VALUES
('file00053809264.jpg','nature',1,'waterfall','description of waterfall'),
('file000267747089.jpg','nature',2,'forest','description of burning forest'),
('file000325161223.jpg','cloudburst',1,'sky','description of sky'),
('file000267804564.jpg','cloudburst',2,'bursting abstract','description bursting abstract'),
('file000132701536.jpg','landmarks',1,'taj mahal','description taj mahal');
我在 CodeIgniter 中的照片控制器:
<?php
class Photos extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('photos_model');
$this->load->helper('url_helper');
$this->load->helper('html'); // needed for header
$this->load->helper('url'); // needed for footer
}
public function view($series = NULL, $id = NULL) {
$data['photos'] = $this->photos_model->get_photos($series, $id)->result_array();
if (empty($data['photos'])){
show_404();
}
$data['title'] = 'Photos';
$this->load->view('templates/header', $data);
$this->load->view('photos/view', $data);
$this->load->view('templates/footer');
}
public function index() {
$data['photos'] = $this->photos_model->get_photos();
$data['title'] = 'Photos';
$this->load->view('templates/header', $data);
$this->load->view('photos/index', $data);
$this->load->view('templates/footer');
}
}
我的照片模型:
<?php
class Photos_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_photos($series = FALSE, $id = FALSE) {
if ($series === FALSE && $id === FALSE) {
$query = $this->db->get('photos');
} else if ($id === FALSE) {
$query = $this->db->get_where('photos', array('series' => $series));
} else {
$query = $this->db->get('photos')->where('series', $series)->where('id', $id);
}
return $query->result_array();
}
}
我的“照片/view.php”视图:
<?php
echo $photos['photos'];
我的路线配置:
$route['photos/'] = 'photos/view';
$route['photos/(:any)'] = 'photos/view/$1';
$route['photos/(:any)/(:num)'] = 'photos/view/$1/$2';
如果你能告诉我我在哪里以及如何滑倒,你能解释一下吗?谢谢。
【问题讨论】:
标签: php mysql codeigniter mysqli routes