Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

php code for excel spreadsheet combining 2 tables?


jcestlin

Recommended Posts

I use the below php code to create an Excel sheet from my customer table, (cred to Developer: Jeffrey M. Johns)

 

Any suggestions on how I could modify to create an Excel sheet that would let me know the customer info as well as specific items that customer purchased, all on one sheet?

Thanks in advance,

Jennifer

 

 

 

<?php

/********************************************

PHP - Excel Extraction Tutorial Code

Page: excel.php

Developer: Jeffrey M. Johns

Support: [email protected]

Created: 10/01/2003

Modified: N/A

*********************************************

Notes/Comments: This code is a very basic/replica of

the code that is in the tutorial. To make it work you must

define your connection variables below. Make sure you

replace the following all CAPS text with your proper values.

*********************************************

YOUR DATABASE HOST = (ex. localhost)

USERNAME = username used to connect to host

PASSWORD = password used to connect to host

DB_NAME = your database name

TABLE_NAME = table in the database used for extraction

*********************************************

This code will extract the data from your table and format

it for an excel spreadsheet download. It is very quick,

simple, and to the point. If you only want to extract

certain fields and not the whole table, simply replace

the * in the $select variable with the fields you want

to extract.

*********************************************

Disclaimer: Upon using this code, it is your responsibilty

and I, Jeffrey M. Johns, can not be held accountable for

any misuse or anything that may go wrong.

*********************************************

Other: Support will not be provided if the code is

enhanced or changed. I do not have the time for

figuring out your changes and modifications. I will only

offer simple support for the code listed below.

/********************************************/

define(db_host, "mysql.secureserver.net");

define(db_user, "*****");

define(db_pass, "*****");

define(db_link, mysql_connect(db_host,db_user,db_pass));

define(db_name, "*****");

mysql_select_db(db_name);

/********************************************

Write the query, call it, and find the number of fields

/********************************************/

$select = "SELECT * FROM records";

$export = mysql_query($select);

$count = mysql_num_fields($export);

/********************************************

Extract field names and write them to the $header

variable

/********************************************/

for ($i = 0; $i < $count; $i++) {

$header .= mysql_field_name($export, $i)."\t";

}

/********************************************

Extract all data, format it, and assign to the $data

variable

/********************************************/

while($row = mysql_fetch_row($export)) {

$line = '';

foreach($row as $value) {

if ((!isset($value)) OR ($value == "")) {

$value = "\t";

} else {

$value = str_replace('"', '""', $value);

$value = '"' . $value . '"' . "\t";

}

$line .= $value;

}

$data .= trim($line)."\n";

}

$data = str_replace("\r", "", $data);

/********************************************

Set the default message for zero records

/********************************************/

if ($data == "") {

$data = "\n(0) Records Found!\n";

}

/********************************************

Set the automatic downloadn section

/********************************************/

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment; filename=spreadsheet.xls");

header("Pragma: no-cache");

header("Expires: 0");

print "$header\n$data";

?>

Link to comment
Share on other sites

or...does anyone have a way of getting a report that details

the customers' details as well as what they purchased, all on one report?

 

 

 

 

I use the below php code to create an Excel sheet from my customer table, (cred to Developer: Jeffrey M. Johns)

 

Any suggestions on how I could modify to create an Excel sheet that would let me know the customer info as well as specific items that customer purchased,? all on one sheet?

Thanks in advance,

Jennifer

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...