【发布时间】:2015-09-10 22:48:25
【问题描述】:
在 Codeigniter 中获取数据库结果时,result_array 方法返回行数据,但数组键不是行 ID。出了什么问题?
型号:
<?php
class Admin_user extends CI_Model
{
public function list_rows()
{
$query = array();
$query = $this->db->get('content');
return $query->result_array();
}
}
查看:
<?php
foreach ($get_all_content as $key => $values)
{
$title = $values['title'];
echo "<th>" . $key . "</th>";
}
控制器:
$data['get_all_content'] = $this->admin_user->list_rows();
print_r:
Array
(
[0] => Array
(
[id] => 1
[menu_id] => 12
[title] => Register Domain name for free
[sub_title] => This is the sub_title
[content] => this is the content description
[description] =>
[section] =>
)
)
【问题讨论】:
-
你
print_r($data['get_all_content'];吗?你从哪里得到你的print_r()? -
是的,它返回,我在视图 Array ( [0] => Array ( [id] => 1 [menu_id] => 12 [title] => 免费注册域名 [sub_title ] => 这是 sub_title [content] => 这是内容描述 [description] => [section] => ) )
-
但是当你这样做
$title = $values['title'];你得到了标题? -
是的!能够轻松获取标题值
-
你的数据的key是0。
标签: php codeigniter