I am working using php and mssql, and have a webpage that outputs contracts and how they are displayed is also controlled, so basically there are many ways in which to view the contracts.

The contracts are on display by building up a html table, so can I use that html table with all the data to export the data on show to a .csv file.

Cups
—
2012-06-22T15:34:04Z —
#2

If you have your results as some kind of array prior to building your html table, then you might be able to use reuse that array and use fputcsv. Otherwise a google search of php array to csv throws up various solutions.

Is that the crux of your question, how to export a result set to csv as well as build a html table?

wonshikee
—
2012-06-22T17:49:00Z —
#3

If the only data source is the HTML table, you will need to use a DOM viewer to pull it out manually.

As Cups stated, if you have access to the data source that created the HTML table, it's very easy to convert it to output in a CSV format

multichild
—
2012-06-22T19:24:30Z —
#4

Yes ive access to all the data in the database, and the html table I have created on the page is dynamically generated from that data using php and mysql.

The data changes to what ever the user wants to see it like, such as view contracts from early to late, view contracts from high price to low and so, and so basically what the user has done to generate that data, I would like to have say an 'export' button, and then when clicked all that is seen in the html table is then saved as a .csv file.

I'm just in the thinking process at the moment, and need direction on how to do it, but will be back in the office on monday and can look and post code too if that would help me, but will spend the weekend researching it a bit now.

wonshikee
—
2012-06-22T21:22:37Z —
#5

At the simplest level (though not the best), you can simply inject the code in between where you pull the data out of the database and where you output it as HTML.

Code to pull data out of database into an array.
if download_as_csv is true
set header for csv download
Loop through array and output as csv cells
exit
loop through array and output array as html table.

A more reusable solution (but also more complex) is to use a function to do the formatting, because html table structure and a csv are similar (td = comma, tr = newline), so you could use a function and do it like this

Code to pull data out of database into an array.
if download_as_csv is true
set header for csv download
echo foobar($array, 'csv');
exit
echo foobar($array, 'table');

Then the logic of the output goes in the function foobar, allowing yourself to reuse same code, which makes future changes/maintenance easier.

multichild
—
2012-06-28T10:25:19Z —
#6

I have a few different ways of pulling the data out of the database as below, would somebody mind looking through what I got and strating me off with the first idea abovem which is the basic one.

Can somebody start me off with this one, as I think I understand that you can use the fact that I am already creating a html tabel with all the data from the database, but I'm not sure where to start the code off so that when then 'Export' button is clicked in the nav bar, it creates the excel file according to the data that is on show on the page.

but im guessing that we up to date with SQL server, but cant be exactly sure

cpradio
—
2012-06-29T13:00:28Z —
#11

Are you using a hosting company or do you have the web server and the sql server in house? The only reason I ask, is you can tell SQL Server to create the CSV file for you, but it requires knowledge of the setup of both the sql server and the web server.

In short, you need to be able to have SQL Server write a file to the web server.Here is the command you would need to run

I only got the link to work mind, I still have to fill that excel file up with the data that's on display on the page.

Unfortunately once again the second I get my teeth into something liek this the weekend comes around again.

Would you mind if I bump this up on Monday, and we pick it up from there.

Thanks again cpradio, you must have a fantastic job there in Ohio, you know a lot fair play.

Take care

cpradio
—
2012-06-29T16:47:37Z —
#18

I do love my job here in Ohio. I actually changed jobs in December of last year and love the new one much more than the old job. A lot more relaxing. If you run into any issues getting your SQL Server questions answered by your host, feel free to ask them here.

If they ultimately tell you, you can't do that approach, then we'll have to work on a PHP only approach.

multichild
—
2012-07-03T08:47:46Z —
#19

Hi cpradio,

OK the techie is in now so can ask the questions we need to find out about the server.

So would you mind reminding me what we need to find out, as the way it currently works is perfect in that you click the 'export' button and they get the option to save the .csv file where they prefer.

The job that I got now, is to try and get the contracts on display into that excel file.

Cheers

cpradio
—
2012-07-03T09:47:44Z —
#20

Okay, to get the contacts to be in the downloaded csv file, you need to ask if you can run the following command on SQL Server:

Items to note:* Your query will by dynamic, meaning it will change based on what the user is currently viewing.* You need to know what to provide for the path (ideally it can write to a directory in your web folder via a network share)