dev dev
dev dev

Reputation: 123

retrieve data from database in for loop

my controller is

function shiftstudentdetailsmultiple(){
    $data['Show'] = $this->input->post('show[]');
    $roll = $this->input->post('roll[]');
    //echo sizeof($data1);
    for($i=0;$i<sizeof($roll);$i++)
    {

        $data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]); 
    }
    $this->load->view('registrar/shift_student_view', $data);

}

and model is

function getstudentsdetailscouseandmultiple($Uniq_Id){

    $this->db->select("sprd.Uniq_Id, sprd.Name, sprd.Uni_Roll_No, cd.Course_Name, bd.Branch_Name, sprd.Year, sprd.Session, sprd.Section, sprd.Fee_Status, sprd.Curr_Status");
    $this->db->where("sprd.Uniq_Id",$Uniq_Id);
    $this->db->from("Students_Prim_Detals sprd");
    $this->db->join('Course_Details cd', 'cd.Course_Id=sprd.Course_id');
    $this->db->join('Branch_Details bd', 'bd.Branch_Id=sprd.Branch_id');

    $query = $this->db->get();
    if ($query->num_rows() > 0){
        return $query->result();
    }
    else {
        return false;
    }


}

when showing in view the fetching data showing only one time.. but in my database i have more then 4 entry when i print_r

                     $data['results']=this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);

the data showing correct .. when load only showing one data..the last entry..

 $this->load->view('registrar/shift_student_view', $data);

Upvotes: 0

Views: 130

Answers (1)

DFriend
DFriend

Reputation: 8964

Each time you call...

$data['results'] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]);

you replace the value stored in $data['results'] with the new return. So only one return is sent to the view.

You will need to revise your controller along these lines.

$student_detail = array();
for($i=0;$i<sizeof($roll);$i++)
{    
  $student_detail[] = $this->FetchData->getstudentsdetailscouseandmultiple($roll[$i]); 
}
$data['results'] = $student_detail;

Then in your view you will have to loop through $results to display each item in the array.

foreach($results as $detail){
  echo "Name: ". $detail->name;
  echo "Course: ". $detail->Course_Name;
  //etc...
}

Upvotes: 1

Related Questions