I wote this code to this and it works

the problem is that my csv file does not have the headers ID, Description , Category in the top of the file. I wrote this code

<?php


$list = array
(
"ID,Category,Description,,,,,,,,,,,,,,,,,,"
);

$file = fopen("DS20080507.csv","r+", 1000);

foreach ($list as $line)
  {
  fputcsv($file,split(',',$line));
  }

fclose($file);



$row = 1;
$handle = fopen("DS20080507.csv", "r+");


while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

    }
}




fclose($handle);


?>

But it does not work right, it puts the headers in the top but deletes part of the first line of the csv file. I ve tries usine file mode a and a+ and puts the headrs in the bottom of the csv

any ideas on how to fix this issue?

Thank you

This works perfectly fine for me.

<?php
$list = array("ID,Category,Description");

$file = fopen("./csv/DS20080507.csv","a+", 1000);
foreach ($list as $line)
{
	fputcsv($file,split(',',$line));
}
fclose($file);
$row = 1;
$handle = fopen("./csv/DS20080507.csv", "a+");
for($i=1;$i<10;$i++) {
	fwrite($handle,"test1,\t test2, \t test3 \t\n");
}
fclose($handle);
$handle = fopen("./csv/DS20080507.csv", "r+");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

    }
}
fclose($handle);
?>

This works perfectly fine for me.

<?php
$list = array("ID,Category,Description");

$file = fopen("./csv/DS20080507.csv","a+", 1000);
foreach ($list as $line)
{
	fputcsv($file,split(',',$line));
}
fclose($file);
$row = 1;
$handle = fopen("./csv/DS20080507.csv", "a+");
for($i=1;$i<10;$i++) {
	fwrite($handle,"test1,\t test2, \t test3 \t\n");
}
fclose($handle);
$handle = fopen("./csv/DS20080507.csv", "r+");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
   
    $row++;
    for ($c=0; $c < $num; $c++) {
        echo $data[$c] . "<br />\n";

    }
}
fclose($handle);
?>

This is what my file does when I write you version of the code

http://media.yumasun.com/xml/csv.php

I just need the headers on the top of the file

Thank you

You can do this. If the counter is 0, then display without a break.

for ($c=0; $c < $num; $c++) {
if($c == 0) {
 echo $data[$c];
} else {
        echo $data[$c] . "<br />\n";

    }

You can do this. If the counter is 0, then display without a break.

for ($c=0; $c < $num; $c++) {
if($c == 0) {
 echo $data[$c];
} else {
        echo $data[$c] . "<br />\n";

    }

It still putting the ID, Category, description in the bottom plus all the test1 test 2 test 3 which I dont need

I need only the headers in the top with out cutting part of the csv

Thank yooou

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.