mobilestimulus
mobilestimulus

Reputation: 180

Pagination Producing Unexpected Results. What did I do wrong?

I am using code A, and everything is as expected. When I try to add pagination script(code B), the results are no longer as expected. What did I do wrong? Any assistance would be much appreciated. Thanks....

code 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>';
}

code 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>';
}

Lets say file.txt contains:

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.

Upvotes: 0

Views: 142

Answers (1)

guido
guido

Reputation: 19194

Your problem is here (in which there's confusion about keys/values):

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

Just replace that code with:

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

array_slice documentation

Upvotes: 1

Related Questions