I'm a relative newbie to PHP, but have been able to put together some PHP
code to generate a CSV/XLS file from a Query result. Now, I would like to
include custom column names instead of the MySQL column table names. I know
that there are codes to generate tabs and carriage returns, but can't find
anything about including "commas" in a string to output to the file to
separate the custom field names. I'd appreciate some help with a line of
code to insert those custom column names like:
First Name | Last Name | DOB | etc.
Thanks,
Joe G.
The generated file is set to the company name + date.xls and output the data
correctly. My PHP code is:
<?
@session_start();
// Remove spaces from Company Name
$trim_name = str_replace(" ", "", $co_name);
$file_name=$trim_name.date("mdY").".xls";
echo $file_name;
// Connect to Database
include("dbinfo.inc.php");
mysql_connect(localhost,$username,$pass);
@mysql_select_db($database) or die( "Unable to select database");
// Find the Number of Fields
$select = "SELECT * FROM employee WHERE co_name='$co_name'";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
// Extract Database Fields
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
// Extract your Data
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);
// Default Message
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
$fp = fopen($file_name,"a"); // $fp is now the file pointer to file
$file_name
if($fp){
$fp = fopen($file_name,"a");
fwrite($fp,$data); // Write information to the file
fclose($fp); // Close the file
echo "File saved successfully";
} else {
echo "Error saving file!";
echo "Use your Browser Back Button to Return";
}
?> 2 3856
Joe Gazda wrote: I'm a relative newbie to PHP, but have been able to put together some
PHP code to generate a CSV/XLS file from a Query result. Now, I would
like to include custom column names instead of the MySQL column table names.
I know that there are codes to generate tabs and carriage returns, but can't
find anything about including "commas" in a string to output to the file
to separate the custom field names.
Just replace "\t" with ",". That's it.
I'd appreciate some help with a line of code to insert those custom column names like:
First Name | Last Name | DOB | etc.
OK, right now you have:
for ($i = 0; $i < $fields; $i++) { $header .= mysql_field_name($export, $i) . "\t"; }
Just replace it with
$header = "First Name,Last Name,DOB,etc.";
Cheers,
NC
Substituting the code yields the field name string in the first cell of MS
Excel.
I've just decided to have the client "import" the data fields into an
existing Excel Spreadsheet with the Custom Field Names already in place.
Thanks for you help, anyway.
"NC" <nc@iname.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Joe Gazda wrote: I'm a relative newbie to PHP, but have been able to put together some
PHP code to generate a CSV/XLS file from a Query result. Now, I would like to include custom column names instead of the MySQL column table names. I know that there are codes to generate tabs and carriage returns, but can't find anything about including "commas" in a string to output to the file to separate the custom field names.
Just replace "\t" with ",". That's it.
I'd appreciate some help with a line of code to insert those custom column names like:
First Name | Last Name | DOB | etc.
OK, right now you have:
for ($i = 0; $i < $fields; $i++) { $header .= mysql_field_name($export, $i) . "\t"; }
Just replace it with
$header = "First Name,Last Name,DOB,etc.";
Cheers, NC This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: James Perry |
last post by:
Hi,
I have been trying to build an custom report interface for a charity
manangement system; which is part of my dissertation. This interface
will allow the chairty to input a SQL query and...
|
by: malcolm |
last post by:
Example, suppose you have these 2 tables
(NOTE: My example is totally different, but I'm simply trying to setup
the a simpler version, so excuse the bad design; not the point here)
CarsSold {...
|
by: JJA |
last post by:
Please advise on how to get the GROUP BY coded in an acceptable way:
DECLARE @LO INT
DECLARE @HI INT
DECLARE @StartDate varchar(10)
DECLARE @EndDate varchar(10)
SELECT @StartDate =...
|
by: Steve |
last post by:
I have looked through the newsgroup for an answer to this but haven't
been able to find anything resembling my situation.
What I want to do is relatively simple, I think. I have a crosstab...
|
by: Larry R Harrison Jr |
last post by:
I have a database I'm designing in Access 97. I have a custom field in a
query which looks in {Table of Documents} and shows them all. It then needs
a "latest revision number," stored in another...
|
by: Mike S |
last post by:
Hi all,
A (possibly dumb) question, but I've had no luck finding a definitive
answer to it. Suppose I have two tables, Employees and Employers, which
both have a column named "Id":
Employees...
|
by: beary |
last post by:
I picked up the following code to take a mysql table to an excel file. What do I need to do to it to make the first excel row be the column names, rather than the first record?
$query = "SELECT *...
|
by: jeoffh |
last post by:
Background:
I am trying to "merge" some attributes into an existing XML column in my MS SQL 2005 database. The general idea is that I have an XML column in a table and I would like to update/delete...
|
by: hwalker |
last post by:
Hello all. Long time reader, first time poster :)
I'm creating a repot that shows "the last two weeks of data the next 6 weeks of data, week over week".
So, I have a calculated field called...
|
by: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: kcodez |
last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |