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


beginner, Cannot access array, codeigniter 2.x

我无法访问我的数组。

我有一个类似的URL:

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 ) ) )

但我似乎无法访问它们以在视图中显示它们。该视图没有显示国家。

[编辑]我认为发生的情况是,我正在传递一个多维数组,并且访问数据时遇到了困难。我在这里发现了一个类似的线程使用Codeigniter 访问多维数组

****新更新***

请注意,方法"function country_index2(…)"按原样工作,并将结果返回给控制器。控制器还清楚地将结果发送到视图。我遇到的问题是无法在视图中显示结果。只是想澄清一下。

是的,有几行是关于排序和分页的,但这还没有实现,就像坐在那里无所事事一样好。很高兴听到。

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

你可以这样循环:

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

然后访问CCD_ 2的属性,如下所示:

$row->country

因为在控制器中,您已经在$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; ?>

您可以保持代码的原样。只需修复这个函数

 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
请记住,您的分页不会像您当前的设计一样工作

有两个错误。第一个错误出现在方法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>

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