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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:27:27+00:00 2026-05-24T11:27:27+00:00

I’m having a problem with my PHP pagination for a project. It almost works

  • 0

I’m having a problem with my PHP pagination for a project.

It almost works but it doesn’t seem to display the numbers correctly.

I want only 6 more page numbers to display after the selected and one before;
(also if you are on page one display 7 after)

For example:

If on Page 1: 1/2/3/4/5/6/7/8

If on Page 2: 1/2/3/4/5/6/7/8

If on Page 5: 4/5/6/7/8/9/10/11

If on Page 10: 9/10/11/12/13/14/15/16

This is my code so far…

if($page == ceil($NumOfPages) && $page != 1){
    for($i = 1; $i <= ceil($NumOfPages)-1; $i++){
    if($i > 0){
        echo "<a href=\"/{$i}\">{$i}</a>";
    }
    }
}
if ($page == ceil($NumOfPages) ) {
    $startPage = $page;
}else{
    $startPage = 1;
}
for ($i = $startPage; $i <= $page+6; $i++){
        if ($i <= ceil($NumOfPages)){
            if($i == $page) {
                echo "<a href='/page/$i/' title='View movies page $i' id='pagelisel'>$i</a> ";
            }else{
                echo "<a href='/page/$i/' title='View movies page $i' id='pageli'>$i</a> ";
            }
        }
}

Any help would be greatly appreciated,

Thanks!

  • 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-24T11:27:28+00:00Added an answer on May 24, 2026 at 11:27 am

    I assumed that (partly for myself… 😉 ):

    • $page is the selected page
    • $startPage is the first page number you want to show
    • $numPages is already ceil-ed

    First you need to find $startPage. Depending whether $page is the first one (ie has the value one 1, another assumption) or not. Your check is slightly off, as it check if it is equal to the last page.

    if($page == 1) {
        $startPage = 1;
    } else {
        $startPage = $page - 1;
    }
    

    Then you need to find out the last page number you want to print ($lastPage). So check if $startPage is near the end and set ~$lastPage` accordingly:

    if($startPage + 7 > $numPages) {
        $endPage = $numPages;
    } else {
        $endPage = $startPage + 7;
    }
    

    Finally, use you for-loop which seem ok, but loop from $startPage to $endPage.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.