1

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);
}

4 Answers 4

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;

Comments

0

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>
}

3 Comments

please give full error. So I can understand properly
I just want to know what is undefined..? I mean which variable is undefined.?
Undefined Index:am <?php echo $paid['am'] ?> and so on
0

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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.