I have a .csv file. the first line of the file has a header and the last line has footer with totals of all columns.

So my question is how can I strip out the header and footer rows and just import the data... the first row of the data will always start with a number and the headers/footers will always be text if that helps...

thanks

    Hi,

    You can just read the whole file, e.g., with the file() function, and use a for loop to only use the required lines. I am not at home, so I cannot giveyou a working example, but something like:

    $file = file($filename);
    $num_lines = count($file);

    for($i=1; $<$num_lines;$i++)
    {
    echo $file[$i];
    }

      leatherback wrote:

      Hi,

      You can just read the whole file, e.g., with the file() function, and use a for loop to only use the required lines. I am not at home, so I cannot giveyou a working example, but something like:

      $file = file($filename);
      $num_lines = count($file);

      for($i=1; $<$num_lines;$i++)
      {
      echo $file[$i];
      }

      here is the code I use because the files I will be using are csv files.

      //Read File
      		$row = 1;
      		$fp = fopen ($uploadfile, "r");
      		while ($data = fgetcsv($fp,1000,",")){
      			$num = count($data);
      			//set Data to Variables
         		$item = $data[0];
      		$ignore1 = $data[1];
      		$account = $data[2];
      		$name = $data[3];
      		$amount = $data[4];
      		$pending = $data[5];
      		$error = $data[6];
      		$status = $data[7];
      		echo "<p> $num fields in line $row: <br /></p>\n";
         $row++;
         for ($c=0; $c < $num; $c++) {
             echo $data[$c] . "<br />\n";
         }
      		//$sql = "INSERT IGNORE INTO modpay_paids (`item`, `ignore1`, `account`, `name`, `amount`, `pending`, `error`, `status`) VALUES ('$item', '$ignore', '$account', '$name', '$amount', '$pending', '$error', '$status')";
      		//$r1 = mysql_query($sql, $dbConn)or die("<br>Error: ". mysql_error());
      		}//end While Loop
      		fclose($fp);
      		//End Read File
      

      I commented out the lines that will put the data into the database. And I put in some code to echo out the data. as I feared the first line puts the header row into field values.

      I was hoping there was a way to check if the value of a field was numeric or alpha. Kind of like VB where you could check the hex range. and only use the range of numbers...

      Thanks

        //Read File
                $row = 1;
                $fp = fopen ($uploadfile, "r");
                while ($data = fgetcsv($fp,1000,",")){
                    $num = count($data);
                    //set Data to Variables
                   $item = $data[0];
                $ignore1 = $data[1];
                $account = $data[2];
                $name = $data[3];
                $amount = $data[4];
                $pending = $data[5];
                $error = $data[6];
                $status = $data[7];
                echo "<p> $num fields in line $row: <br /></p>\n";
           $row++;
        
        //the line below I modified. Check whether it excludes only the first and the last
        
           for ($c=1; $c < $num-1; $c++) {
               echo $data[$c] . "<br />\n";
           }
                //$sql = "INSERT IGNORE INTO modpay_paids (`item`, `ignore1`, `account`, `name`, `amount`, `pending`, `error`, `status`) VALUES ('$item', '$ignore', '$account', '$name', '$amount', '$pending', '$error', '$status')";
                //$r1 = mysql_query($sql, $dbConn)or die("<br>Error: ". mysql_error());
                }//end While Loop
                fclose($fp);
        
        
        
        
          Write a Reply...