PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1848
Number of posts: 5016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Viewing Data! Posted by sweeney1 on 1 Feb 2006 at 7:57 AM
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
Report
Re: Viewing Data! Posted by tvienti on 1 Feb 2006 at 8:17 AM
What you're looking to do is called pagination (breaking results up into pages). I generally use something along these lines:

// 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 .'</a>';
}


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
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.