doulongti5932 2016-10-11 10:06
浏览 48
已采纳

在codeigniter控制器中创建一个多维数组

I'm having a controller to get users from DB and display every user in a table row

Controller: [update] //only first page displays data correctly but pager +1 using 
pagination displays user data only not getting orders as first page
when I print_r($orders) in all pages I get correct orders but in the table the inner 
foreach doesn't work

public function index($page_id = 1)
{
    $perPage = 25;
    $offset = ($page_id - 1) * $perPage;

    if ($offset < 0) {
        $offset = $perPage;
    }
    $lang_id = $this->data['active_language']->id;
    $config['base_url'] = base_url() . "reports/myController/index/";
    $config['per_page'] = $perPage;
    $config['first_link'] = FALSE;
    $config['last_link'] = FALSE;
    $config['uri_segment'] = 4;
    $config['use_page_numbers'] = TRUE;
    $config['first_link'] = lang('first_page');
    $config['last_link'] = lang('last_page');
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['prev_tag_open'] = '<li>';
    $config['prev_tag_close'] = '</li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li><strong>';
    $config['cur_tag_close'] = '</strong></li>';

    $config['display_pages'] = TRUE;

    $orders_serials_ids = $this->myModel->get_orders_serials_ids();

    $config['total_rows'] = $this->myModel->count_orders();

    $this->pagination->initialize($config);

    $users = $this->myModel->users_merchants($perPage, $offset);
    $this->data['users'] = $users;

    $user_orders = array();
    foreach ($users as $user){
        $user_orders[$user->user_id] = $this->myModel->get_orders($user->user_id);
    }

View:
<table>
    <tr>
      <th>
        <?php echo 'user';?>
      </th>
      <th>
        <?php echo 'email';?>
      </th>            
      <th>
        <?php echo 'device id';?>
      </th>
    </tr>
    <?php
    foreach($users as $val){
    ?>
    <tr>
      <td>
        <?=$val->user_id?>
      </td>
      <td>
        <?php echo $val->email;?>
       </td>
       <td>
        <?php echo $val->device_id;?>
       </td>
    </tr>
    <?php
    $order = array(); //empty the variable before each loop
    foreach($orders[$user->user_id] as $order){
        echo '<tr><td colspan="4">'. date('Y-m',$order->unix_time).
' -> '.round($order->amount2,2).'</td></tr>';                   
    }
}
?>
</table>
<ul class="pagination"><?php if($pagination) echo $pagination;?></ul>

user id    user name        user email
1          Mike             mike@yahoo.com
order:10   order total:50   order date: 2016-09-12
order:12   order total:100  order date: 2016-09-14
2          Tom              Tom@live.com
order:15   order total:80   order date: 2016-09-13
order:16   order total:120  order date: 2016-09-14
order:17   order total:140  order date: 2016-10-10      

This works in the first page only but when I navigate to second page the controller url changes to index/2 r any other page number I get users rows without data orders though if I print_r orders above the table I get correct orders

  • 写回答

1条回答 默认 最新

  • dongnanbi4942 2016-10-11 12:40
    关注

    you can reduce the code a little but in controller like this:

    $this->data['users'] = $users; // correct data no problem here
    $user_orders = array();
    foreach ($users as $user){
       $user_orders[$user->user_id] = $this->myMmodel>get_orders($user->user_id); //make user_id as key so that you can access easily for each user 
    }
    
    $this->data['orders'] = $user_orders;
    

    Then in view, you can access orders of each user inside the foreach loop like this:

    $orders[$user->user_id]

    as it is also an array, you have to run loop to create order rows for each user:

    $order = array(); //empty the variable before each loop
    foreach($orders[$user->user_id] as $order){
       echo '<tr><td>'.$order->id.'</td>'.....//more columns like this
    }
    

    Hope this would help!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B