Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Php and csv files + increments + PLEASE HELP?

0

35 views

Hello, i need help and i need it fast! .. it's pretty self explanatory if you look at the code..

what i NEED this code to do is to create a new CSV and call it output_1.csv, write 100 lines to that CSV, then save it, create output_2.csv, and write up to 100 lines, and continue to create 100 line files until all info is exhausted. can someone help me with this?????

This is what i have to work with. At the moment, it just writes to 1 file until everything is exhausted. truthfully, i need this to do 100,000 lines at a time, but for testing purposes, 100 is fine, i can change it. PLEASE PLEASE HELP. i've maxed out my deadline for this! thanks in advance!!

by the way.. yeah, im sure your eyes got big when i said 100,000 lines.......... i know.............. i know... but that's what i need. we have about 8 million things i need to split up. take too long to explain, just help me get it done please please.


<?php

include "base.php";

//$result1 = mysql_query("SELECT * FROM pts_ultd , scrape_data WHERE pts_ultd.B == scrape_data.R");

$a = "4";

$result1 = mysql_query("SELECT * FROM scrape_data , pts_ultd WHERE pts_ultd.P_B = scrape_data.S_R && pts_ultd.P_E != '".$a."' ");

//grab all the content
while($r=mysql_fetch_array($result1))
{
$A = $r["S_A"];
$B = $r["S_B"];
$C = $r["S_C"];
$D = $r["S_D"];
$E = $r["S_E"];
$F = $r["S_F"];
$G = $r["S_G"];
$H = $r["S_H"];
$I = $r["S_I"];
$J = $r["S_J"];
$K = $r["S_R"];
$L = $r["S_L"];
$M = $r["S_M"];
$N = $r["S_N"];
$O = $r["S_O"];
$P = $r["S_P"];
$Q = $r["S_Q"];
$R = $r["S_R"];
$S = $r["P_D"];

//this is where the intering of the data takes place currently..
$cvsData = $A . "," . $B . "," . $C . "," . $D . "," . $E . "," . $F . "," . $G . "," . $H . "," . $I . "," . $J . "," . $K . "," . $L . "," . $M . "," . $N . "," . $O . "," . $P . "," . $Q . "," . $R . "," . $S ."\n";


$fp = fopen("feed_output.csv","a"); // $fp is now the file pointer to file $filename

if($fp){
fwrite($fp,$cvsData); // Write information to the file
fclose($fp); // Close the file



} // end if



}//end while


?>

asked April 3, 2011 8:03 pm CDT
posted via StackOverflow

2 Answers

0
 

You can do that easily by keeping a counter variable.

$i++;
$file_index = floor($i / 100);

file_put_contents("output.$file_index.csv", $cvsData,
                  FILE_APPEND|LOCK_EX);

The last line replaces your fopen/fwrite/fclose construct.

answered April 3, 2011 8:23 pm CDT
0
 

This is very basic stuff. A simple "state machine" will do this for you:

$lines = 0;
$repeats = 0;
$fh = fopen("feed_output-{$repeats}", "wb");
while( ... generate a row... ) {
   fwrite($fh, $row);
   if ($lines % 100 = 0) {
      fclose($fh);
      $repeats++;
      fopen("feed_output-{$repeats}", "wb");
   }
   $lines++;
}
fclose($fh);

Basically, open a file, then starting writing out lines. If the # of lines written is a multiple of 100, close the previous file, open a new one, and keep on going with a new filename. REpeat until done, then close the last file.

answered April 3, 2011 8:23 pm CDT

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Generating swf files
March 16, 2011
View .apk files
January 7, 2011
FTP Ignoring Git Files
February 24, 2011
Two db connect files
February 21, 2011
Php including files
January 2, 2011
Postgresql log files
January 16, 2011