2012-07-05 73 views
0

所以我有这个页面列出网站上的'.txt'文件内容。我试图分页,但它不起作用。我想每页只有一个故事(一个故事是数据[0] .data [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

“我试图进行分页,但它不工作”,是什么不起作用?任何错误? – Tomer 2012-07-05 12:08:05

+0

它只显示1页(这是正确的,因为它设置为显示12层),当我点击它时,它复制当前页面。我试图将它设置为1个故事/页面,但没有成功。 – trisztann 2012-07-05 12:30:33

回答

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第二个解决方案://者优先它使用较少的内存比1解决方案

$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 
}