初学者,无法访问数组,codeigniter 2.x.

时间:2015-02-11 05:57:55

标签: php codeigniter codeigniter-2

我无法访问我的阵列。

我的网址如下:

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

**** NEW UPDATE ****

请注意,方法“function country_index2(...)”按原样工作,并将结果返回给控制器。控制器也清楚地将结果发送到视图。我遇到的问题是无法在视图中显示结果。只想清除它。

是的,有些线路正在讨论排序和分页,但这还没有实现,只是坐在那里什么都不做。很高兴听到。

4 个答案:

答案 0 :(得分:0)

print_r($results)是一个包含对象的数组。

你可以像这样循环:

foreach ($results['rows'] as $row) {

...然后像这样访问$row的属性:

$row->country

答案 1 :(得分:0)

因为在您的控制器中,您已经在$data数组中设置了结果,并向您传递了视图部分。

$data['query'] = $this->model_admin->search_countries($config['per_page'] , $offset, $sort_by, $sort_order);

如果您检查了模块,那么$query->get()->result();会为您提供数据对象而不是数组。

应该是:

<?php 
 if( is_array($query) && count( $query ) > 0  ) :
 foreach ($query['rows'] 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 } 
endif; ?>

答案 2 :(得分:0)

您可以保持代码不变。只需修复此功能

 function search_countries($limit, $offset, $sort_by, $sort_order){

    //your other codes here
    //$query.....
    //$results['rows'] = $query->get()->result();//replace it as bellow     
    $results = $query->get(); 
    return $results;  
}

<强>通知
此行在search_countries函数

中无效
$query_count = $this->db->select('COUNT(*) as count', FALSE)->from('mhcountry');

希望它能解决您当前的问题。

<强> Warining
请记住,您的分页不适用于您当前的设计

答案 3 :(得分:0)

有两个错误。第一个错误发生在search_countries()方法中;活动记录语法不正确。

在:

 $query = $this->db->select('country', 'country_id')
        ->from('mhcountry')
        ->limit($limit, $offset)
        ->order_by($sort_by, $sort_order);

后:

 $query = $this->db->select('country_id country')
        ->from('mhcountry')
        ->limit($limit, $offset)
        ->order_by($sort_by, $sort_order);

第二个错误发生在VIEW中。当我收到一个对象而不是一个数组时,视图循环应如下所示:

                                <tbody>

                                <?php

                                //var_dump($query);
                                foreach ($query as $row ) {
                                ?>
                                <tr>
                                    <td><?php echo $row->country_id; ?></td>
                                    <td><?php echo $row->country; ?></td>
                                    <td>something</td>
                                    <td>someone</td>
                                </tr>
                                <?php } ?>
                            </tbody>

感谢您的帮助。它现在正在运作。