Results 1 to 7 of 7
  1. #1
    Internet Cowboy
    Join Date
    January 18th, 2005
    Posts
    4,662
    Page numbers. Need help please.
    OK, I am not a programmer. My brother is doing some php scripting for me and I have a question for you whiz's.

    He is making a script to display results from a MySQL database. When a 'tight' term is entered, it returns, say, 8 - 10 pages of results. It is all set up to put page numbers atop the page with a Next and Previous link. That works fine.

    When a broad term is entered, the script might show 200 pages of results across the top, which ends up making the page REALLY wide and creating an awful horizontal scroll.

    We need to be able to display 10 pages and when someone clicks on, say, page 6, it them displays links to pages 6 - 16 across the top and so on, always displaying 10 page links with the Next and Previous links there.

    Can someone post a snippet that would help him here or enlighten us to the theory of how this is done?

    Many thanks in advance!


  2. #2
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    1. Set up some PHP variables that indicate how many rows and columns you want per page. Let's say 4x4 = 16 items per page.

    2. When you initially display the catalog you don't have a page number. Assume page one and display items 1 - 16.

    3. You can use the number 16 and the total number of items you have (200) to figure how many pages. In this case 12.5 or really 13 pages. If the result of the math (200/16=12.5) does not equal the integer portion (12) then you bump the number of needed pages to the the integer portion (12) plus 1 = 13. Anything over 12 (12.0001) means thirteen pages.

    4. Now that you know how many pages you can build a navigation bar for the top. For each number build a URL that links back to the same page with a parm '$page=' . $x. $x is a loop variable that you loop through and increase for the number of pages you want. So now we have a bar that has 1 2 3 4 5 6 7 8 9 10 .... Each number is a link.

    5. When you click on one of those numbers you will get sent back to the same page with a page number parm. http://mypage.com/home.php?page=4. Since you know it is page 4 and you know how many items per page (16) you have an approximate idea where to start in your list. 16 * (page-1) = 16*3 = 48. To show everything on page 4 you will rebuild your page display products 48 through 48+16 (15 really. You want 16 products starting at 48. Start counting from +0 (48) to +15 (63)). So you want to display products 48 - 63 on page 4.

    6. For the page forward/backwards arrows you want to take the current page that you are building and subtract one for the left arrow and add one for the right arrow. Make sure that you don't go less than page 1 or greater than the total number of pages you need. Best bet is to disable those arrows when they are not needed i.e. at page one you don't need a left/previous arrow. Display the arrow but don't make it a hyperlink.

    Help?

    Patrick

  3. #3
    ABW Ambassador Greg Rice's Avatar
    Join Date
    January 18th, 2005
    Location
    Ohio
    Posts
    4,889
    I have some code I use and put it in an include file and place one at the top and one at the bottom of the page. If you're interested I can email or PM the code to you, just let me know.
    Greg Rice Affiliate Program Management
    www.gocmc.com info(AT)gocmc.com | 330-259-1223

    Join us! - MiNeeds.com | DiscountCandleShop/CheeseSupply | Feng Shui Plaza

  4. #4
    ABW Ambassador PatrickAllmond's Avatar
    Join Date
    September 20th, 2005
    Location
    OKC
    Posts
    1,219
    Greg,

    How does the code in your include file know how many items are in the database ?

    Patrick

  5. #5
    ABW Ambassador Greg Rice's Avatar
    Join Date
    January 18th, 2005
    Location
    Ohio
    Posts
    4,889
    The code doesn't know, it just keeps adding page numbers until there are no more results. I use Dreamweaver and when I display the results, for example the product name, I select "Repeat Region" from the "Behaviors" tab and select how many records to display per page. It's set to display 5 numbers before and after the current page like this |<< << 1 2 [3] 4 5 6 7 8 >> >>|. Does that make sense?
    Greg Rice Affiliate Program Management
    www.gocmc.com info(AT)gocmc.com | 330-259-1223

    Join us! - MiNeeds.com | DiscountCandleShop/CheeseSupply | Feng Shui Plaza

  6. #6
    Member SeanW's Avatar
    Join Date
    July 28th, 2005
    Posts
    69
    Quote Originally Posted by UncleScooter
    Can someone post a snippet that would help him here or enlighten us to the theory of how this is done?
    I'm thinking something along the lines of

    // The parameters
    $maxPagesToShow = 10;


    // do the query. $numPages is the number of pages, $curPage is the page you're on

    if ($numPages <= $maxPagesToShow) {
    // Easy case
    $startPage = 1; $endPage = $numPages;
    } else {
    // Limit the number of pages to display
    $startPage = $curPage;
    $endPage = $startPage + $maxPagesToShow;
    // Clamp to numPages
    if ($endPage > $numPages) { $endPage = $numPages; }
    }

    // spit out the pages from $startPage to $endPage
    ...

    Sean

  7. #7
    Internet Cowboy
    Join Date
    January 18th, 2005
    Posts
    4,662
    Thanks all for your help.


  8. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Discrepancy: Google Numbers vs. ShareASale Numbers
    By shocker in forum ShareASale - SAS
    Replies: 1
    Last Post: April 24th, 2011, 06:58 PM
  2. How to show index numbers on index page?
    By sinpeople in forum WebMerge (Fourthworld.com)
    Replies: 0
    Last Post: June 24th, 2007, 12:06 AM
  3. Page numbers at bottom of Amazon script?
    By Tyranny in forum Cusimano.com Scripts
    Replies: 2
    Last Post: August 21st, 2003, 12:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •