[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

add column names

Posted on 2010-01-05
8
Medium Priority
?
422 Views
Last Modified: 2012-05-08
want to add column names maybe something like this

fputcsv('column1', column2');
foreach($products as $p){
fputcsv($fp, array($p['folderid'], $p['name']) );
}
<?php
require_once('../common.php');    
$sql = "SELECT * FROM productfolders WHERE active = '1' ORDER BY name"; 
$products = dbfetcharray($sql); 
$fp = fopen('file.csv', 'w'); 
foreach($products as $p){
fputcsv($fp, array($p['folderid'], $p['name']) );
} 
fclose($fp); 
?>

Open in new window

0
Comment
Question by:rgb192
  • 4
  • 2
  • 2
8 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 26185892
Could you post a sample of the current CSV line you currently have
and a sample as you want it.

Or confirm :

Currently (one row from CSV file)
12,"potatoes"

And now you want something like :
12, "potatoes", "1Kg", 1455
0
 

Author Comment

by:rgb192
ID: 26185906
currently
12,"potatoes"



want

column1 column2
12,"potatoes"
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 1000 total points
ID: 26186121
OK so you code in your question look great, you just need to add the file pointer and create an array with the columns (the head) when you use fputcsv :    
<?php
require_once('../common.php');    
$sql = "SELECT * FROM productfolders WHERE active = '1' ORDER BY name"; 
$products = dbfetcharray($sql); 
$fp = fopen('file.csv', 'w'); 
// HEAD
fputcsv($fp, array('column1', column2'));
foreach($products as $p){
// ROWS
    fputcsv($fp, array($p['folderid'], $p['name']) );
}
fclose($fp); 
?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 26186353
If you want a generalized solution to this, you can use SHOW COLUMNS to get the column names.  This is untested code but you should be able to adapt it to your needs.  Best, ~Ray
<?php
require_once('../common.php');

// GET THE COLUMN NAMES
$cols = get_my_db_table_columns('productfolders');

// RUN A QUERY TO GET THE DATA SET
$sql = "SELECT * FROM productfolders WHERE active = '1' ORDER BY name"; 
$products = dbfetcharray($sql);

// OPEN THE OUTPUT CSV
$fp = fopen('file.csv', 'w');

// WRITE THE COLUMN NAMES
fputcsv($fp, $cols);

// ITERATE OVER THE DATA SET
foreach($products as $p)
{
    fputcsv($fp, array($p['folderid'], $p['name']) );
}
// CLOSE OUT THE CSV FILE
fclose($fp); 



function get_my_db_table_columns($table_name)
{
    $sql_c    = "SHOW COLUMNS FROM $table_name";
    if (!$c = mysql_query("$sql_c")) { die( mysql_error() ); }
    if (mysql_num_rows($c) == 0)
    {
        return FALSE;
    }
    while ($show_columns = mysql_fetch_assoc($c))
    {
        $my_columns[]    = $show_columns;
    }
    return $my_columns;
}

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 26186368
Mr Paseur, work only if we have only the two columns : 'folderid' and 'name'
OR we should modify line 20 too

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 26186388
Yes, of course - that is why I suggested "adapt it to your needs."

Maybe this would work better, with less adaptation... However it would need to be modified every time the columns changed.  Thoughtful naming of DB columns is a bit of an art and a bit of a science.

Best to all, ~Ray
<?php
require_once('../common.php');

// GET THE COLUMN NAMES
$cols = array('folderid', 'name');

// RUN A QUERY TO GET THE DATA SET
$sql = "SELECT folderid, name FROM productfolders WHERE active = '1' ORDER BY name"; 
$products = dbfetcharray($sql);

// OPEN THE OUTPUT CSV
$fp = fopen('file.csv', 'w');

// WRITE THE COLUMN NAMES
fputcsv($fp, $cols);

// ITERATE OVER THE DATA SET
foreach($products as $p)
{
    fputcsv($fp, array($p['folderid'], $p['name']) );
}
// CLOSE OUT THE CSV FILE
fclose($fp);

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 31673227
just needed those two lines
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26191834
Thanks for the points!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 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