By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,383 Members | 2,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,383 IT Pros & Developers. It's quick & easy.

Remove columns from CSV using a php script

P: 7
Hello all. Not sure if this is the correct place to ask this but I am new to php and I am building a script to remove certain columns in a comma delimited file plus other things. My problem is removing the columns using a php script. Im not sure if I should use $temp or what. Could anyone please help me out. Thanks and glad to be a member.

JSchofield
Jan 18 '07 #1
Share this Question
Share on Google+
11 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Welcome to our community!

The following snippet from the PHP documentation (see fgetcsv) could help you. It reads a CSV file and echoes the data fields from each row. You can easily modify that to build a new file from the data. Holler if it doens't work out.[php]<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?> [/php]
Ronald :cool:
Jan 18 '07 #2

P: 7
That did work. Thank you very much!! You have been a great help to me.
Johnnie

Welcome to our community!

The following snippet from the PHP documentation (see fgetcsv) could help you. It reads a CSV file and echoes the data fields from each row. You can easily modify that to build a new file from the data. Holler if it doens't work out.[php]<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?> [/php]
Ronald :cool:
Jan 18 '07 #3

P: 7
I am now trying to add two columns (fields) together. Below is my code. Im using the . to add the two columns but it is not working for me. My output is still has the two seperate fields. I am actually trying to put an area code that is in one colmn and a phone number that is in another together to make a 10 digit number in one field. I am taking out some of the fields. Any idea why it isnt adding the two columns? Thanks again.
Johnnie

$oldphone = array($row[11].$row[12]);
$phone = implode( "", $oldphone);
$temp = array($row[0],$row[1],$row[2],$row[10],$phone,$row[13],$row[] = "English");


Welcome to our community!

