We help IT Professionals succeed at work.
Get Started

csv totals

sulentho
sulentho asked
on
399 Views
Last Modified: 2011-09-20
I have written this script to download data from a mysql table to csv and xls but I have one part where I am kind of confused how to do. Once I get all the results from the table I want the last row of the file to contain the total for the tax_sales and tax_ship columns. I will appreciate any idea that I can get. Thanks

Sulen

<?php
ob_start();
session_start();
require_once("db.php");
$sql ="SELECT tax_zip AS 'ZIP CODE', tax_state AS 'STATE', tax_county AS 'COUNTY', tax_city AS 'CITY', tax_jcode AS 'JURISDICTION CODE', tax_sales AS 'TAXABLE SALES', tax_ship AS 'TAXABLE S/H',      tax_tottax AS 'TAX RATE APPLIED' FROM taxreport ORDER BY tax_zip ASC";
$result = mysql_unbuffered_query($sql) or die ('CSV-query failed');
$rows = 0;
$csv = '';
while($row = mysql_fetch_assoc($result)) {
  if($rows++ == 0) {
     $csv .= '"'.implode('","', array_keys($row)).'"';
  }
  $csv .= "\r\n".'"'.preg_replace("/[\n\r]/",'', implode('","', $row)).'"';
}
$filename = $tax_state."_".$tax_smo.$tax_sday.$ax_syr."_".$tax_emo.$tax_eday.$tax_eyr.".csv";

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$filename);
echo $csv;
$sql ="DELETE FROM taxreport";
$result = mysql_query($sql);
ob_flush();
?>
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE