Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Viewing Data!

sweeney1sweeney1 Posts: 7Member
hi people, hows all doing? Iam wondering, if you had a large number of items to be viewed e.g messages, and you dont want to put them all in one page dose anyone know a good way of puting them into different pages? ie if i had 50 messages but only wanted to view 10 and put 10 in another 5 pages. If you understand that then your fantasitc LMAO if you want me to explane it better just ask!

Thannks For Reading This Message And Hope To Hear From You Soon!

sweeney1

Comments

  • tvientitvienti Posts: 230Member
    What you're looking to do is called pagination (breaking results up into pages). I generally use something along these lines:

    [code]
    // Assuming an existing array called $records you want to page through
    $TotalRecords = count($records);
    $PerPage = 10; // num to display per page
    $TotalPages = $TotalRecords / $PerPage;
    if ($TotalRecords % $PerPage) $TotalPages++;
    $Page = $_GET['pg']; // the page we want to view

    // Make sure page is w/in boundaries
    if ($Page < 1) $Page = 1;
    if ($Page > $TotalPages) $Page = $TotalPages;

    // Find the index of the first record for this page
    $Start = ($Page * $PerPage) - 1;
    $Slice = array_splice($Start, $PerPage, $records);

    // At this point, Slice should contain only the portion of records that
    // you want for this page.
    foreach ($Slice as $s)
    {
    // do whatever it is you're doing with the records
    }

    // Finally, display page navigation
    for ($p = 1; $p <= $TotalPages; $p++)
    {
    // Only display the link if it's not the current page
    if ($p == $Page) echo $p;
    else echo '<a href="something.php?pg='. $p .'">'. $p .'';
    }
    [/code]

    This is whipped up off the top of my head based off code I've used before, but not tested or anything like that so expect errors :) That should give you the general idea. Also, it's somewhat inefficient since, according to the assumption at the top of my code sample, there's an array $records containing all of the records you want to paginate.

    What would be better is if you could find out your starting index BEFORE reading in the array, and instead of array_splice'ing out what you need, only read in what you need to begin with. For example, if you're displaying results from a MySQL database, you can work out which records you want and plug this information directly into the query's LIMIT clause to only get the $PerPage records you want.

    Hope this helps.

    T

    : hi people, hows all doing? Iam wondering, if you had a large number of items to be viewed e.g messages, and you dont want to put them all in one page dose anyone know a good way of puting them into different pages? ie if i had 50 messages but only wanted to view 10 and put 10 in another 5 pages. If you understand that then your fantasitc LMAO if you want me to explane it better just ask!
    :
    : Thannks For Reading This Message And Hope To Hear From You Soon!
    :
    : sweeney1
    :

Sign In or Register to comment.