Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:29:33+00:00 2026-05-28T18:29:33+00:00

I am using code A, and everything is as expected. When I try to

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:29:34+00:00Added an answer on May 28, 2026 at 6:29 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing some code to type strings using the Robot class. Everything is
I'm viewing variables using the debugger. In debug builds everything in the code below
I am currently using code similar to this: try { // IE ONLY var
I am compiling with MinGW in Windows 7, using Code::Blocks as the IDE. Everything
I'm using code that I found on the CodeProject.com for a low-level keyboard hook
I've recently started using code coverage tools (particularily Emma and EclEmma), and I really
I have been using code similar to this MessageDlg('', mtWarning, [mbOK], 0); throughout my
I'm using code that takes a bitmap and converts it to 24 BPP so
I'm using code in the Application_BeingRequest() handler of my Global.asax to do some very
How do I close (using code) the last created CView in my MDI app.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.