Usman
Usman

Reputation: 13

How can I return multiple rows from the model same function in codeigniter?

This code print only one row, not print all data

Model

public function schbysempaid($batch, $sem){
        $query=$this->db->query("SELECT * FROM stu WHERE curem=$sem AND baid=$batch");
        foreach ($query->result_array() as $row) {
            $querys=$this->db->query("SELECT SUM(install.adfee) as am,SUM(install.lifee) as lb,SUM(install.tfee) as tt,SUM(install.enrfee) as enr,SUM(install.refee) as re,SUM(install.spofee) as spo,SUM(install.exfee) as ex FROM stu JOIN install ON install.sid=stu.sid Where install.sid='".$row['sid']."' AND install.curem=$sem AND stu.curem=$sem AND stu.baid=$batch AND install.paid=1");
        return $querys->result_array();
        }
    }

Controller

public function searchsubmit()
{
        $batch=$this->input->post('srch');
        $sem=$this->input->post('srch1');
        $data['searchpaid']=$this->law_model->schbysempaid($batch, $sem);
        $this->load->view('admission/dashboard',$data);
}

Upvotes: 1

Views: 2301

Answers (4)

Keith Asilom
Keith Asilom

Reputation: 179

You are getting only one row because in your model your return statement is inside the loop, so after the first iteration the data is returned. Try placing the return statement after the loop.

public function schbysempaid($batch, $sem){
    $query=$this->db->query("SELECT * FROM stu WHERE curem=$sem AND baid=$batch");
    $temp_arr = array();
    foreach ($query->result_array() as $row) {
        $querys=$this->db->query("SELECT SUM(install.adfee) as am,SUM(install.lifee) as lb,SUM(install.tfee) as tt,SUM(install.enrfee) as enr,SUM(install.refee) as re,SUM(install.spofee) as spo,SUM(install.exfee) as ex FROM stu JOIN install ON install.sid=stu.sid Where install.sid='".$row['sid']."' AND install.curem=$sem AND stu.curem=$sem AND stu.baid=$batch AND install.paid=1");
        array_push($temp_arr , $querys->result_array());
    }
    return $temp_arr;
}

Upvotes: 0

Gyandeep Sharma
Gyandeep Sharma

Reputation: 2327

Model

public function schbysempaid($batch, $sem){
$result = array();
        $query=$this->db->query("SELECT * FROM stu WHERE curem=$sem AND baid=$batch");
        foreach ($query->result_array() as $row) {
            $querys=$this->db->query("SELECT SUM(install.adfee) as am,SUM(install.lifee) as lb,SUM(install.tfee) as tt,SUM(install.enrfee) as enr,SUM(install.refee) as re,SUM(install.spofee) as spo,SUM(install.exfee) as ex FROM stu JOIN install ON install.sid=stu.sid Where install.sid='".$row['sid']."' AND install.curem=$sem AND stu.curem=$sem AND stu.baid=$batch AND install.paid=1");
        $result[] = $querys->result_array();
}

return $result;
    }

Controller

    public function searchsubmit()
{
        $batch=$this->input->post('srch');
        $sem=$this->input->post('srch1');
        $data['result']=$this->law_model->schbysempaid($batch, $sem);
        $this->load->view('admission/dashboard',$data);
}

View

foreach($result as $paid){
<td><?php echo $paid['am'] ?></td>
                          <td><?php echo $paid['lb'] ?></td>
                          <td><?php echo $paid['tt'] ?></td>
                          <td><?php echo $paid['spo'] ?></td>
                          <td><?php echo $paid['ex'] ?></td>
                          <td><?php echo $paid['enr'] ?></td>
                          <td><?php echo $paid['re'] ?></td>
}

Upvotes: 0

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Change the following line:

return $querys->result_array();

to

$response[] = $querys->result_array();

Explanation: You are using $querys->result_array(); inside a loop, in that case for the first iteration it return the result and skip the next iterations. So hold it in some array and return it like:

return $response;

Upvotes: 1

Rahul
Rahul

Reputation: 18557

You are returning result in foreach loop, instead you could save that data in array and after end of loop you can return it. check below

function schbysempaid($batch, $sem)
{
    $query = $this->db->query("SELECT * FROM stu WHERE curem=$sem AND baid=$batch");
    $result = $query->result_array();
    $retArr = [];
    foreach ($result as $row) {
        $querys = $this->db->query("SELECT SUM(install.adfee) as am,SUM(install.lifee) as lb,SUM(install.tfee) as tt,SUM(install.enrfee) as enr,SUM(install.refee) as re,SUM(install.spofee) as spo,SUM(install.exfee) as ex FROM stu JOIN install ON install.sid=stu.sid Where install.sid='" . $row['sid'] . "' AND install.curem=$sem AND stu.curem=$sem AND stu.baid=$batch AND install.paid=1");
        $retArr[] = $querys->result_array();
    }
    return $retArr; // I am returning your data here
}

Upvotes: 1

Related Questions