Jump to content

PHP to excel problem


nerd99

Recommended Posts

Hi,

 

I am attempting to export some table data to an excel spreadsheet. Below is the code I am working with.

 

<?php 

include('includes/admin_session.php');

require_once("includes/connection.php"); 

function cleanData(&$str) {

  $str = preg_replace("/\t/", "\\t", $str);
  $str = preg_replace("/\r?\n/", "\\n", $str);

  if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

}
// file name for download
$filename = "student_list_" . date('Ymd') . ".xls";

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

$flag = false;

$result = mysqli_query($connection,"SELECT * FROM students ORDER BY tutor") or die(mysqli_error($connection));

while(false !== ($row = mysqli_fetch_assoc($result))) {
  if(!$flag) { // display field/column names as first row
    echo implode("\t", array_keys($row)) . "\n";
    $flag = true;
  }
  array_walk($row, 'cleanData');
  echo implode("\t", array_values($row)) . "\n";
  
  }
  
  ?>

 

It is exporting the rows as requested and I am able to open the file in excel....however...when downloading it becomes stuck on a loop and continues to download over 20mb of nothing. As a result I am forced to stop the download. When I open my excel file, below my table data is the following error, over and over and over and over!

<br />

<b>Warning</b>:  array_walk() [<a href='function.array-walk'>function.array-walk</a>]: The argument should be an array in <b>/home/inschool/public_html/student_list_export.php</b> on line <b>33</b><br />

<br />

<b>Warning</b>:  array_values() [<a href='function.array-values'>function.array-values</a>]: The argument should be an array in <b>/home/inschool/public_html/student_list_export.php</b> on line <b>34</b><br />

<br />

<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>/home/inschool/public_html/student_list_export.php</b> on line <b>34</b><br />

 

Any help on this would be awesome :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.