title: Format data in generated csv file using php codeigniter
body: <p>I am generating an exam report csv file for my project but I wanted to customize the data that will be inserted on the exported csv file. How do I do that? I am using php Codeigniter for this. </p>
  
  <p>As of now, my code can generate a csv file containing the questions and the number of correct and wrong answers for that question. </p>
  
  <p>here is a picture of the downloaded file. <a href="https://i.stack.imgur.com/7SEzi.png" rel="nofollow noreferrer">Generated CSV File</a></p>
  
  <p>But I wanted it to look like this:</p>
  
  <p><a href="https://i.stack.imgur.com/p4qH1.png" rel="nofollow noreferrer">Wanted Result</a></p>
  
  <p>Here is my controller and model:
  Controller:</p>
  
  <pre><code>public function csv_report($exam_no){
      $this-&gt;load-&gt;model('instant_model');
      $median = $this-&gt;median($exam_no);
  
      $this-&gt;instant_model-&gt;exportToCSV($exam_no);
  }
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>public function exportToCSV($exam_no){
  
          $this-&gt;load-&gt;dbutil();
          $this-&gt;load-&gt;helper('file');
          $this-&gt;load-&gt;helper('download');
  
          $delimiter = ",";      
          $newline = "\r\n";
          $filename = "exam_report.csv";
          $query = " SELECT DISTINCT(questions.question_id), questions.question, 
              (SELECT count(score) FROM exam_set WHERE exam_no = '$exam_no' AND score &gt; 0 AND questions.question_id = exam_set.question_id) AS correct , 
              (SELECT count(score) FROM exam_set WHERE exam_no = '$exam_no' AND score = 0 AND questions.question_id = exam_set.question_id) AS wrong 
              FROM questions INNER JOIN exam_set ON (questions.question_id = exam_set.question_id) WHERE exam_no = '$exam_no';";
  
           $result = $this-&gt;db-&gt;query($query);
          $data = $this-&gt;dbutil-&gt;csv_from_result($result, $delimiter, $newline);
  
          force_download($filename, $data);
  
      } 
  </code></pre>