php pagination question...for loop...need expert opinion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

php pagination question...for loop...need expert opinion

Post by scarface222 »

Hey guys I am using a Jquery pagination system to display page results based on my mysql query. I have a simple problem, and I have not used 'for' loops enough that I know what to do.

to display my pages I used

<ul id="pagination">

Code: Select all

for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
    }
</ul>

Where $i is the page number

There is an obvious problem, that is that every page is listed which is more that the user wants to see. I would like to limit the results to 4 results on each side of the page number you are on. I have tried $i<=$i+4 as a condition to limit one side of the results but that did not work. Anyone have an idea on how to approach this? Can php solve this or is this a javascript problem?

This is the javascript to give you an idea of how it works

Code: Select all

$(document).ready(function(){
        
    //Display Loading Image
    function Display_Load()
    {
        $("#loading").fadeIn(900,0);
        $("#loading").html("<img src='pagination/bigLoader.gif' />");
    }
    //Hide Loading Image
    function Hide_Load()
    {
        $("#loading").fadeOut('slow');
    };
    
 
   //Default Starting Page Results
   
    $("#pagination li:first").css({'color' : '#FF0084'}).css({'border' : 'none'});
    
    Display_Load();
    
    $("#content").load("pagination/pagination_data.php?page=1", Hide_Load());
 
 
 
    //Pagination Click
    $("#pagination li").click(function(){
            
        Display_Load();
        
        //CSS Styles
        $("#pagination li")
        .css({'border' : 'solid #dddddd 1px'})
        .css({'color' : '#0063DC'});
        
        $(this)
        .css({'color' : '#FF0084'})
        .css({'border' : 'none'});
 
        //Loading Data
        var pageNum = this.id;
        
        $("#content").load("pagination/pagination_data.php?page=" + pageNum, Hide_Load());
    });
    
    
});
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php pagination question...for loop...need expert opinion

Post by AbraCadaver »

You're sending page as a get var so you need to retrieve this and work out how many pages to show (4 on either side). I assume $pages is the total number of pages?

Code: Select all

$page = $_GET['page'];
 
// set start to the current page minus 4 unless it is less than one then use 1 
if(($start = $page-4) < 1) {
    $start = 1;
}
// set end to the current page plus 4 unless it is greater than the total number of pages then use pages
if(($end = $page+4) > $pages) {
    $end = $pages;
}
 
for($i=$start; $i<=$end; $i++) {
    echo '<li id="'.$i.'">'.$i.'</li>';
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: php pagination question...for loop...need expert opinion

Post by scarface222 »

Thanks for the response as usual man. While your answer did not completely solve the problem, it did lead me to the root of it. The problem was that I was a little confused with how the jquery function was working, but when you mentioned the get url aspect it made sense to me. 'Get' will not work on the index page since the link that was being loaded in the jquery function was pagination_data.php?page=... so the answer is to use javascript and echo the page numbers by using php to supply the necessary data.

I have to somehow translate the for loop into javascript with php added values. As you can see the page value is stored in the javascript printed above. I am having some trouble, first I cannot get javascript to print anything. I tried with no luckI

document.getElementById('pagination').innerHTML="<li>testing</li>";

I know this is javascript but If you have an idea, please let me know, it will save me hours lol. I suppose I will be learning but in this case I don't really want to.

Thanks again abra, always appreciate your advice
scarface222
Forum Contributor
Posts: 354
Joined: Thu Mar 26, 2009 8:16 pm

Re: php pagination question solved

Post by scarface222 »

Ok nevermind I was gonna use a jquery pagination, but I forgot that then ads do not get refreshed, so I am cancelling those plans
Post Reply