Solved

How do I add a header row to this txt file export

Posted on 2015-02-13
2
69 Views
Last Modified: 2015-02-13
This script works perfectly, however, I need to add a header row to the txt output file.  How and where do I do that?

$fh = fopen('ShoppingFeeds/Bing/Bing.txt', 'w');

$sql = 'SELECT * FROM Feeds_BD_Bing';
$pdos = $conn->prepare($sql);

// TRY THE QUERY
try
{
    $pdos->execute();
}
catch(PDOException $exc)
{
    var_dump($exc);
    trigger_error($exc->getMessage(), E_USER_ERROR);
}

// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
$num     = $pdos->rowCount();
$num_fmt = number_format($num);
if (!$num)
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND NO DATA ";
    echo PHP_EOL;
}
else
{
    echo "<br/>QUERY: $sql ";
    echo "<br/>FOUND $num_fmt ROWS OF DATA ";
    echo PHP_EOL;
}

//ADD HEADER ROW HERE???   PHP echo???

// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row40 = $pdos->fetch(PDO::FETCH_ASSOC))
{
    
	
	foreach ($row40 as $item) {
        fwrite($fh, $item);
        if ($item != $last)
            fwrite($fh, "\t");
    }
    fwrite($fh, "\n");
	
	
	
	
	
}

Open in new window

0
Comment
Question by:lawrence_dev
2 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 500 total points
ID: 40608003
Before this line:
// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND
while ($row40 = $pdos->fetch(PDO::FETCH_ASSOC))
{

Open in new window


...add:
fwrite($fh, "ColumnA,ColumnB,ColumnC...etc...\n");

Open in new window

0
 

Author Comment

by:lawrence_dev
ID: 40608776
Thanks Gr8Gonzo!  Really appreciate the help!  Worked great!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now