php - Break query data into several files -
i want table database , break output data files (50 entries in each file): list01.txt, list02.txt... somehow got stacked @ question how break data more effectively.
if ( $result = $mysqli->query($query) ) { icount = 0; while ( $row = mysqli_fetch_array($result) ) { if ( icount % 50 == 0 ) { $snum = int( icount / 50 ); $filename = 'scripts/spisok'.$snum.'.txt'; $handle = fopen( $filename, 'w' ); } fwrite( $filename, $row['uname'].';'.$row['email'].'<br />' ); icount++; } echo 'ok'; $result->free(); }
can break $result 50-entry arrays first , write them all? sorry, im novice php
you can use array_chunk
<?php if ( $result = $mysqli->query( $query ) ) { $data = array(); while( $row = mysqli_fetch_array( $result ) ) { $data[] = $row['uname'].';'.$row['email']; } $result->free(); // divide array desired number of split lists $chunks = array_chunk( $data, 50 ); // loop through chunks foreach( $chunks $index => $chunk ) { $file = 'scripts/spisok'.( $index + 1 ).'.txt'; $chunk = implode( "<br />", $chunk ); file_put_contents( $file, $chunk ); // or /* $handle = fopen( $file, 'w' ); fwrite( $handle, $chunk ); fclose( $handle ); */ } unset( $data, $chunks ); } ?>
Comments
Post a Comment