Jump to content

Recommended Posts

is your output going to be display as webpage?

one of the best ways is do the read/write to file if you only need specific data from the table...

select the data then write it to file...

Super simple instruction here, might just do the trick

http://www.w3schools.com/php/php_file.asp

 

goodluck :D

Well, you take his code snippet:

<?PHP 
   function cleanData($str) { 
      $str = preg_replace("/\t/", "\\t", $str); 
      $str = preg_replace("/\r?\n/", "\\n", $str);
    }
header("Content-Type: text/plain"); 
  $flag = false;
  foreach($data as $row) { 
     if(!$flag) { 
        # display field/column names as first row 
        echo implode("\t", array_keys($row)) . "\r\n"; 
        $flag = true; 
    } 
     array_walk($row, 'cleanData');
   echo implode("\t", array_values($row)) . "\r\n";  
  } 
?>

 

And make some changes:

<?PHP 
    function cleanData($str) { 
      $str = preg_replace("/\t/", "\\t", $str); 
      $str = preg_replace("/\r?\n/", "\\n", $str);
    }
//header("Content-Type: text/plain"); //<-lose the header function.
$filename = 'excel_export.xls';
$output = NULL; //define our holding variable.
  $flag = false;
  foreach($data as $row) { 
     if(!$flag) { 
        # display field/column names as first row 
        $output .= implode("\t", array_keys($row)) . "\r\n"; //set this row to our variable.
        $flag = true; 
    } 
    array_walk($row, 'cleanData');
   $output .= implode("\t", array_values($row)) . "\r\n";  //set this row to our variable.
  } 
file_put_contents($filename,$output);  //write the output to a file;
?>

 

Well, I guess that was on me, as I thought you knew how to populate an array from the database.  Being that I don't know your database structure, I will give you arbitrary code.

 

<?php

include 'database_connection.php';

$sql = "SELECT * FROM table";
$result = mysql_query($sql) or trigger_error($sql . ' has an error.<br/>' . mysql_error());
if(mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
  }
}

if(!is_array($data)) {
  exit('There is no data to create an excel file from');
}

   function cleanData($str) { 
      $str = preg_replace("/\t/", "\\t", $str); 
      $str = preg_replace("/\r?\n/", "\\n", $str);
    }
//header("Content-Type: text/plain"); //<-lose the header function.
$filename = 'excel_export.xls';
$output = NULL; //define our holding variable.
  $flag = false;
  foreach($data as $row) { 
     if(!$flag) { 
        # display field/column names as first row 
        $output .= implode("\t", array_keys($row)) . "\r\n"; //set this row to our variable.
        $flag = true; 
    } 
    array_walk($row, 'cleanData');
   $output .= implode("\t", array_values($row)) . "\r\n";  //set this row to our variable.
  } 
file_put_contents($filename,$output);  //write the output to a file;
?>

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.