0

I have to run a pairing algorithm for a game and when the pairing is done, I display the pairing on HTML and create a csv file as well. Right now, once I am done with pairing, I create a multidimensional array to store the specific value and then pass it to the function in same php file to generate the csv file. However, doing this outputs the entire page code i.e. html and php code to the .csv file. Here is the code:

   function performPairing()
   {
       .... 
      $count=0;
      $resultArray[][] = array();
      while ($currrow = @mysql_fetch_row($result))
      {
         $playerone = $currrow;
         $playertwo = @mysql_fetch_row($result);
         $resultArray[$count][] = $playerone[1];
         $resultArray[$count][] = $playerone[0];
         $resultArray[$count][] = $playertwo[1];
         $resultArray[$count][] = $playertwo[0];
         $count++;
         updateforeachrow($playerone, $playertwo);
      }
      generateDocument($resultArray, $count);
   }

    function generateDocument($resultArray, $count)
    {
        $output = fopen('php://temp/maxmemory'.(5*1024*1024), 'r+');
        $columns = array('Player One Col1', 'Player One Col2', 'Player Two Col1', 'Player Two Col2');
        fputcsv($output, $columns);
         for ($index=0 ; $index <=$count; $index++)
         {
            fputcsv($output, $resultArray[$index]);
         }
        rewind($output);
        $export = stream_get_contents($output);
        fclose($output);
        header('Content-type: application/octet-stream');
        header('Content-Disposition: attachment; filename = "export.csv"');
        echo $export;  
} 

However doing this outputs the entire html code to csv rather than specific rows. Can anyone please help me on this?

1 Answer 1

1

. make string . output string to file . send header with (is this allowed this way?)

use file_put_contents ( filename, str ), and than send it with headers. Make code simpler :)

Sign up to request clarification or add additional context in comments.

1 Comment

Did not get you. Can you please explain me what you mean through my code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.