【发布时间】:2015-02-11 05:57:55
【问题描述】:
我无法访问我的数组。
我有一个类似的网址:
http://localhost:8000/admin/country_index2/
Country2方法如下:
public function country_index2($sort_by = 'country_id', $sort_order='ASC', $offset = 0){
$this->load->model('model_admin');
$this->load->library('pagination');
/* This Application Must Be Used With BootStrap 3 * */
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$config['base_url'] = 'http://localhost:8000/admin/country_index';
$config['total_rows'] = $this->db->get('mhcountry')->num_rows();
$config['per_page'] = 10;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
// table, limit, offset
// $data['query'] = $this->db->get('mhcountry', $config['per_page'], $this->uri->segment(3));
$data['query'] = $this->model_admin->search_countries($config['per_page'] , $offset, $sort_by, $sort_order);
$data['templateVersion'] = 'template1';
$data['headerVersion'] = 'header1';
$data['navBarVersion'] = 'navbar1';
$data['main_content'] = 'detail-wrap/admin/country_index';
$data['page_title' ] = 'example.com - App';
$data['footerVersion'] = 'footer1';
// $data['num_rows'] = $this->db->get('mhcountry')->num_rows();
//if(empty($page_number)) $page_number = 1;
//$offset = ($page_number-1) * $config['per_page'];
//var_dump($data);
$this->load->view('detail-wrap/includes/template1', $data);
}
search_countries 函数内容如下:
function search_countries($limit, $offset, $sort_by, $sort_order){
// you can also do this with if then else
$sort_order = ($sort_order == 'DESC') ? 'DESC' : 'ASC';
$sort_columns = array('country_id', 'country');
$sort_by = (in_array($sort_by, $sort_columns)) ? $sort_by : 'country';
// results query
$query = $this->db->select('country', 'country_id')
->from('mhcountry')
->limit($limit, $offset)
->order_by($sort_by, $sort_order);
$results['rows'] = $query->get()->result();
$query_count = $this->db->select('COUNT(*) as count', FALSE)->from('mhcountry');
// $tmp = $query_count->get()->result();
//$results->[num_rows] =$tmp[0]->count;
return $results;
#print_r($results);
#die;
}
在我看来,我有这个:
<div class="bs-example">
Found
<?php
echo $num_rows ;
?>
Countries
<table class="table table-hover">
<thead>
<tr>
<th>Country ID</th>
<th>Country Name</th>
<th>Status</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<?php
//print_r(data[$query]);
//foreach ($query->result() as $row ) {
foreach ($query->result_array() as $row ) {
?>
<tr>
<td><?php echo $row['country_id']; ?></td>
<td><?php echo $row['country']; ?></td>
<td>Carter</td>
<td>johncarter@mail.com</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo $pagination; ?>
</div>
[剪辑]
当我转储 print_r($results) 时,我得到以下信息:
Array ( [rows] => Array ( [0] => stdClass Object ( [country] => Canada ) [1] => stdClass Object ( [country] => Japan ) [2] => stdClass Object ( [country] => Korea ) [3] => stdClass Object ( [country] => 0 ) [4] => stdClass Object ( [country] => pakist ) [5] => stdClass Object ( [country] => ddsdsd ) [6] => stdClass Object ( [country] => texas ) [7] => stdClass Object ( [country] => scotland ) [8] => stdClass Object ( [country] => scotland ) [9] => stdClass Object ( [country] => scotland ) ) )
但我似乎无法访问它们以在视图中显示它们。该视图未显示国家/地区。
[编辑] 我认为正在发生的事情是我正在传递一个多维数组并且只是难以访问数据。我在这里找到了一个类似的线程Accessing Multidimensional array with Codeigniter
**** 新更新 ****
请注意,“function country_index2(...)”方法按原样工作,并将结果返回给控制器。控制器也清楚地将结果发送到视图。我遇到的问题是无法在视图中显示结果。只是想澄清一下。
是的,有一些台词在谈论排序和分页,但这还没有实现,就像坐在那里什么都不做一样好。很高兴听到。
【问题讨论】:
-
如您所见,响应是一个对象。只需将您的 $row['country_id'] 替换为 $row->country_id
-
它不会工作你的分页。当你在第 2 页时,你会得到
$sort_by=2。 -
关于分页,这是另一个问题。我仍在尝试查看页面上的数据。
标签: php codeigniter codeigniter-2