0

我已经用 codeigniter 编写了一个简单的数据库显示控制器,并且正在尝试添加分页。控制器从数据库中获取信息(它非常小,因此不需要模型)并将其发送到视图以进行分页显示。分页链接显示,但由于某种原因,数据库信息未显示。我不断收到此错误:资源 ID #7 资源 ID #13

遇到 PHP 错误

严重性:通知

消息:数组到字符串的转换

文件名:views/blog_view.php

行号:12

<--- 数据库结构--->

CREATE TABLE `cities` (
`city` varchar(50) NOT NULL,
`state_code` char(2) NOT NULL,
 KEY `idx_state_code` (`state_code`)
 ) ENGINE=MyISAM;

<---现场控制器--->

<?php
class Site extends CI_Controller {

public function index()
{
    $this->load->library('pagination');
    $this->load->library('table');
    $config['base_url'] =     'http://localhost:8888/pagination/index.php/site/index/';
    $config['total_rows'] = $this->db->get('cities')->num_rows();
    $config['per_page'] = 20;

    $this->pagination->initialize($config);

    $data['records'] = $this->db->get('cities', $config['per_page'], $this->uri->segment(3));

    $this->load->view('data_view',$data);
 }
}
?>

<--- 数据视图 --->

<!DOCTYPE html>
<html>
<head>
<title>Display Database info</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>

<?foreach($records as $item):?>
    <?php echo $item; ?>
<?endforeach?>

<?php echo $this->pagination->create_links(); ?>

<!-- Enable responsive features in IE8 with Respond.js (https://github.com/scottjehl/Respond) -->
<script src="js/respond.js"></script>
</body>

4

2 回答 2

0

使用这个更新的代码,您必须调用 result() 方法并指定字段名称。

<?foreach($records->result() as $item):?>
    <?php echo $item->city; ?>
<?endforeach?>
于 2013-08-15T10:08:02.280 回答
0

您需要在该查询上调用 result() 。

public function index()
{
    $this->load->library('pagination');
    $this->load->library('table');
    $config['base_url'] =     'http://localhost:8888/pagination/index.php/site/index/';
    $config['total_rows'] = $this->db->get('cities')->num_rows();
    $config['per_page'] = 20;

    $this->pagination->initialize($config);

    $query = $this->db->get('cities', $config['per_page'], $this->uri->segment(3));
    $data['records'] = $query->result();

    $query->free_result();

    $this->load->view('data_view',$data);
}
于 2013-08-15T07:25:55.630 回答