TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   Arrays + Pages (http://www.talkphp.com/script-giveaway/2668-arrays-pages.html)

blayne4k 04-20-2008 10:35 PM

Arrays + Pages
 
Here is a script that converts arrays into pages, very customizable.

PHP Code:

<?PHP
//array paging system
//how many do you want per page?
$perpage 30;

//query to make the array, here im doing a query to select all the users
$query mysql_query("SELECT * FROM users");
while(
$row=mysql_fetch_assoc($query)) {
    
//set all users in an array
    
$users[] = '<a href="'.$row[homepage].'">'.$row[username].'</a>';
}

//I need the total amount of rows I have
$total mysql_num_rows($query);

/*
    how many pages are there?
    $pages = $total / $perpage
    thats "about" how many pages but if there are 323 users then we have:
    323 / 30 = 10.7666667
    so we obviously need to make that 11 not 10 so...
    this gets the number 10.76 and adds 1 to it, so its 11.76, then explodes it so [0] = 11; and [1] = 76; and 11 is the amount of pages we want.
*/
$pages explode('.', (($total/$perpage)+1));
$pages $pages[0];

//I need to find out what page im on
$pageid ereg_replace('[^0-9]'''$_GET['pageid']);
//if page is not set it needs to be one
if($pageid=='') { $pageid 1; }

//so if pageid = 1, we need to display the array numbers 0-29, pageid 2 would be 30-59 and so on..
$start = ($pageid $perpage)-$perpage// (1*30)-30=0; (2*30)-30=30;
$end $start + ($perpage 1);    // 0 + (30-1) = 29; 30+(30-1)=59;

//echo the output
while ($start <= $end) {
    echo 
$users[$start] . '<br>';
    
$start++;
}

//what about pages?
$i 1;
//do a while loop from 1 to how many pages you want
while($i <= $pages) {
    if(
$i == $pageid) {
        
//if this page is the page your on you wont wanna link it again cause there already on that page
        
$pagess .= ' ['.$i.'] ';
    } else {
        
$pagess .= ' <a href="?pageid='.$i.'">['.$i.']</a> ';
    }
    
$i++;
}
echo 
$pagess;
?>

This actual script is untested but the concept I used on an old project.


All times are GMT. The time now is 03:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0