Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd array elements between each of the existing array elements
    primarykey
    data
    text
    <p>I am trying to create a report in Excel by fetching data from MySql. I need to get subject headings from the database and output them to the excel sheet as columns e.g cell: D5, E5, F5 ... M5 etc.</p> <p>So far I have managed to do so, therefore the display under the cells holds headings something like:</p> <p>D5:Mathematics, E5:English, F5:Physics, etc.</p> <pre><code> | A | B | C | D | E | F | G | H | -------------------------------------------------------------- 1 | | | | | | | | | ... 5 | | | | Mathematics | English | Physics | ... | ... | </code></pre> <p>which is fine. The challenge is, I need to insert a <code>GRADE</code> heading in between each of the <code>Subject</code>s, something like this:</p> <pre><code> | A | B | C | D | E | F | G | H | I | J | K | -------------------------------------------------------------------------------------- 1 | | | | | | | | | | | | ... 5 | | | | Mathematics | GRADE | English | GRADE | Physics | GRADE | ... | ... | </code></pre> <p>Now here is what I have so far:</p> <pre><code>$stringindex = "DEFGHIJKLMNOPQRSTUVWXYZ"; // For the excel column cells $arr_index = str_split($stringindex); // create array elements $arr_val2 = array(); // create array $subject = array(); $subname2 = 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 } foreach($subject as $sub =&gt; $subname) { $subkey[] = $sub; // array of subject keys e.g 0|1|2|3 $subname2[] = $subname; // array of subjects } foreach($arr_index as $arr =&gt; $arr_val) { $arr_val2[] = $arr_val; // array of letters e.g D|E|F|G } $acount = count($subname2); $bcount = count($arr_val2); $size = ($acount &gt; $bcount) ? $bcount : $acount; $a = array_slice($subname2, 0, $size); $b = array_slice($arr_val2, 0, $size); $combine = array_combine($a, $b); $sheet = $objPHPExcel-&gt;setActiveSheetIndex(0); // PHPExcel functions foreach($combine as $key =&gt; $val) { // GET SUBJECTS $objPHPExcel-&gt;getActiveSheet()-&gt;getColumnDimension($val)-&gt;setAutoSize(true); // Sets Column Width $sheet -&gt;setCellValue($val.'5', $key); // Lists Subjects as columns } // END of FOREACH LOOP </code></pre> <p>The above code is able to display the subjects as column headings in excel:</p> <p>QUESTION: How do I add a code to add a GRADE column after each subject heading?</p> <p>I hope you'll point me in the right direction coz' I am now stuck.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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