Writing multiple rows from query to specific/random columns not in order

Topics: Developer Forum, User Forum
Aug 15, 2014 at 3:13 AM
Edited Aug 15, 2014 at 3:25 AM
I am creating a report from a template. In order for the report to look half way decent I am having to spread the columns out randomly, merge cells, etc. The following code will insert the results in normal order but what I need to be able to do is insert the results into specific columns (A20, C20, then E20, G20, H20, J20, and L20) and have all the results repeat this in the next rows. Any ideas? I am stumped for some reason. I know the class isn't really necessary for what I am doing here...but I will be adding some things to it that will make it useful. Just haven't gotten that far. Let me know if this doesn't make sense O_O
<?php

try {
    $handler = new PDO('mysql:host=localhost;dbname=mydb', 'root', '$$$$');
    $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo $e->getMessage();
        die();
    }
//I know this class doesn't seem necessary but I will be adding things that will make it useful soon
class QueryClass {
    public $name, $age, $rights, $amount, $association, $status, $owner;
}

require '../PHPExcel/Classes/PHPExcel.php'; 

$id = $_POST["exportID"];

$query = $handler->prepare("SELECT name, age, amount, association, rights, status, owner FROM ownership WHERE ID = :id");
$query->execute(array('id'=>$id));

$query->setFetchMode(PDO::FETCH_CLASS, 'QueryClass');

// Read the template file
$inputFileType = 'Excel5';
$inputFileName = 'ReportTemplateXLS.xls';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);

//insert into sheet
$rowNumber = 20; 
while ($r = $query->fetch()) { 
   $col = 'A'; 
   foreach($r as $cell) { 
      $objPHPExcel->getActiveSheet()->setCellValue($col.$rowNumber,$cell); 
      $col++; 
   } 
   $rowNumber++; 
}

    // Save as an Excel(xls) file 
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); 

     header('Content-Type: application/vnd.ms-excel');
     header('Content-Disposition: attachment;filename="Report.xls"');
     header('Cache-Control: max-age=0'); 

   $objWriter->save('php://output'); 
   exit(); 
  
?>