分页产生意外结果。我做错了什么?

时间:2012-01-31 03:19:21

标签: php pagination

我正在使用代码A,一切都按预期进行。当我尝试添加分页脚本(代码B)时,结果不再符合预期。我做错了什么?任何援助将不胜感激。感谢....

代码A:

$data = 'path/to/file.txt';

$counts = array_count_values(
      array_map(function($line){return strtoupper(end(explode('||', $line, -4)));},
      array_filter(file($data), 'trim')));

foreach($counts as $key1=>$value){
echo '<div>'. $key1 .' - '. $value .'</div>';
}

代码B:

$link_range = 2;
$listings = 2;
if (isset($_SERVER['QUERY_STRING'])) {
$currentPage = $_SERVER['QUERY_STRING'];
} else {
$currentPage = '0';
}

$reg_ex = "[page=]";
$replace_word = ""; 
$str = $currentPage;
$currentPage = ''.ereg_replace($reg_ex, $replace_word, $str).'';

$data = 'path/to/file.txt';

$counts = array_count_values(
      array_map(function($line){return strtoupper(end(explode('||', $line, -4)));},
      array_filter(file($data), 'trim')));

$dataArray = $counts;

// Pagination settings  
$perPage = $listings;  
$numPages = ceil(count($dataArray) / $perPage);  
if(!$currentPage || $currentPage > $numPages)  
    $currentPage = 0;  
$start = $currentPage * $perPage;  
$end = ($currentPage * $perPage) + $perPage;  
// Extract ones we need  
foreach($dataArray AS $keys => $val)  
{  
    if($keys >= $start && $keys < $end)  
        $pagedData[] = $dataArray[$keys];  
}

     $range = $link_range;    
 if ($currentPage > 0 && $currentPage < $numPages) {
// show << link to go back to page 1
echo '<a href="?page=0" title="Link">&lt;&lt;</a> |';
// get previous page num
$prevpage = $currentPage - 1;
// show < link to go back to 1 page
echo ' <a href="?page='. $prevpage .'" title="Link">&lt;</a> |';
 } // end if

 // loop to show links to range of pages around current page
 for ($x = ($currentPage - $range); $x < (($currentPage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > -1) && ($x <= $numPages - 1)) {
   // if we're on current page...
   if ($x == $currentPage) {
      // 'highlight' it but don't make a link
      echo ' '. ($x + 1) .' |';
   // if not current page...
   } else {
      // make it a link
      echo ' <a href="?page='. ($x) .'" title="Link">'. ($x + 1) .'</a> |';
   } // end else
} // end if 
 } // end for

if ($currentPage != $numPages - 1) {
// get next page
$nextpage = $currentPage + 1;
 // echo forward link for next page 
echo ' <a href="?page='. $nextpage .'" title="Link">&gt;</a> |';
// echo forward link for lastpage
echo ' <a href="?page='. ($numPages - 1) .'" title="Link">&gt;&gt;</a>
';
 } // end if

foreach($pagedData as $key1=>$value){
echo '<div>'. $key1 .' - '. $value .'</div>';
}

让我们说file.txt包含:

a||b||Vietnam||c||d||e||f
a||b||HONG KONG||c||d||e||f
a||b||Vietnam||c||d||e||f
a||b||INDONESIA||c||d||e||f
a||b||UNITED STATES||c||d||e||f
ect.

1 个答案:

答案 0 :(得分:1)

你的问题在这里(关于键/值的混淆):

// Extract ones we need  
foreach($dataArray AS $keys => $val)  
{  
    if($keys >= $start && $keys < $end)  
        $pagedData[] = $dataArray[$keys];  
}

只需用以下代码替换该代码:

$pagedData = array_slice($dataArray, $start, $listings, true);

array_slice documentation