TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Pagination (http://www.talkphp.com/advanced-php-programming/4548-pagination.html)

Randy 06-14-2009 03:09 AM

Pagination
 
So i know this has been asked a billion times (maybe a trillion but idk), but i tried google, tutorial sites, the search feature here and nothing seems to be working for me i keep getting errors or its not what i need so here i come asking for help again.

I have this to display my data as it is designed to function as a forum but im calling it 'admin notes' in my script.
PHP Code:

            <?php
            
while($rows mysql_fetch_array($admin_forum_result)) {
            
$dateformat "M j, Y";
            
?>
            <tr>
                <td class="table_check"><input type="checkbox" class="noborder" /></td>
                <td class="table_date"><? echo date($dateformat ,$rows['datetime']); ?></td>
                <td class="table_title"><? echo $rows['topic']); ?></td>
                <td><? echo $rows['category']); ?></td>
                <td><? echo $rows['priority']); ?></td>
                <td><? echo $rows['username']); ?></td>
            </tr>
            
            <?php ?>

I want to use pagination to display 5 rows (topics) per page..

My database looks like this:
DB NAME = staff
TABLE NAME = adminforum_question
ROWS = id, topic, message, name, datetime, priority, category

Every tutorial i read kept giving me errors and i got frustrated figuring it out so.

Any help is appreciated.

codefreek 06-14-2009 04:16 AM

You should take a look at this, i know some people who this has helped very much including me, also it's basic + it's a video it's great. Check it out if you don't understand it, let us know but watch it at least 2,3 times.. it takes a while for it to sink in to the head/brain but it will as everything does ;)

PHP Pagination - Part 1

-Cf

Randy 06-14-2009 05:16 AM

Thanks abunch mate! worked perfectly while making me laugh at the same time. Did everything i needed just on part 1 not sure if part 2 does anything more but im going to watch it. thanks.

codefreek 06-14-2009 07:06 AM

np mate ;)

Randy 06-14-2009 07:28 AM

Alright read that got a question:

I am trying to make it display all page numbers without doing the "..." things and I cant seem to figure out how I would go about doing that lol.

My code is pretty much exactly the same as his, but his code seems quite messy in my opinion.

Any thoughts?

PHP Code:

            <?php
            
            
// define variables
            
$pageNumber = (isset($_GET["page"]) && is_numeric($_GET["page"])) ? $_GET["page"] : 1;
            
            
// establish number per page
            
$perPage 5;
            
            
// establish padding value
            
$padding 2;
            
            
// get start index of results
            
$startIndex = ($pageNumber $perPage) - $perPage;
            
            
// get total number of database entries
            
$totalCount "SELECT COUNT(*) as 'Total' FROM adminforum_question";
            
$rsCount mysql_query($totalCount) or die(mysql_error());
            
$rowCount mysql_fetch_object($rsCount);
            
            
// show page listings
            
echo "<div class=\"pagination\">";
            
                
// get total number of pages
                
$numOfPages ceil($rowCount->Total $perPage);
                
                
// print link to first page
                
if (($pageNumber != 0) && ($PageNumber != 1) && ($pageNumber != $numOfPages)) {
                }
                
?>
                
                <span class="pages">Page <?php echo $pageNumber ?> of <?php echo $numOfPages ?> </span>
                
                <?php
                
// if our current page, minus padding, greater tthen 1
                
if (($pageNumber $padding) > 1) {
                    echo 
"... ";
                        
// set lower limit
                        
$lowerLimit $pageNumber $padding;
                        
// print all padded numbers between lowerlimit and current page
                        
for($i $lowerLimit$i $pageNumber$i++) {
                            echo 
"<a href=\"index.php?page=".$i."\">".$i."</a> ";
                        }
                } else {
                    
// print all numbers between current page and first page
                    
for($i 2$i $pageNumber$i++) {
                        echo 
"<a href=\"index.php?page=".$i."\">".$i."</a> ";
                    }
                }
                
                
// if were not on first page, or last page, print current page
                
if (($pageNumber != 0) && ($PageNumber != 1) && ($pageNumber != $numOfPages)) {
                    echo 
"<b> - " $pageNumber ." - </b>";    
                }
                
                
// if current page, plus padding, less then total number of pages
                
if(($pageNumber $padding) < $numOfPages) {
                    
//  set upper limit
                    
$upperLimit $pageNumber $padding;
                    
// print all numbers from padding pages above current page
                    
for($i = ($upperLimit +1); $i $pageNumber$i++) {
                        echo 
"<a href=\"index.php?page=".$i."\">".$i."</a> ";
                    }
                    echo 
"... ";
                } else {
                    
// print all page numbers between number of pages and current page
                    
for($i = ($pageNumber 1); $i $numOfPages$i++) {
                        echo 
"<a href=\"index.php?page=".$i."\">".$i."</a> ";
                    }
                }

            echo 
"</div>";
?>

Basically I want it to look like this:

Tanax 06-14-2009 09:41 AM

http://www.talkphp.com/script-giveaw...ion-class.html

codefreek 06-14-2009 05:48 PM

Randy: just take out the echo "... ";
knight13: i am glad that it helped ;)

Randy 06-14-2009 07:24 PM

I figured that but there is a problem because those echo's don't even seem to show up... i just get the 'Page 1 of 3' nothing else works.

codefreek 06-14-2009 09:26 PM

You should take a look at Tanax's Pagination class as well..

Randy 06-15-2009 01:28 AM

Watched it for the 5th time and i did my styling while following along instead of after and its working, i guess i was just getting confused with his coding style mixed with mine.

SCREENSHOT OF MY WORK

Randy 06-15-2009 03:14 PM

I may be wrong so dont quote me on it but i believe that is a basic php function where it reads something in the url. i know its a long shot but its all i got.

Randy 06-15-2009 04:59 PM

nah the $_GET[''] gets something from the url if its not defined by a form, but i believe 'page' is what retrieves a number from the end.

Randy 06-15-2009 05:08 PM

from the url but here is what your looking for:

Your URL is like this correct:
index.php?page=9

by putting $_GET['page'] you are telling it to put page there so change page to whatever you want to be there such as id, number, etc.. and then it reads the number after page=.

Randy 06-16-2009 05:32 AM

no problem lol honestly took me about 15 mins reading that line over and over to figure it out, sorry if its not the exact explanation your looking for, im not the best at explaining so I did best I could.

rguy84 06-16-2009 07:07 AM

Quote:

Originally Posted by knight13 (Post 25436)
PHP Code:

$PageNumber = (isset($_GET['page'])&& is_numeric($_GET['page'])) ? $_GET['paget'] : 1


Without looking at the code, it seems as though it needs a counter.


All times are GMT. The time now is 05:51 AM.

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