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 9129207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:35+00:00 2026-06-17T07:41:35+00:00

I have the following script, which will paginated results from an array… but only

  • 0

I have the following script, which will paginated results from an array… but only when using a numeric key value. Can this script be modified to work with an associative array using a string value for the key? I need to do that to sort the array alphabetically, with the eventual aim of pagination in an alphabetical fashion.

Here the script so far.

$my_array = array(0 => 'a', 1 => 'b', 2 => 'c'); // THIS WORKS FINE

//$my_array = array('a' => 'a', 'b' =>'b', 'c'=>'c'); // THIS DOESN'T WORK

$arr = $my_array; 

$rows_per_page = 1;

$numrows = count($arr);

// Calculate number of $lastpage
$lastpage = ceil($numrows/$rows_per_page);

// condition inputs/set default
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
}

// validate/limit requested $pageno
$pageno = (int)$pageno;
if ($pageno > $lastpage) {
   $pageno = $lastpage;
}
if ($pageno < 1) {
   $pageno = 1;
}

// Find start and end array index that corresponds to the requested pageno
$start = ($pageno - 1) * $rows_per_page;
$end = $start + $rows_per_page -1;

// limit $end to highest array index
if($end > $numrows - 1){
    $end = $numrows - 1;
}

// display array from $start to $end
for($i = $start;$i <= $end;$i++){
    echo $arr[$i] . '<br />';
}
echo "<p style='text-align:center;'>";

// first/prev pagination hyperlinks
if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='?pageno=$prevpage'>PREV</a> ";
}

// Display current page or pages
echo " ( Page $pageno of $lastpage ) ";

// next/last pagination hyperlinks
if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='?pageno=$lastpage'>LAST</a> ";
}

echo "</p>";
?>
  • 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-06-17T07:41:36+00:00Added an answer on June 17, 2026 at 7:41 am

    You are referring to an associative array by a numerical index, replace

    for($i = $start;$i <= $end;$i++){
        echo $arr[$i] . '<br />';
    }
    

    with:

    $arr_keys = array_keys($arr);
    for($i = $start;$i <= $end;$i++){
        echo '-'.$arr[$arr_keys[$i]] . '<br />';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following script which will delete a file off a BlackBerry. However,
I have the following script which first shows only the first para of the
I have the following script which will change the background image of a text
I have a ruby script which will read data from some CSV file and
I have the following bash script which I will use to analyze all report
I have the following script which works very will: http://jsfiddle.net/oshirowanen/uUAqe/ I just need to
Hi I have the following script which works fine running on IIS but when
Here's what I am trying to do. I have the following script which will
I have written following script in python which works fine: from sys import argv
I have developed the following user script which will show the html element under

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.