1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Navigation

Discussion in 'PHP' started by vijaykoul, Oct 13, 2005.

  1. #1
    Hi frnds,

    I want to have the code for PHP Navigation. Can anybody give me the code for it. I want navigaition as is in google.
    e.g.
    Previous 1 2 3 4 5 Next.

    will be highly thankful
     
    vijaykoul, Oct 13, 2005 IP
  2. boccio

    boccio Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    gosh... suppose you didn't try "pagination" search on google? anyhow, here's how it's done in a nuttshall, hope you'll get the picture and write the code for pagination yourself from now on:

    OK, you have some recordset you need to paginate. Naturally, first you have to retrieve # of total records using:

    SELECT count(*) FROM, or mysql_num_rows() ... and extract total number of records, let's say the varialbe is called $totalRecords. Then, define number of results for each page, let's call that $resultsPerPage, and assign some initial value to it. So far, the code might look like:
    $totalRecords = mysql_num_rows($some_result_set);
    $resultsPerPage = 20;
    PHP:
    Next thing we have one trivial thing - to find out how many pages will we have, so we need to divide total results and number of results per page. With given results we make a loop to echo out the pagination pages.
    
    //find out total number of pages
    $totalPages = ceil($totalRecords / $resultsPerPage);
    
    //loop thru and echo the links
    for ($i = 1; $i <= $totalPages; $i++)
       echo "<a href='".$_SERVER['PHP_SELF']."?page=".$i."'>$i</a> ";
    
    PHP:
    OK, so we have pagination now. Next thing is to display proper results once someone clicks on "Page 3", for example. The whole point is using LIMIT patameter to SELECT query to point the cursor to proper starting point on resultset. Pick up GET with "page" paremeter and calculate exact record to start with.
    
    // multiply paging with results per page.
    $fromResult = ($_GET['page']-1) * $resultsPerPage;
    
    // make the query
    SELECT * FROM ... LIMIT $fromResult, $resultsPerPage
    
    PHP:
    And that would be it... this is only basic pagination, you can make things much more complicated than this, but hopefully you'll grasp the idea.

    Hope this helped
     
    boccio, Oct 13, 2005 IP