【问题标题】:beginner, Cannot access array, codeigniter 2.x初学者,无法访问数组,codeigniter 2.x
【发布时间】: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


【解决方案1】:

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

你可以这样循环:

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

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

$row->country

【讨论】:

  • 我相信我的数据是由 $data['query'] 发送到我的视图的。在我看来,我已经运行 print_r($query);我可以看到我的数据如下: 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 ) 顺便说一句,我明白了 [country]但没有看到 [country_id]... 有什么意义吗?
  • 嗯,刚刚做了一个 var_dump($query) 并在下面收到。我看不到 [country_id] 虽然我认为数字在那里...奇怪.... array(1) { ["rows"]=> array(10) { [0]=> object(stdClass)#21 ( 1) { ["country"]=> string(6) "Canada" } [1]=> object(stdClass)#22 (1) { ["country"]=> string(5) "Japan" } [2 ]=> object(stdClass)#23 (1) { ["country"]=> string(5) "Korea" } [3]=> object(stdClass)#24 (1) { ["country"]=> string(1) "0" } [4]=> object(stdClass)#25 (1) { ["country"]=> string(6) "pakist" } [5]=> object(stdClass)#26 ( 1) { ["国家"]=> 字符串(6) "ddsdsd" }
  • 好吧,发现一个错误。我在这里的 country_id 和 country 顺序错误... $query = $this->db->select('country_id', 'country') ->from('mhcountry') ->limit($limit, $offset ) ->order_by($sort_by, $sort_order);但是仍然无法将数据输出到视图中。
  • 如果您可以在原始帖子末尾发布的输出中看到 country_id,您可以像上面的示例一样访问它,例如 $row-&gt;country_id(在 foreach 循环内)
【解决方案2】:

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

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

如果您检查了您的模块,那么$query-&gt;get()-&gt;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; ?>

【讨论】:

  • 谢谢。我尝试了上述方法,但出现以下错误:遇到 PHP 错误 严重性:警告消息:为 foreach() 提供的参数无效 文件名:admin/country_index.php 行号:41 第 41 行开始如下:foreach ($query[ 'rows'] as $row ) { ?> country_id; ?> country; ?> 卡特 johncarter@mail.com
  • @user3264461 你的控制器中有$this-&gt;load-&gt;view('detail-wrap/includes/template1', $data);吗?
  • 是的,这在原始帖子中显示。这是执行并确认工作。 $data['query'] = $this->model_admin->search_countries($config['per_page'] , $offset, $sort_by, $sort_order);除其他外,数据被返回给 this; $this->load->view('detail-wrap/includes/template1', $data);当我在视图中执行以下操作时: print_r($query);我确实看到了一个包含上面发布的数据(国家和 ID)的数组。很高兴听到。
  • user3264461 - 抱歉,“在我看来,如果我使用的话”是什么意思?
【解决方案3】:

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

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

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

警告
请记住,您的分页不会像您当前的设计那样工作

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 2010-09-15
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多