텍스트 및 DB의 구간별 분할 처리
라인별로 데이터가 저장된 텍스트 파일의 분할 처리 방법입니다.
파일의 용량이 작을수록 부담이 줄어들겠죠?
(여러 방법 중 하나입니다. 쓰기 나름이죠.)
DB의 값을 이용하는 방법도 큰 차이 없습니다.
$start값은 0부터 시작해, 페이지가 바뀌며 증가합니다.
체험판 예제 코드를 끝으로 이만. ^^);
파일의 용량이 작을수록 부담이 줄어들겠죠?
(여러 방법 중 하나입니다. 쓰기 나름이죠.)
<?php
set_time_limit(0); // 30초 실행 제한 해제
$proc = 500; // 한 번에 500라인씩 처리
$temp = explode("\n",file_get_contents('data.txt')); $size = count($temp); // 파일 라인별 읽기 및 크기
$start = ( isset($_GET['start']) && is_numeric($_GET['start']) ) ? $_GET['start'] : 0; // 시작 위치
for ($i=$start,$end=$start+$proc;$i<$end&&$i<$size;$i++) // 반복
{
if ( !($data=trim($temp[$i])) ) continue; // 좌우공백제거, 값 없으면 Skip
// $data로 필요한 부분 처리 (지정 파일의 라인별 데이터)
}
if ( $end<$size ) { echo '<script type="text/javascript">location.href="?start='.$i.'";</script>'; exit; } // 이동
?>
set_time_limit(0); // 30초 실행 제한 해제
$proc = 500; // 한 번에 500라인씩 처리
$temp = explode("\n",file_get_contents('data.txt')); $size = count($temp); // 파일 라인별 읽기 및 크기
$start = ( isset($_GET['start']) && is_numeric($_GET['start']) ) ? $_GET['start'] : 0; // 시작 위치
for ($i=$start,$end=$start+$proc;$i<$end&&$i<$size;$i++) // 반복
{
if ( !($data=trim($temp[$i])) ) continue; // 좌우공백제거, 값 없으면 Skip
// $data로 필요한 부분 처리 (지정 파일의 라인별 데이터)
}
if ( $end<$size ) { echo '<script type="text/javascript">location.href="?start='.$i.'";</script>'; exit; } // 이동
?>
DB의 값을 이용하는 방법도 큰 차이 없습니다.
<?php
set_time_limit(0); // 30초 실행 제한 해제
$table = '테이블명';
$proc = 500; // 한 번에 500건씩 처리
$conn = mysql_connect('HOST','USER','PASS');
mysql_select_db('DB');
$size = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$table),0,0);
$start = ( isset($_GET['start']) && is_numeric($_GET['start']) ) ? $_GET['start'] : 0; // 시작 위치
$result = mysql_query("SELECT * FROM {$table} LIMIT {$start},{$proc}");
while ( $row = mysql_fetch_assoc($result) )
{
// $row로 필요한 부분 처리
}
mysql_close($conn);
if ( ($start+$proc)<$size ) { echo '<script type="text/javascript">location.href="?start='.($start+$proc).'";</script>'; exit; } // 이동
?>
set_time_limit(0); // 30초 실행 제한 해제
$table = '테이블명';
$proc = 500; // 한 번에 500건씩 처리
$conn = mysql_connect('HOST','USER','PASS');
mysql_select_db('DB');
$size = mysql_result(mysql_query('SELECT COUNT(*) FROM '.$table),0,0);
$start = ( isset($_GET['start']) && is_numeric($_GET['start']) ) ? $_GET['start'] : 0; // 시작 위치
$result = mysql_query("SELECT * FROM {$table} LIMIT {$start},{$proc}");
while ( $row = mysql_fetch_assoc($result) )
{
// $row로 필요한 부분 처리
}
mysql_close($conn);
if ( ($start+$proc)<$size ) { echo '<script type="text/javascript">location.href="?start='.($start+$proc).'";</script>'; exit; } // 이동
?>
$start값은 0부터 시작해, 페이지가 바뀌며 증가합니다.
체험판 예제 코드를 끝으로 이만. ^^);
<?php
$size = 1000;
$proc = 100;
$start = ( isset($_GET['start']) && is_numeric($_GET['start']) ) ? $_GET['start'] : 0;
for ($i=$start,$end=$start+$proc;$i<$end&&$i<$size;$i++) // 반복
{
echo $i.', ';
}
flush();
sleep(2);
if ( $end<$size ) { echo '<script type="text/javascript">location.href="?start='.($start+$proc).'";</script>'; exit; } // 이동
?>
$size = 1000;
$proc = 100;
$start = ( isset($_GET['start']) && is_numeric($_GET['start']) ) ? $_GET['start'] : 0;
for ($i=$start,$end=$start+$proc;$i<$end&&$i<$size;$i++) // 반복
{
echo $i.', ';
}
flush();
sleep(2);
if ( $end<$size ) { echo '<script type="text/javascript">location.href="?start='.($start+$proc).'";</script>'; exit; } // 이동
?>
함께 나눈 이야기
댓글댓글 남기기