PDA

View Full Version : Get CSV from a PHP script on a server (mysql 5)



j.smith1981
09-21-2009, 07:29 AM
I have a dilemma here was wondering if someone could help me?

I the following script on a server:


$connect = mysql_connect('localhost', '****', '****'); // Connects to MySQL Server
$db_select = mysql_select_db('xcart', $connect); // Selects X Cart db

$filename = "products_Orders_" . date('HidmY') . ".csv"; // Makes the file name variable with date!

function parseCSVComments($comments) {
$comments = str_replace('"', '""', $comments); // Escape all " and replace with ""
if(eregi(",", $comments) or eregi("\n", $comments)) { // Checks if there's any commas or new lines
return '"'.$comments.'"'; // If there's any commas or new lines escape them!
} else {
return $comments; // If no new lines or commas just return the value
}
}

$sql = mysql_query("SELECT A4.value AS adventcode, A1.productcode, A1.product, SUM(A2.price) AS cost_price, SUM(A1.amount) AS qty, SUM(A2.price * A1.amount) AS grand_total, DATE_FORMAT(FROM_UNIXTIME(A3.date), '%e/%c/%Y') AS order_date
FROM xcart_order_details A1
LEFT JOIN xcart_pricing A2 ON A1.productid = A2.productid
LEFT JOIN xcart_orders A3 ON A1.orderid = A3.orderid
LEFT JOIN xcart_extra_field_values A4 ON A1.productid = A4.productid
WHERE A2.membershipid = '2' AND A3.status = 'P' AND A4.fieldid = '3'
GROUP BY A1.productid
ORDER BY A1.orderid AND A1.productid ASC");

$numberFields = mysql_num_fields($sql); // Set out number of fields we are actually fetching should be (4) with any luck!

if($numberFields) { // Checks if we can output anything or we need to?
for($i=0; $i<$numberFields; $i++) {
$head[] = mysql_field_name($sql, $i); // Create the headers for each column, this is the field name in the database
}
$headers = join(',', $head)."\n"; // Makes the header row in the CSV file

while($info = mysql_fetch_object($sql)) {
foreach($head as $fieldName) {
$row[] = parseCSVComments($info->$fieldName);
} // Ends foreach loop
$data .= join(',', $row)."\n"; // Create a new row of data and append it to the last row
$row = ''; // Clear the contents of the $row variable to start a new row
}
// Start out actual output of the CSV file!
header("Content-type: application/x-msdownload");
// header("Content-Disposition: attachment; filename=purchase_Report.csv"); // OLD FILENAME!
header("Content-Disposition: attachment; filename=\"$filename\""); // New file name with date added automatically!
header("Pragma: no-cache");
header("Expires: 0");
echo $headers.$data;
} else {
// Nothing needed to be output. Put an error message here or something.
echo 'No data available for this CSV.';
}
Basically when a user clicks on a link, standard "a href" link, it generates a csv file with a timestamp.

Is there anyway of getting VBA in Excel to save that file to a folder on the users computer?

Just would like some advice as to the best method to go about this as I am a bit confused at the moment.

Of course when a user clicks on the link it asks where the user wants to save the file (as the filename with includes a php timestamp is sent to the user as a html attachment as it where), would VBA be able to select yes and then fill in the information as to where to save this at? Or would I have to go by another method, if either please explain, bit stuck now.

Thanks in advance,
Jeremy.