Welcome Guest, Not a member yet? Register   Sign In
This page isn’t working using while loop.
#1
Rainbow 

hi all,

I have a strange problem in my application. unable to loop through an array. 

here my code:

Controller:

Code:
  public function rep_fee_invoice_4class()
   {
       
       $data = array(  
                   //'fee_type'    => $this->input->post('fee_type'),
                   'id'     => $this->input->post('class')
                   );

       $this->data['fee_detail']= $this->Fee_model->get_fee_invoice_4class($data);
       
       $this->load->view('reports/rep_fee_invoice_4class_html', $this->data , TRUE); 
   }    

Model:
Code:
  public function get_fee_invoice_4class($data){

   $class_condition = "WHERE studentclass=". $data['id'];

   $student_query=$this->anotherDb->query("SELECT * from
       users $class_condition
       AND activated =1
       AND role = 'Student'");


$totalrows = $student_query->num_rows();

               for($i=0;$i<$totalrows;$i++) {
                             
           $result = $student_query->result_array();
           $student_id = $result[$i]['id'];

                       $student_condition = "WHERE paymentStudent=". $student_id;
                       $query=$this->anotherDb->query("
                           SELECT paymentStudent, feeMonth, paymentDescription, paymentamount
                           FROM payments
                           $student_condition
                           AND date_format(feeMonth, '%d-%m-%Y') >= date_format(curdate(), '00-%m-%Y')
                           AND PAYMENTSTATUS =0");
                   }
       return $query->result_array();
   }

View:

Code:
<?php

do {

       ?> <table>
       <?php
               if(isset($fee_detail) && is_array($fee_detail) && count($fee_detail)): $i=1;
               foreach ($fee_detail as $key => $data) {
               ?>
               <tr>
                   <td style=" border: 1px solid #555555;"><?php echo $i; ?></td>
                   <td style=" border: 1px solid #555555;"><?php echo $data['paymentStudent']; ?></td>
                   <td style=" border: 1px solid #555555;"><?php echo $data['paymentDescription']; ?></td>
                   <td style=" border: 1px solid #555555;"><?php echo $data['paymentamount']; ?></td>
               </tr>
               <?php
                $i++;
                   
                     }
                   else:
               ?>
               <tr>
                   <td style=" border: solid 1px #000000 colspan="8" >No Records Found..</td>
               </tr>
               <?php
                   endif;

               ?>
       </tbody>
</table>
<?php

}

while ($fee_detail);

?>
Reply
#2

I think the problem is in your model.
It has a for (... )  loop, to iterate through the students that where found in the query with the class_condition. But in the end, your model only returns the result for the very last iteration.

A good way to detect what your model is returning, is to put this code in your controller after you called the model:
PHP Code:
echo '<pre>';
print_r($data);
echo 
'</pre>';
die(); 

Take a good look at the output.

By the way, running queries in CI can be done so much easier than what you are doing. Let me give you an example:
PHP Code:
$this->db
->where('studentclass',$data['id'])
->
where('activated',1)
->
where('role','Student');
$student_query $this->db->get('users');
$totalrows $student_query->num_rows(); 

If you understand that, you can try to let your model get the payment results in one single query, using the JOIN statement:
PHP Code:
$this->db
->select('s.*,p.feeMonth,p.paymentDescription,p.paymentamount')
->
from('users s')
->
join('payments p','s.id = p.paymentStudent')
->
where('s.activated',1)
->
where('s.role','Student')
->
where('p.feeMonth >='Date('Y-m') .'-01')
->
where('p.PAYMENTSTATUS',0)
->
order_by('s.id');
$query $this->db->get();
if (
$query->num_rows() == 0) return array();
return 
$query->result_array(); 

This query will return an array of records. Each record will have all the fields form the users table, and some fields from the payments table. If no records match the conditions, it will return an empty array.

Good luck.
Reply
#3

Hi Wouter60 ,

Thanks for your reply.

Can you tell me where and how my model iterate for all student in a class and return the last result?

I want to return all the records by unable to do so.

I considered your query writing and will convert all these to that, I new so, following the code where I found here and there.
Reply
#4

Thanks dear,

Found the mistake, I needed to store the values in an array with in the for loop and then out of the loop return that array.

{
for () {

...
...
...

$result_array[] = $query->result_array(); //$this->db->get()->result()

}
return $result_array;
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB