CodeIgniter Forums

Full Version: model function generating weird query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Ignis Avis[/eluser]
Following is the model function I use to get late fee from the database
Code:
function get_late_fee_by_id($id) {

        $month = date("F, Y", time());
        $this->db->select('mwf_late_fee,total_due');
        $this->db->from('mwf');
        $this->db->where('mwf_student_id',$id);
        $this->db->where('mwf_month',$month);

    }

In my controller i am using following code to call it.
Code:
$data['late_info'] = $this->transaction_model->get_late_fee_by_id($this->uri->segment(3));

when passing to the view it generates following error
Code:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/invoice.php
Line Number: 95

and in view this is the code

Code:
<?php foreach($late_info as $late) { ?>
                <tr>
                    <td></td>
                    <td>Late Fee</td>
                    &lt;?php echo '<td class="right"><strong>'. $late['mwf_late_fee'].'</strong></td>'; ?&gt;
                </tr>

So i dumped the $data[late_info] which is null. so i wanted to see what query was executing.
I found the query and ran it in mysql it generates perfect row which I inteded to generate.

but for model function above, following query is generated. one extra parameter. I have no idea from where it is getting it.
Code:
SELECT `mwf_late_fee`, `total_due`, `grade` FROM (`mwf`, `student_info`) WHERE `1mwf_student_id` = 'MCS20145B53' AND `mwf_month` = 'September, 2014' AND `student_gen_id` = 'MCS20145B53'

Its weird! to me. so rest of my controller function look like

Code:
public function generate_invoice() {

        $data['fin_info'] = $this->student_model->get_financial_info($this->uri->segment(3));
        $data['stu_info'] = $this->student_model->get_student_info($this->uri->segment(3));
        $data['late_info'] = $this->transaction_model->get_late_fee_by_id($this->uri->segment(3));
        $dt_id = $this->student_model->get_grade_by_id($this->uri->segment(3));
        $data['grade_info'] = $this->transaction_model->get_grade_info_by_id($dt_id[0]['grade']);
        //load the view
        $data['content'] = 'invoice';    
        $this->load->view('layout', $data);
    }

So i'm kind of stuck. Please give me some guidelines.

El Forum

[eluser]InsiteFX[/eluser]
For one you are not getting the data with $this->db->get('table_name'); and two you need to return the query result back to your controller all of this is in your function get_late_fee_by_id($id)

Read the CodeIgniter Users Guide on the Database and Active Record