CodeIgniter Forums

Full Version: multidimensional array in view
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi all,

I have the following array from my model, here the var_dump: 

Quote:array(4) {
 [0]=>
 array(1) {
   [0]=>
   array(5) {
     ["payment_student"]=>
     string(4) "2406"
     ["fee_month"]=>
     string(10) "2018-02-00"
     ["arrears"]=>
     string(1) "0"
     ["payment_description"]=>
     string(17) "Transport, 120 , "
     ["payment_amount"]=>
     string(3) "120"
   }
 }
 [1]=>
 array(1) {
   [0]=>
   array(5) {
     ["payment_student"]=>
     string(4) "2415"
     ["fee_month"]=>
     string(10) "2018-02-00"
     ["arrears"]=>
     string(1) "0"
     ["payment_description"]=>
     string(33) "Transport, 1500 , Tuition, 620 , "
     ["payment_amount"]=>
     string(4) "2120"
   }
 }
 [2]=>
 array(1) {
   [0]=>
   array(5) {
     ["payment_student"]=>
     string(4) "2175"
     ["fee_month"]=>
     string(10) "2018-02-00"
     ["arrears"]=>
     string(1) "0"
     ["payment_description"]=>
     string(17) "Transport, 620 , "
     ["payment_amount"]=>
     string(3) "620"
   }
 }
 [3]=>
 array(1) {
   [0]=>
   array(5) {
     ["payment_student"]=>
     string(4) "2087"
     ["fee_month"]=>
     string(10) "2018-02-00"
     ["arrears"]=>
     string(1) "0"
     ["payment_description"]=>
     string(33) "Transport, 1500 , Tuition, 420 , "
     ["payment_amount"]=>
     string(4) "1920"
   }
 }
}

here my view for loop:

PHP Code:
$i=0;
foreach (
$payments_view as $key => $data) { 

 
               ?>
                    <td><?php echo $i?></td>
                    <td><?php echo $data[$i]['payment_student']?></td>
                    <td><?php echo $data[$i]['arrears']?></td>

                    <td><?php echo $data[$i]['payment_description']; ?></td>
                    <td><?php echo $data[$i]['payment_amount']+$data[$i]['arrears']; ?></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
                <?php
                    $i
++;
 } 

It display the 1st row, but for next rows give following error: 

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 1
Filename: views/fee_register_list.php
Line Number: 95
Backtrace:
File: C:\xampp\htdocs\learn_ci\ksi_gentelella\application\views\fee_register_list.php
Line: 95
Function: _error_handler

File: C:\xampp\htdocs\learn_ci\ksi_gentelella\application\controllers\Fee_Controller.php
Line: 173
Function: view

File: C:\xampp\htdocs\learn_ci\ksi_gentelella\index.php
Line: 315
Function: require_once
The array from your model has 4 elements, which are arrays too.
Each of these 4 elements has only one element (index 0).
That's why it doesn't accept index 1.

Try this:
PHP Code:
foreach ($payments_view as $key => $data) {
 
 
  
<tr>
 
 <td><?= $data[0]['payment_student'];?></td>
  <td><?= $data[0]['arrears'];?></td>
  <td><?= $data[0]['payment_description'];?></td>
  <td><?= $data[0]['payment_amount']+$data[$i]['arrears'];?></td>
  </tr>



The foreach loop will let you display all rows. There's no need to use the $i variable as an index.
(02-05-2018, 11:30 AM)I need to display the multiple records from this array, your loop just return 0 index. Wouter60 Wrote: [ -> ]The array from your model has 4 elements, which are arrays too.
Each of these 4 elements has only one element (index 0).
That's why it doesn't accept index 1.

Try this:
PHP Code:
foreach ($payments_view as $key => $data) {
 
 
  
<tr>
 
 <td><?= $data[0]['payment_student'];?></td>
  <td><?= $data[0]['arrears'];?></td>
  <td><?= $data[0]['payment_description'];?></td>
  <td><?= $data[0]['payment_amount']+$data[$i]['arrears'];?></td>
  </tr>



The foreach loop will let you display all rows. There's no need to use the $i variable as an index.