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

  • Home
  • SEARCH
  • 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 6743885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:01:01+00:00 2026-05-26T12:01:01+00:00

I have two tiny little problems; 1 . I want to add a previous

  • 0

I have two tiny little problems;

1 . I want to add a previous / next button into this code
2 . I want it to only show like max 10 links between previous and next. So if i have 50 numbers/links it will only show 10 of them and not 50 links on the page.

I have searched on the clo

The code works, only need that two options in it.
Can someone help me out? Thank you !

 <?php 

        include 'includes/connection.php';  
        $per_page = 8;

        $pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
        $pages = ceil(mysql_result($pages_query, 0) / $per_page);

        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
        $start = ($page - 1) * $per_page;



        $query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
        while ($query_row = mysql_fetch_assoc($query)) {
            echo '<p>', $query_row['name'] ,'</p>';
        }

        if ($pages >= 1 && $page <= $pages) {
            for ($x=1; $x<=$pages; $x++) { 
                //echo $x, ' '; 

                echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
            }
        }else{
            header("location:index.php?page=1");
        }


    ?>
  • 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-26T12:01:02+00:00Added an answer on May 26, 2026 at 12:01 pm

    First of all, you should, just for good practice, put an “exit;” after the header() call at the end. It doesn’t make a difference in this particular script, but keep in mind that any code following a header(“Location: …”) call WILL be executed before redirection.

    Now to your question, try this (UPDATE: This code has been tested and works.)

    <?php 
    include 'includes/connection.php';  
    $per_page = 8;
    $pages_query = mysql_query("SELECT COUNT(`id`) FROM `products`");
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
    
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;
    
    $query = mysql_query("SELECT `name` FROM `products` LIMIT $start, $per_page");
    while ($query_row = mysql_fetch_assoc($query))
    {
        echo '<p>' . $query_row['name'] . '</p>';
    }
    
    // If the requested page is less than 1 or more than the total number of pages
    // redirect to the first page
    if($pages < 1 || $page > $pages)
    {
        header('Location: ?page=1');
        // end execution of the rest of this script
        // it will restart execution after redirection
        exit;   
    }
    // If more than one page, show pagination links
    if($pages > 1)
    {
        $html = array();
        $html[] = '<strong>';
        // if you're on a page greater than 1, show a previous link
        $html[] = (($page > 1) ? '<a href="?page=' . ($page - 1) . '">Previous</a> ' : '');
        // First page link
        $pageFirst = '<a href="?page=1">1</a>';
        $html[] = (($page == 1) ? "</strong>{$pageFirst}<strong>" : $pageFirst);
    
        if ($pages > 6)
        {
            $start_cnt = min(max(1, $page - (6 - 1)), $pages - 6);
            $end_cnt = max(min($pages, $page + 4), 8);
    
            $html[] = ($start_cnt > 1) ? '...' : ' ';
    
            for ($i = $start_cnt + 1; $i < $end_cnt; $i++)
            {
                $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>';
                if ($i < $end_cnt - 1)
                {
                    $html[] = ' ';
                }
            }
    
            $html []= ($end_cnt < $pages) ? '...' : ' ';
        }
        else
        {
            $html[] = ' ';
    
            for ($i = 2; $i < $pages; $i++)
            {
                $html[] = ($i == $page) ? '</strong><a href="?page=' . $i . '">' . $i . '</a><strong>' : '<a href="?page=' . $i . '">' . $i . '</a>';
                if ($i < $pages)
                {
                    $html[] = ' ';
                }
            }
        }
        // last page link
        $pageLast = '<a href="?page=' . $pages . '">' . $pages . '</a>';
        $html[] = (($page == $pages) ? "</strong>{$pageLast}<strong>" : $pageLast);
        // Show next page link if you're on a page less than the total number of pages
        $html[] = ($page < $pages) ? ' <a href="?page=' . ($page + 1) . '">Next</a>' : '';
        // If you're not on the last page, show a next link
        $html[] = '</strong>';
    }
    else
    {
         // show page number 1, no link.
        $html[] = '<strong>1</strong>';
    }
    echo implode('', $html);
    

    Also note that the final ?> is not required in PHP files that do not have HTML code following the PHP code, so I left it off.

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

Sidebar

Related Questions

Okay so I have two import pieces of code involved in this. This first
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
I have two spreadsheets... when one gets modified in a certain way I want
I have this application that consists of two phases. Queuing phase and chatting Phase.
Have two fields start and end, both are dates, I want to write a
I have two Javascript scripts on a site. One is an accordion (show/hide) and
I am working on a small project, and am having two tiny problems with
I have two machines in the same subnet. I want to exchange objects between
Imagine you have two tables, with a one to many relationship. For this example,

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.