Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can try something like this:</p> <pre><code>$stringindex = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // For the excel column cells $columns = str_split($stringindex); // create array elements $rowIndex = 5; // start at row 5 in Excel $columnIndex = 3; // start at column D in Excel $subjects = array(); while($sub_row = mysqli_fetch_array($resub, MYSQLI_ASSOC)) { $subject[] = $sub_row['name']; // Get the subjects from the database and put them in an array } $sheet = $objPHPExcel-&gt;setActiveSheetIndex(0); // PHPExcel functions foreach($subject as $index =&gt; $subjectName) { // GET SUBJECTS $columnName = getColumnName($columnIndex, $columns); $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension($columnName)-&gt;setAutoSize(true); // Sets Column Width $sheet-&gt;setCellValue($columnName.$rowIndex, $subjectName); // Lists Subjects as columns $columnIndex++; $columnName = getColumnName($columnIndex, $columns); $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension($columnName)-&gt;setAutoSize(true); // Sets Column Width $sheet-&gt;setCellValue($columnName.$rowIndex, "GRADE"); $columnIndex++; } // END of FOREACH LOOP function getColumnName($index, $columns) { $columnName = ""; $numColumns = count($columns); while($index &gt;= $numColumns) { $columnName = $columns[($index % $numColumns)] . $columnName; $index = floor($index / $numColumns) - 1; } $columnName = $columns[($index % $numColumns)] . $columnName; return $columnName; } </code></pre> <p>I added a function <em>getColumnName()</em> which gives you "AA", "AB", ... in the event that your subject and grade columns go past the A-Z limit.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload