Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-23-2008, 04:05 AM help: paginationg
Novice Talker

Posts: 8
Name: Allan
Trades: 0
im having a problem with paginating.. can somebody give me a code that works: with a blog showing only 4 post and having a pagination at the last part... pls.. thanks for the help.. pls. complete code im just a noob here. thank you again

Last edited by wheay; 03-23-2008 at 04:09 AM..
wheay is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-24-2008, 03:35 PM Re: help: paginationg
Novice Talker

Posts: 7
Trades: 0
Sure, here's a download link to my pagination script. I've used in numerous websites that I've done before, and it works fine!

Code:
    $msql = "SELECT * FROM "; // The SQL of whatever your trying to paginate
    $mresult = mysql_query($msql);
    $num_records = mysql_num_rows($mresult);
    
    $limit = $_GET['l'];
    $page = $_GET['p'];
    
    if(!isset($_GET['l']) or !is_numeric($_GET['l'])) {
        $limit = 5;
    }
    
    if(!isset($_GET['p']) or !is_numeric($_GET['l'])) {
        $page = 1;
    }
    
    $total_pages = ceil($num_records / $limit);
    $set_limit = $page * $limit - ($limit);
    
    $finalsql = $msql." LIMIT $set_limit, $limit";
    
    $q = mysql_query($finalsql);
    
    $num_records = mysql_num_rows(mysql_query($finalsql));
    
    if($num_records <= 0) {
    echo '<b>No members were found in the database.</b>';
    } else {
    while($mrow = mysql_fetch_array($q)) {
    print("{$mrow['first_name']} {$mrow['last_name']}<br />e-Mail: <a href=\"mailto:{$mrow['email']}\">{$mrow['email']}</a><br /><a href=\"deletemember.php?mid={$mrow['member_id']}\"><b>Delete Member</b></a><br /><br />");
    }
# PRINT OUT ALL OF THE NEXT AND PREVIOUS PAGES
    echo'
    <div class="pages">';
    $prev_page = $page - 1;
                
    if($prev_page >= 1) { 
    echo("<a href=viewmembers.php?l=$limit&p=$prev_page><b><<</b></a>"); 
    } 
        
    for($a = 1; $a <= $total_pages; $a++) {
        if($a == $page) {
        echo("<b> $a</b> "); //no link
        } else {
         echo("<a href=viewmembers.php?l=$limit&p=$a> $a </a> ");
    } 
    } 
        
    $next_page = $page + 1;
                
    if($next_page <= $total_pages) {
    echo("<a href=viewmembers.php?l=$limit&p=$next_page><b>>></b></a>"); 
    } 
    echo'</div>';

        }
and that should get you what you want
Raphy is offline
Reply With Quote
View Public Profile
 
Old 03-25-2008, 08:51 AM Re: help: paginationg
Novice Talker

Posts: 8
Name: Allan
Trades: 0
wow! it works, but now i have another problem i need to post my blog last in, first out. or just like if i post a new message it will be the first to display. its a while statement but i cant put it in the code.. can you help me with this.

thank you so much..
wheay is offline
Reply With Quote
View Public Profile
 
Old 03-25-2008, 11:12 AM Re: help: paginationg
Novice Talker

Posts: 7
Trades: 0
Yep, all you have to do is make sure that your posts id in your database are set to integer, primary key, and auto increment. Once that's done, you just modify the $msql like so -

$msql = "SELECT * FROM posts ORDER BY post_id DESC "; // The SQL of whatever your trying to paginate

Just configure the above SQL to work as you would like it.

All the best.
Raphy is offline
Reply With Quote
View Public Profile
 
Old 03-25-2008, 11:24 AM Re: help: paginationg
Novice Talker

Posts: 8
Name: Allan
Trades: 0
Quote:
Originally Posted by Raphy View Post
Yep, all you have to do is make sure that your posts id in your database are set to integer, primary key, and auto increment. Once that's done, you just modify the $msql like so -

$msql = "SELECT * FROM posts ORDER BY post_id DESC "; // The SQL of whatever your trying to paginate

Just configure the above SQL to work as you would like it.

All the best.

yup! i get it. thanks. i was thinking about it also.
thanks for the help.
__________________
im a noob so pls. help me.. thanks
wheay is offline
Reply With Quote
View Public Profile
 
Old 03-26-2008, 05:04 AM Re: help: paginationg
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
To obtain total number of records to paginate you should "select count(*) ..." instead of "select * ..." because if you have any reasonable amount of records in your table and if row length is big enough your script will simply fail with "out of memory" error. Anyway there is absolutely no need to select the whole table only to count number of rows, let the specialized software (mysql server in this case) to count rows for you.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to help: paginationg
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.17669 seconds with 12 queries