The following snippet from the PHP documentation (see fgetcsv) could help you. It reads a CSV file and echoes the data fields from each row. You can easily modify that to build a new file from the data. Holler if it doens't work out.[php]<?php
$row = 1;
$handle = fopen("test.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
?> [/php]
Ronald :cool:
Jan 18 '07 #4

ronverdonk
Expert 2.5K+
P: 4,258
You'd better show YOUR code, so I can see how you constructed these $row values.

Ronald :cool:
Jan 18 '07 #5

P: 7
Im sorry. Here is all of my code. Thanks. Johnnie
[php]
<?php

$inputfile = "C:/SMDIM/Data/StudentDemographics_AllActive.txt";
$outputfile = "C:/SMDIM/Data/Roanoke_City_Student_Data.csv";
$logfilename = "C:/SMDIM/Logs/Roanoke_City_Student_Log.txt";
include'C:/SMDIM/Translators/importlibrary.inc.php';
$count = 0;
//open the file
if(!$outfilefp = fopen($outputfile, "w")) {
wlogdie("Failed to open $outputfile");
}else{
wlog("Opened $outputfile for writing");
}

if(!$inputfp = fopen($inputfile, "r")) {
wlogdie("Failed to open $inputfile");
}else{
wlog("Opened $inputfile for reading");
}

//add English language codes to the end of each line
//Keep needed lines and push unwanted out

while ($row = fgetcsv($inputfp, 1000, ",")){
$oldphone = array($row[11].$row[12]);
$phone = implode( "", $oldphone);
$temp = array($row[0],$row[1],$row[2],$row[10],$phone,$row[13],$row[] = "English");
writeline($outfilefp, $temp);
$count++;
}
if($count !== '0') {
wlog("finished the import with a total of $count records");
}else{
wlog("Failed to import data");
}

?>[/php]
You'd better show YOUR code, so I can see how you constructed these $row values.

Ronald :cool:
Next time show the code enclosed in tags! Makes it a lot better to read from a screen! - Ronald
Jan 18 '07 #6

P: 7
Sorry here is the import.inc.php that my code refers too as well.

<?

function wlogDie ($str) {
global $logfp;

wlog($str);
fclose($logfp);
die(-1);
}
function wlog ($str) {
global $logfp;
echo $str . "\n";
fwrite($logfp, date("Y-m-d H:i:s") . " - $str\r\n");
}

function writeLine ($stufile, $row) {
$sisline = '"' . implode('","',$row) . "\"\n";
if (!fwrite($stufile,$sisline)) {
fclose($stufile);
wlogDie("failed to write student line to import file: $sisline");
}
}

function andList ($row) {
$line = NULL;
for ($x = 0; $x < count($row); $x++) {
if ($line === NULL) {
$line = $row[$x];
} else if ( $x == (count($row) -1) ) {
$line .= " and " . $row[$x];
} else {
$line .= ", " . $row[$x];
}
}
return $line;
}

$logfp = fopen($logfilename,"a");
if (!$logfp) {
exit(-1);
}
/*****************************************/


?>
Jan 18 '07 #7

ronverdonk
Expert 2.5K+
P: 4,258
Why don't you just concatenate the 2 $row-fields in a string. Like this
Expand|Select|Wrap|Line Numbers
  1. $phone = $row[11].$row[12];
Ronald :cool:
Jan 18 '07 #8

P: 7
I did and it is not combining the two columns. I might add that the main file is a txt and in the php script I have it saving as a csv. I wouldnt think that would have anything to do with it. I really appreciate all the help. Thanks

Johnnie
Jan 18 '07 #9

ronverdonk
Expert 2.5K+
P: 4,258
Well, I just did that part in your code again. Only I write the new file with the fputcsv php command. And it works as it should (I think). Look at the following snippet.
Expand|Select|Wrap|Line Numbers
  1. input file:
  2. ======
  3. aa1,bb1,cc1,dd1,ee1,ff1,gg1,hh1,ii1,jj1,kk1,ll1,mm1,nn1
  4. aa2,bb2,cc2,dd2,ee2,ff2,gg2,hh2,ii2,jj2,kk2,ll2,mm2,nn2
  5. aa3,bb3,cc3,dd3,ee3,ff3,gg3,hh3,ii3,jj3,kk3,ll3,mm3,nn3
  6.  
  7. output file:
  8. =======
  9. aa1,bb1,cc1,kk1,ll1mm1,nn1,English
  10. aa2,bb2,cc2,kk2,ll2mm2,nn2,English
  11. aa3,bb3,cc3,kk3,ll3mm3,nn3,English
  12.  
  13. Screen output:
  14. ==========
  15. Opened myinfile.txt for reading
  16. Opened myoutfile.txt for writing
  17. finished the import with a total of 3 records
PHP program:
[php]<?php
$inputfile = "myinfile.txt";
$outputfile = "myoutfile.txt";
if (!$inputfp = fopen($inputfile, "r"))
die("<br>Failed to open $inputfile");
else
echo "<br>Opened $inputfile for reading";
if (!$outfilefp = fopen($outputfile, "w"))
die("<br>Failed to open $outputfile");
else
echo "<br>Opened $outputfile for writing";
//add English language codes to the end of each line
//Keep needed lines and push unwanted out
while ($row = fgetcsv($inputfp, 1000, ",")){
$phone = $row[11].$row[12];
$temp = array($row[0],$row[1],$row[2],$row[10],$phone,$row[13],$row[] = "English");
fputcsv($outfilefp, $temp);
$count++;
}
if ($count !== '0')
echo "<br>finished the import with a total of $count records";
else
echo "<br>Failed to import data";
?>[/php]
Ronald :cool:
Jan 18 '07 #10

P: 7
You got it to work. I changed my script to what you have sent me, tried it and it worked. Must have liked the fputcsv. You are the man!!! Thank you so much. I'm sure we will be talkiing again. Take care.

Johnnie
Jan 18 '07 #11

ronverdonk
Expert 2.5K+
P: 4,258
Glad I could help you out. See you again.

Ronald :cool:
Jan 18 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.