Remove columns from CSV using a php script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jschofield
    New Member
    • Jan 2007
    • 7

    Remove columns from CSV using a php script

    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
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    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:

    Comment

    • jschofield
      New Member
      • Jan 2007
      • 7

      #3
      That did work. Thank you very much!! You have been a great help to me.
      Johnnie

      Originally posted by ronverdonk
      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:

      Comment

      • jschofield
        New Member
        • Jan 2007
        • 7

        #4
        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");


        Originally posted by ronverdonk
        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:

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          You'd better show YOUR code, so I can see how you constructed these $row values.

          Ronald :cool:

          Comment

          • jschofield
            New Member
            • Jan 2007
            • 7

            #6
            Im sorry. Here is all of my code. Thanks. Johnnie
            [php]
            <?php

            $inputfile = "C:/SMDIM/Data/StudentDemograp hics_AllActive. txt";
            $outputfile = "C:/SMDIM/Data/Roanoke_City_St udent_Data.csv" ;
            $logfilename = "C:/SMDIM/Logs/Roanoke_City_St udent_Log.txt";
            include'C:/SMDIM/Translators/importlibrary.i nc.php';
            $count = 0;
            //open the file
            if(!$outfilefp = fopen($outputfi le, "w")) {
            wlogdie("Failed to open $outputfile");
            }else{
            wlog("Opened $outputfile for writing");
            }

            if(!$inputfp = fopen($inputfil e, "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($inputf p, 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($outf ilefp, $temp);
            $count++;
            }
            if($count !== '0') {
            wlog("finished the import with a total of $count records");
            }else{
            wlog("Failed to import data");
            }

            ?>[/php]
            Originally posted by ronverdonk
            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

            Comment

            • jschofield
              New Member
              • Jan 2007
              • 7

              #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($stufi le,$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($logfilen ame,"a");
              if (!$logfp) {
              exit(-1);
              }
              /*************** *************** ***********/


              ?>

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                Why don't you just concatenate the 2 $row-fields in a string. Like this
                Code:
                $phone = $row[11].$row[12];
                Ronald :cool:

                Comment

                • jschofield
                  New Member
                  • Jan 2007
                  • 7

                  #9
                  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

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    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.
                    Code:
                    input file:
                    ======
                    aa1,bb1,cc1,dd1,ee1,ff1,gg1,hh1,ii1,jj1,kk1,ll1,mm1,nn1
                    aa2,bb2,cc2,dd2,ee2,ff2,gg2,hh2,ii2,jj2,kk2,ll2,mm2,nn2
                    aa3,bb3,cc3,dd3,ee3,ff3,gg3,hh3,ii3,jj3,kk3,ll3,mm3,nn3
                    
                    output file:
                    =======
                    aa1,bb1,cc1,kk1,ll1mm1,nn1,English
                    aa2,bb2,cc2,kk2,ll2mm2,nn2,English
                    aa3,bb3,cc3,kk3,ll3mm3,nn3,English
                    
                    Screen output:
                    ==========
                    Opened myinfile.txt for reading
                    Opened myoutfile.txt for writing
                    finished the import with a total of 3 records
                    PHP program:
                    [php]<?php
                    $inputfile = "myinfile.t xt";
                    $outputfile = "myoutfile.txt" ;
                    if (!$inputfp = fopen($inputfil e, "r"))
                    die("<br>Failed to open $inputfile");
                    else
                    echo "<br>Opened $inputfile for reading";
                    if (!$outfilefp = fopen($outputfi le, "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($inputf p, 1000, ",")){
                    $phone = $row[11].$row[12];
                    $temp = array($row[0],$row[1],$row[2],$row[10],$phone,$row[13],$row[] = "English");
                    fputcsv($outfil efp, $temp);
                    $count++;
                    }
                    if ($count !== '0')
                    echo "<br>finish ed the import with a total of $count records";
                    else
                    echo "<br>Failed to import data";
                    ?>[/php]
                    Ronald :cool:

                    Comment

                    • jschofield
                      New Member
                      • Jan 2007
                      • 7

                      #11
                      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

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        Glad I could help you out. See you again.

                        Ronald :cool:

                        Comment

                        Working...