I have done ample research and got hopeless with PHPExcel output. When I use PHPexcel to list out the result, I get the output, but not always, in codeigniter. How do I rectify this issue? I am not very proficient with the Excel sheet coding. Here is my query:
 public function excel_customer_all($date, $fdate)
{
  if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] === true && $_SESSION['role'] && $_SESSION['role_des'])
    {
    $this->load->library('excel');
    $styleArray = array(
    'font'  => array(
    'bold'  => true,
    'color' => array('rgb' => '#000000'),
    'size'  => 10,
    'name'  => 'Verdana'
    ));
    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle('All Customer Details');
    .....
    SETTING CELL VALUES HERE
    .....
            //set cell A1 content with some text
    ................just ignoring these parts to shorten the code.........
            //retrive contries table data
     $rs=$this->Customer_model->list_all_customer_date($date, $fdate);
    $row = 4; // 1-based index
    $col = 0;
    $no = 1;    
    foreach($rs as $key=>$value) {
                   echo $row . ", ". $col . "<br>";
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row,$no);
                    $col++;
                     $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_main_ph);
                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_name);
                    $col++;$col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_add);
                    $col++; $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_email);
                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->cus_main_location);
                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->category);
                    $col++;
                   if($value->used_devices&&$value->used_device_count){
        $z=array();
         $a=explode(',',$value->used_devices);
                     $b=explode(',',$value->used_device_count);
        foreach($a as $index=>$val){
           if($b[$index]=!0)
           {
           array_push($z,$a[$index].'-'.$b[$index]);
           }
        }
        $y='';
         $y=implode("  , ",$z);
        }
        if($value->other_device&&$value->other_device_count){
        $f=array();
         $c=explode(',',$value->other_device);
                     $d=explode(',',$value->other_device_count);
        foreach($c as $index=>$val){
           if($d[$index]=!0&&$c[$index])
           {
           array_push($f,$c[$index].'-'.$d[$index]);
           }
        }
         $g=implode("  , ",$f); 
        } 
        if($y || $g)
        {
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $y.' , '.$g);
        }
                    $col++;
                    $this->excel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value->follow_up_dt);
                    $col++;
                 $row++;$col++;
                    echo $row . ", ". $col . "<br>";
                    $col = 0;
 $no++;
}
            $this->excel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $this->excel->getActiveSheet()->getStyle('B4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $this->excel->getActiveSheet()->getStyle('C4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
          $filename=mt_rand(1,100000).'.xls'; //just some random filename
          ob_clean();
    ob_start(); 
    header('Content-type: application/vnd.ms-excel; charset=UTF-8' ); 
    //header('Content-Type: application/xlsx');
    header('Content-Disposition: attachment;filename="'.$filename.'"');
    header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($this->excel,'Excel5');
        $objWriter->save('php://output');
        }  else
    {
        redirect('Admin/index', 'refresh');
      }
}
When I tested the code with multiple selected data, I found that the issue is somewhere near the imploding part in the above function. Sometimes, the error shows "Undefined variable $y" and sometimes it shows "headers already sent " warning message with some numbers, for example: see the image below:
What should be done in order to get output always irrespective of the type of data present in the database.

 
     
    