Hi!

I am developing a php site and required to export the .php files to HTML, PDF, EXCEL, TEXT, CSV, ODS and SXC files. The problem i'm having here is that i don't know anything about exporting files. How should i start? Could anyone provide some steps to help me start up this task? Thanks in advance. Have a good day~:)

Recommended Answers

Hi lcyew and welcome to DaniWeb :)

Do you mean that you want to provide an export facility, or that you need to export your source code to those formats?

To provide an export facility you need to:

- read the contents of the file to be …

Jump to Post

Ok, that's what I meant from providing an export facility. From your original description I thought maybe you meant you want to convert your php source code to html or something.

So, your function should do the following:
- get table data
- open a destination file using fopen('w')

Jump to Post

All 8 Replies

Hi lcyew and welcome to DaniWeb :)

Do you mean that you want to provide an export facility, or that you need to export your source code to those formats?

To provide an export facility you need to:

- read the contents of the file to be exported using file_get_contents and change format if necessary for the new file format (might not need to parse)
- open destination file using fopen('w') -> the 'w' means write and create new rather than append
- use the returned resource to write the contents of the file to be exported using fwrite

For more info on these functions (and some good examples of file manipulation) check the documentation at www.php.net.

Hi, thanks for your reply.

May i know what is the difference between "to provide an export facility" and "to export your source code to those formats"?

I need it like....i have a table displaying information. Then by clicking (maybe a link), i can get a document in either of the formats mentioned above.

Ok, that's what I meant from providing an export facility. From your original description I thought maybe you meant you want to convert your php source code to html or something.

So, your function should do the following:
- get table data
- open a destination file using fopen('w')
- write to the file using fwrite
- close the file using fclose

Hope this gives you a start. Have a try and repost with your code if you get stuck.

I've go through some tutorial on fopen, fwrite and fclose. Sorry, but i still don't quite get what should i do.

Let's say i have a file named report.php, with all data on it.

According to what you've said,

- get table data - what if part of my data is directly retrieved from
the database, while the other part is produced by calculation
made on the page?
Will this affect anything?

- open a destination file using fopen('w') - is it if i want to get a .xls
file, and i create the file using fopen?

- write to the file using fwrite - what exactly should i write to the file? All data i want is already there in report.php.

Sorry for bothering you again. Thanks for your help. It's greatly appreciated. :)

Creating output files in most of these formats isn't that difficult. To make it even easier, I created a small include module that you can use. You can download it at:
Desktop_Write

For example, with CSV file format, each row of data is a row in the csv file and each column is separated by a comma. So what you need to do is this:

// let's say the report.php file has a table of data called $table
// and we want to export that data to the file export.csv
// first we open the file that we want to write to
$export_handle = fopen("export.csv", "w"); // note the "w" format here
// make sure that we opened it
if(! $export_handle)
{
   echo "Error opening file.";
   die;
}
// we need to get each row in the table and write it to the file, so...
foreach($table as $row)
{
   // in each row there are several columns that we need to output
   // note you could also use the implode function here, but for illustration purposes...
   foreach($row as $column)
   {
      $result = fwrite($export_handle, $column . ",");
      // check the write succeeded - $result = number of bytes written
      if($result === false)
      {
         echo "Error in column write.";
         die;
      }
       // each row needs to have a carriage return appended
       $endLine = fwrite($export_handle, "\r\n");
       // check write was ok
       if($endLine === false)
       {
            echo "Error in row write.";
            die;
       }
   }
}
// here we need to cleanup after ourself by closing the file
fclose($export_handle);

Hope this illustration helps. Other formats will be different in what gets written, but the basic algorithm is the same for any export.

Chrishea and darkagn, I've tried both codes you guys provided and I'm having problem in writing the files in desired format. The table I'm having is complicated(at least for me) and I do not know how to write it in report.csv.

This is the view of the tables(may have more than one table):
http://www.daniweb.com/forums/attachment.php?attachmentid=12014&stc=1&d=1254973036

How should I write them? Sorry again for bothering...thanks a lot.

I attach along my report.php file:

dt_summaryReport.php

Any help is much appreciated:) Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.