CodeIgniter Forums
unable to display values from array in a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: unable to display values from array in a view (/showthread.php?tid=69450)

Pages: 1 2


unable to display values from array in a view - nadeem14375 - 11-25-2017

Hi all,

I am trying to out values using loop from an array, but facing error:

Quote:Message: Undefined index: payment_description

here my model:

PHP Code:
public function get_fee_invoice_4class($data){


 
   $this->anotherDb
    
->where('class_id',$data['id'])
 
   ->where('activated',1)
 
   ->where('role','Student');
 
   $student_query $this->anotherDb->get('users');
 
   
    $totalrows 
$student_query->num_rows(); 




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

 
       $this->anotherDb
        
->where('payment_student'$result[$i]['id'])
 
       ->where('date_format(fee_month,"%d-%m%Y") >=date_format(curdate(),"00-%m-%Y")')
 
       ->where('payment_status=0');
 
       $this->anotherDb
        
->select('payment_amount''payment_description','payment_student');
 
       $query $this->anotherDb->get('payments');
 
               
        $result_array
[] = $query->result_array();

 
       }
 
   return $result_array     
    


Here my controller:

PHP 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);
/*       
        echo '<pre>';
        print_r($this->data['fee_detail']);
        echo '</pre>';
        die(); 
*/
 
       $template_pdf $this->load->view('reports/rep_fee_invoice_4class_html'$this->dataTRUE);

 
       $html2pdf = new Html2Pdf();
 
       $html2pdf->writeHTML($template_pdf);
 
       $html2pdf->output('Student_Fee_invoice.pdf''D');
 
       

Here my view, I am using html2pdf for pdf outpu.

PHP Code:
<style type="text/Css">
<!--.
test1
{
 
   bordersolid 1px #FF0000;
 
   background#FFFFFF;
 
   border-collapsecollapse;
}
-->
</
style>
<
page style="font-size: 14px;">

<
div style="fload: left; width: 48%;">
 
   <span style="font-weight: bold; font-size: 18pt; color: #FF0000; font-family: Times">
 
   <h1 style="font-size: 24px; text-align: center;">
 
   Fee Invoice</h1>
 
   <br></span>
<?
php
    $school_name 
= ($this->Main_model->get_settings('school_name'));
 
   $school_address = ($this->Main_model->get_settings('address'));
 
   $phone_no = ($this->Main_model->get_settings('phone_no'));
?>
    <br>
    <p> School Name: <?php echo $school_name?></p>
    <p> School Address: <?php echo $school_address?></p>
    <p> Phone No: <?php echo $phone_no?></p>    
    <table style=" border: 1px solid #555555;">
        <thead>
                <tr>
                    <th style=" border: 1px solid #555555;">No.</th>
                    <th style=" border: 1px solid #555555;">Particulars</th>
                    <th style=" border: 1px solid #555555;">Amount</th>
                </tr>
        </thead>
        <tbody>
              <?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['payment_description']; ?></td>
                    <td style=" border: 1px solid #555555;"><?php echo $data['payment_amount']; ?></td>

                </tr>
                <?php
                    $i
++;
 
                     }
 
                   else:
 
               ?>
                <tr>
                    <td style=" border: solid 1px #000000 colspan="8" >No Records Found..</td>
                </tr>
                <?php
                    endif
;
 
               ?>
        </tbody>
                      <!--<td style="width: 100%">Test 1</td> </tr>-->
    </table><br />
    </div>
    </page> 

I tried 

/*      
       echo '<pre>';
       print_r($this->data['fee_detail']);
       echo '</pre>';
       die();
*/
and it return the following but unable to display these fields in view.


Quote:Array
(
   [0] => Array
       (
           [0] => Array
               (
                   [payment_amount] => 1800
               )

       )

   [1] => Array
       (
           [0] => Array
               (
                   [payment_amount] => 1800
               )

           [1] => Array
               (
                   [payment_amount] => 1800
               )

           [2] => Array
               (
                   [payment_amount] => 1800
               )

           [3] => Array
               (
                   [payment_amount] => 1800
               )

           [4] => Array
               (
                   [payment_amount] => 1800
               )

           [5] => Array
               (
                   [payment_amount] => 1800
               )

           [6] => Array
               (
                   [payment_amount] => 1800
               )

           [7] => Array
               (
                   [payment_amount] => 1800
               )

           [8] => Array
               (
                   [payment_amount] => 1800
               )

       )

   [2] => Array
       (
           [0] => Array
               (
                   [payment_amount] => 1800
               )

           [1] => Array
               (
                   [payment_amount] => 1800
               )

           [2] => Array
               (
                   [payment_amount] => 1800
               )

           [3] => Array
               (
                   [payment_amount] => 1800
               )

           [4] => Array
               (
                   [payment_amount] => 1800
               )

           [5] => Array
               (
                   [payment_amount] => 1800
               )

           [6] => Array
               (
                   [payment_amount] => 1800
               )

           [7] => Array
               (
                   [payment_amount] => 1800
               )

       )

)



RE: unable to display values from array in a view - PaulD - 11-25-2017

Your problem is that your array does not contain the index 'payment_description' so when you try to output it then php is saying it is not defined, which it is not. It seems you are selecting it though in your model, which is strange.

If I were in this situation, I would first output the query string itself and check it is doing what I expected. Use $this->db->last_query() as described in docs: https://www.codeigniter.com/user_guide/database/helpers.html

Then run the query that was outputted in your database directly and look at the results. Again, are they what you expected? ie do they have payment description field in them.

If you remove the line for payment description, is your code finding and displaying the payment_amount? If so you are doing nothing wrong in your output, just that the index is not defined for payment_description.

Hope that helps,

Paul.


