2016-10-11 137 views
0

我有一個控制器從數據庫獲取用戶,並在錶行創建笨控制器多維數組

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    m[email protected] 
order:10 order total:50 order date: 2016-09-12 
order:12 order total:100 order date: 2016-09-14 
2   Tom    [email protected] 
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  

這工作在第一頁只,但會顯示每一個用戶,當我瀏覽到第二頁控制器的URL更改爲索引/ 2任何其他頁碼我得到的用戶行沒有數據的訂單,但如果我print_r訂單上面的表我得到正確的訂單

回答

1

你可以減少代碼一點點,但在控制器像這樣:

$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; 

然後,在視圖中,可以訪問命令的每個用戶的foreach循環內是這樣的:

$訂單[$用戶> USER_ID]

,因爲它也是一個數組,就必須運行環爲每個用戶創建訂單行:

$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 
} 

希望這會有所幫助!

+0

只有當我使用分頁導航到第2頁時,第一頁才能正常工作,但我僅在沒有獲取訂單的情況下獲得用戶行。如果我在表格前獲得print_r($ orders),我會在第二頁中獲得正確的用戶訂單,但不會爲每個用戶打印訂單行。 URL更改爲索引/ 2我不知道原因 –

+0

我不知道不在其他頁面打印訂單的原因,只是打印在第一頁 –

+0

我不確定也是爲什麼。如果問題仍未解決,您可以分享確切的代碼。 –