2012-07-05 5 views
0

그래서이 페이지는 '.txt'파일 내용을 웹 사이트에 나열합니다. 페이지 매김을 시도했지만 작동하지 않습니다. 페이지 당 단 하나의 이야기 만하고 싶습니다 (한 이야기는 데이터입니다 [0]. 데이터 [1]) 페이지가 ajax를 통해 브라우저에 호출됩니다. 그래서 다시PHP 동적 페이지 페이지 지정

<?php 
$dataArray = array(); 
//Number of chars for the string 
$num = 500; 
$dir = '../php/biralas_tortenetek/'; 
$willcount = readdir(opendir($dir)); 
$totfiles = count(readdir(opendir($dir))); 
//Check if </div>DIR e</div>xists 
if ($handle = opendir($dir)) { 
    //Loop over the directory 
    while (false !== ($file = readdir($handle))) { 
     //Strip out the . and .. files 
     if ($file != "." && $entry != "..") { 
      //Store file contents 
      $filecontent = file_get_contents($dir . $file); 
      //Split the content and store in array 
      $length = strlen($filecontent); 
      $dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length)); 

     } 
    } 
    //close the dir 
    closedir($handle); 
} 


?><?php 
$page = isset($_GET['page']) ? $_GET['page']-1 : 0; 
echo "<br/>"; 
for($x=$page*1; $x < $totfiles && $x < ($page+1)*12; $x++) 
{ 
    foreach($dataArray as $data) { ?> 
     <div class="visible"> 
      <?php echo $data[0] . $data[1]; ?> 
     </div><?php } ?> 
</div> 
<?php } 
for($page=1; ($page-1)*12 < $totfiles; $page++) 
{ 
    echo "<div class='lapozo'><a onclick='story_changepage($page);' href='../html/blog.php#tortenetek?page=$page'>$page</a></div>"; 
} 
?> 

, 목표는 페이지 당 하나의 이야기를하는 것입니다 : 다음은 내 코드입니다. 감사합니다.

+0

1 솔루션보다 적은 메모리를 사용입니다 "내가 페이지를 매기려고했는데, 그것은 작동하지 않습니다"선호, 어떤 작동하지 않습니까? 오류가 있습니까? – Tomer

+0

12 페이지를 표시하도록 설정되어 있기 때문에 1 페이지 만 표시됩니다. 클릭하면 현재 페이지가 복제됩니다. 1 층/1 페이지로 만들려고했지만 성공하지 못했습니다. – trisztann

답변

2

1 솔루션 :

$willcount = readdir(opendir($dir)); 
$i = 0; 
//Check if </div>DIR e</div>xists 
if ($handle = opendir($dir)) { 
    //Loop over the directory 
    while (false !== ($file = readdir($handle))) { 
     //Strip out the . and .. files 
     if ($file != "." && $entry != "..") { 
      //Store file contents 
      $filecontent = file_get_contents($dir . $file); 
      //Split the content and store in array 
      $length = strlen($filecontent); 
      // store file as indexed item of array 
      $dataArray[$i++] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length)); 
     } 
    } 
    //close the dir 
    closedir($handle); 
} 
// store total files in dir 
$totfiles = $i; 

$page = isset($_GET['page']) ? $_GET['page']-1 : 0; 
echo "<br/>"; 

for($x=$page*12; $x < $totfiles && $x < ($page+1)*12; $x++) { 
    $data = $dataArray[$x]; 
    ?> 
     <div class="visible"> 
      <?php echo $data[0] . $data[1]; ?> 
     </div> 
<?php 
} 

CUT 2 해결 : // 그것은

$willcount = readdir(opendir($dir)); 
$i = 0; 
// get page 
$page = isset($_GET['page']) ? $_GET['page']-1 : 0; 

//Check if </div>DIR e</div>xists 

if ($handle = opendir($dir)) { 
    //Loop over the directory 
    while (false !== ($file = readdir($handle))) { 
     //Strip out the . and .. files 
     if ($file != "." && $entry != "..") { 
      $i ++; 
      // if our page not first, skip add 
      if ($i <= $page * 12) continue; 
      // if we reach end of the page, break 
      if ($i > ($page + 1)* 12) break; 
      //Store file contents 
      $filecontent = file_get_contents($dir . $file); 
      //Split the content and store in array 
      $length = strlen($filecontent); 
      $dataArray[] = array(substr($filecontent, 0, $num), substr($filecontent, $num, $length)); 
     } 
    } 
    //close the dir 
    closedir($handle); 
} 
// store total files in dir 
$totfiles = $i; 

echo "<br/>"; 

foreach($dataArray as $data) { 
    ?> 
     <div class="visible"> 
      <?php echo $data[0] . $data[1]; ?> 
     </div> 
<?php 
}