RE: unable to display values from array in a view - nadeem14375 - 11-25-2017

(11-25-2017, 10:34 AM)PaulD Wrote: Your problem is that your array does not contain the index 'payment_description' so when you try to output it then php is saying it is not defined, which it is not. It seems you are selecting it though in your model, which is strange.

If I were in this situation, I would first output the query string itself and check it is doing what I expected.  Use $this->db->last_query() as described in docs: https://www.codeigniter.com/user_guide/database/helpers.html

Then run the query that was outputted in your database directly and look at the results. Again, are they what you expected? ie do they have payment description field in them.

If you remove the line for payment description, is your code finding and displaying the payment_amount? If so you are doing nothing wrong in your output, just that the index is not defined for payment_description.

Hope that helps,

Paul.


Hi Paul, 
Thanks for your reply.

$this->db->last_query() 

Return this query 
Quote:SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0


I tested the query in Mysql Workbench, it return data.

I tried only the payment_amount in my view, but still the same error.

Please check the last lines of my view are these correct?


RE: unable to display values from array in a view - PaulD - 11-25-2017

Your query is asking for:
PHP Code:
->select('payment_amount', 'payment_description','payment_student'); 

But your array is not showing those indexes. I do not understand why not.

You have a missing index 'payment_description'. In the error message, where is it indicating this is an issue, as if it does not exist in your database I am not certain how the query would run in the first place. Is it in the model, the controller or the view?

Perhaps you are fiddling with the code between pastes, but the query string you posted is different as it is not selecting 'payment_description', so where did that come from?

Paul.


RE: unable to display values from array in a view - nadeem14375 - 11-25-2017

(11-25-2017, 11:56 AM)PaulD Wrote: Your query is asking for:
PHP Code:
->select('payment_amount', 'payment_description','payment_student'); 

But your array is not showing those indexes. I do not understand why not.

You have a missing index 'payment_description'. In the error message, where is it indicating this is an issue, as if it does not exist in your database I am not certain how the query would run in the first place. Is it in the model, the controller or the view?

Perhaps you are fiddling with the code between pastes, but the query string you posted is different as it is not selecting 'payment_description', so where did that come from?

Paul.

Whats the problem with query in this model?

PHP Code:
       $this->anotherDb
        
->where('payment_student'$result[$i]['id'])
 
       ->where('date_format(fee_month,"%d-%m%Y") >=date_format(curdate(),"00-%m-%Y")')
 
       ->where('payment_status=0');
 
       $this->anotherDb
        
->select('payment_amount''payment_description','payment_student');
 
       $query $this->anotherDb->get('payments'); 

echo $this->anotherDb->last_query();

returns the following, means the about ->select has problem, here is only payment_student.

PHP Code:
SELECT `payment_amountFROM `paymentsWHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =
 
I can execute the following in query in mysqlworkbench.
PHP Code:
SELECT payment_descriptionpayment_studentpayment_amount FROM payments



RE: unable to display values from array in a view - PaulD - 11-25-2017

IGNORE THIS:
(Your query is in a for loop so you do not want a single query as written here)

Try replacing your query with this:
PHP Code:
return $this->anotherDb
    
->select('payment_amount', 'payment_description','payment_student')
    ->
where('payment_student', $result[$i]['id'])
    ->
where('date_format(fee_month,"%d-%m%Y") >=date_format(curdate(),"00-%m-%Y")')
    ->
where('payment_status'0)
    ->
get('payments')
    ->
result_array(); 

You also do this
PHP Code:
foreach ($fee_detail as $key => $data) 
But you do not use $key, so just do this:
PHP Code:
foreach ($fee_detail as $data) 

Does that make any difference?

PS I really do not like your for loop to get each student id. Just nest two foreaches, would be cleaner to debug.


RE: unable to display values from array in a view - nadeem14375 - 11-25-2017

no luck

I tried both

foreach ($fee_detail as $key => $data) {

and

foreach ($fee_detail as $data) {

tried to pick the last_query, and the result is strange:
echo $this->anotherDb->last_query();
die();

SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0


RE: unable to display values from array in a view - PaulD - 11-25-2017

Try something like this:
PHP Code:
public function get_fee_invoice_4class($data)
{
    
// set return variable
    
$result = array();
    
    
// get students
    $students = $this->anotherDb->from('users')
        ->where('class_id'$data['id'])
        ->where('activated'1)
        ->where('role''Student')
        ->get()
        ->result_array();

    
// get payment details
    if ( ! empty($students) )
    {
        foreach (
$students as $student)
        {
            $result[] = $this->anotherDb->from('payments')
                ->select('payment_amount','payment_description','payment_student')
                ->where('payment_student', $student['id'])
                ->where('date_format(fee_month,"%d-%m%Y") >=date_format(curdate(),"00-%m-%Y")')
                ->where('payment_status'0)
                ->get()
                ->result_array();
        }
    }

    return $result;      
}  

Should work, however this is BAD, because imagine you have 1000 students, then you will have 1000 database calls because of running a query in a loop. I think what you really need is a more complex query with a join from students to students payments to get all the data in one query.


RE: unable to display values from array in a view - nadeem14375 - 11-25-2017

here is the output pdf attached.


RE: unable to display values from array in a view - PaulD - 11-25-2017

(11-25-2017, 01:44 PM)nadeem14375 Wrote: SELECT `payment_amount` FROM `payments` WHERE `payment_student` = '44' AND date_format(fee_month,"%d-%m%Y") >= date_format(curdate(),"00-%m-%Y") AND `payment_status` =0

What result do you get without all that strange date stuff that I am unfamiliar with it. This looks wrong too "%d-%m%Y" should it be "%d-%m-%Y" (notice the additional hyphen)