Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 3 123 LastLast
Results 1 to 15 of 36
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Creating PHP Pagination

    Hello,

    I'm stuck on creating a php pagination script that I want. I've tried loops, etc, and it's not doing the job.. Here's my code for my database that gets what I have now using previous and back links

    Code:
     <? 
    Def($start,0);
     Def($limit,20);
    //Calculate total
     $db->open("SELECT count(*) as cnt FROM songlist $where ");
     $row = $db->row();
     $cnt = $row["cnt"];
     
     //Now grab a section of that
    //ORDER BY artist ASC, title ASC
     $db->open("SELECT * FROM songlist $where", $limit, $start);
     
     $first = $start+1;
     $last  = min($cnt,$start+$limit);
     $rc    = $start;?>
    I am trying to create pagination like on the bottom of this site: http://www.lite945.com/iplaylist/playlist.html

    Notice how currently the 1-20 is bold, and if you click on another number say 21-40 and on that it goes bold. That's exactly what I am trying to do.

    I already have my SQL statements to. It get's the $rc fine on my page I have, where it shows 1-20 on the rows, but I am unable to figure out how to do the bottom pagination style. I've been trying this for a long time and can't figure it out for the life of me. Any help would greatly be appreciated! Thanks!

  • #2
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    What if you just have a get value for page number, and then:

    PHP Code:
    $pgnum $_GET["pg"];

    $first = ($pgnum 1) * $limit

    for(
    $count 0$count $limit && $count $cnt$count++)
    {
        
    $current $first $count;
        
    //Post the item using $current variable.

    Of course you will need to code the page buttons etc. but hopefully this is a start.
    Last edited by KULP; 11-28-2012 at 05:32 AM.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I was actually looking to do the range like that at the bottom.. No buttons.. Just the 1-20, etc and if on that page.. say start= 20 then 21-40 would be bold with no <a href="> tag

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Also I have a get value called start. So it's like playlist.php?start=0. If start is 0 1-20 songs will show and so forth. I can't seem to figure this pagination out. Can ya help with that? I can't get it to go 1-20 21-40 41-60 and if my start is = $start then that number is bold.

    I do want to say I am wanting the links like so:

    1-20 21-40 41-60 etc all dependent what is in the database of results, which is what the cnt, etc are already there for.
    Last edited by mfoland; 11-27-2012 at 06:54 AM.

  • #5
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    Hi, well hopefully this will make your life a whole lot easier. One of my scripts uses

    http://developersbench.blogspot.com/...on-script.html (the download link is below the overview at the bottom)

    But honestly although i like the way it does pagination data i did not like the output so what i do is i load the pagination data as normal,

    then i grab those vars from the ps pagination script and did my own deal.

    Im not saying you have to use ps pagination at all, its just what was in my script and i didnt want to rip it out so i just share the duty with my new way.

    Here is the basic process so i hope it helps you. You can also set the range on either side of the current page.

    first you do your normal query then you can see where i use the ps pagination to and where i xfer the values to my own deal.

    It works perfectly for me.

    PHP Code:

     $query
    ="did my query here";
     
    $pager = new PS_Pagination($link,$query,$record_per_page,5);
     
    $rs $pager->paginate();



    //#############  added for page number display

    // grab the values for my own page display code

    $maxpg $pager->max_pages;

    $curpg $pager->page;

    $rppg =  $pager->rows_per_page;

    $off_setpg $pager->offset;



    function 
    pag_links() {

      global 
    $curpg$maxpg;


      
    /******  build the pagination links ******/

      // range of num links to show on either side of current page

      
    $range 5;

     
      
      
    // show $range pages on either side of current page. if this includes page

      // 1, don't show "First". if this includes last page, don't show "Last"

      // show back links |< and < unless at page 1

      
    if ($curpg 1) {  

        if (
    $curpg $range 1) { // wouldn't show page 1 in range

          // show << link to go back to page 1

          
    echo "<a href='{$_SERVER['PHP_SELF']}?page=1'>|&lsaquo;</a> ";

        }

        
    // get previous page num   

        
    $prevpage $curpg 1;   

        
    // show < link to go back 1 page

        
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>&lsaquo;</a> ";

      } 



      
    // loop to show links to range of pages around current page

      // if previous page <, there is a space after it



      
    for ($x $curpg $range

           
    $x $curpg $range 1

           
    $x++) {   

        
    // if it's a valid page number (not < 1 or > last page)...  

        
    if ($x && $x <= $maxpg) {      

          
    // if we're on current page, not a link...      

          
    if ($x == $curpg) { 

            
    // 'highlight' it but don't make a link

            
    echo "[<b>$x</b>] "

          } else { 

            
    // if not current page, make it a link

            
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> "

          }

        } 

      } 

      

      
    // if not on last page, show forward > and last page >| links 

      
    if ($curpg $maxpg) {   

        
    // get next page   

        
    $nextpage $curpg 1;    

        
    // echo forward link for next page    

        
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>&rsaquo;</a> ";

        
    // echo forward link for lastpage, unless last page already showing

        
    if ($curpg $range $maxpg) {

          echo 
    " <a href='{$_SERVER['PHP_SELF']}?page=$maxpg'>&rsaquo;|</a>";

        }

      }

      
      
    /****** end build pagination links ******/

    // end pag_links() 
    and then when you want to call the function on your page just do this



    PHP Code:

    <td colspan="4" align="center">
    <?php
    if(mysql_num_rows($rs)>0)
    {
     
    pag_links();

    ?>

    </td>
    Hope that helps ya..

  • Users who have thanked durangod for this post:

    mfoland (11-27-2012)

  • #6
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    That looks like something I've used. I've done the previous and next, but I really was wanting the text links like on: http://www.lite945.com/iplaylist/playlist.html at the bottom..

    How hard is that? I've tried to do the loop, but no go.. It wants to just echo random stuff haha.

  • #7
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    If you can help me build what i'm wanting, i'll be more than glad to link to you or whatever

  • #8
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,341
    Thanks
    260
    Thanked 32 Times in 31 Posts
    well you have the data youll just have to change the out put. If you can give me a few days ill do some testing, im in the middle of a project and my brain power is neck deep in it. But ill be glad to play around with it and get the output changed if you can give me a few days.

    If not if anyone can assist would be great, ill check back in a few days and see your status. Im suppose to have this deal done by thursday for my friend.

  • #9
    New Coder
    Join Date
    Jan 2010
    Posts
    68
    Thanks
    3
    Thanked 5 Times in 4 Posts

  • #10
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'm wanting the style that goes 1-20 21-40, etc as a pagination like on my link before

  • #11
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by durangod View Post
    well you have the data youll just have to change the out put. If you can give me a few days ill do some testing, im in the middle of a project and my brain power is neck deep in it. But ill be glad to play around with it and get the output changed if you can give me a few days.

    If not if anyone can assist would be great, ill check back in a few days and see your status. Im suppose to have this deal done by thursday for my friend.
    That sounds good because I'm not sure that people understand that this style of pagination:

    1-20 21-40 41-60 61-80 81-100 101-120 121-140 141-160 161-180 181-200 201-220 221-240 241-260 261-280 281-293

    On there if you click say 21-40 that bolds... it's by the value of net=. On the one I was trying, where I have a previous and next link right now.. and with a ton of results, eh.. the style I want is better.. I use the value start= and it's like $current = "$start"; so it grabs the value and start is +1 so if you're on 0 you're seeing 1-20.. I got all that stuff good.. it's just the pagination for this style is very hard, but I really want it! I have the SQL above and that's the main functions.

    Thanks!
    Last edited by mfoland; 11-27-2012 at 11:04 PM. Reason: fixed pagination not showing

  • #12
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by mfoland View Post
    That sounds good because I'm not sure that people understand that this style of pagination:

    1-20 21-40 41-60 61-80 81-100 101-120 121-140 141-160 161-180 181-200 201-220 221-240 241-260 261-280 281-293
    I think everyone understands that style of pagination. The pagination would be simple.

    PHP Code:
    $cnt//set to value in database
    $currentpage $_GET['page']; // Whatever you wanna use.
    $current 1;
    $limit 20//Or set to other value, or user defined.

    while($current <= $cnt)
    {
        
    $thislast $current $limit;
        if(
    $thislast $cnt)
        {
              
    $thislast $cnt;
        }
        if(
    $current = ((($currentpage 1) * 20) + 1))
        {
              echo 
    "<p class = 'paginationBold'>{$current} - {$thislast}</p>";
        }
        else
              echo 
    "<a class = 'paginationLink' href = "insert url">{$current} - {$thislast}</p>";

    Last edited by KULP; 11-28-2012 at 05:32 AM.

  • #13
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KULP View Post
    I think everyone understands that style of pagination. The pagination would be simple.

    Code:
    $cnt= //set to value in database
    $currentpage = $_GET['page']; // Whatever you wanna use.
    $current = 1;
    $limit = 20; //Or set to other value, or user defined.
    
    while($current <= $cnt)
    {
        $thislast = $current + $limit;
        if($thislast > $cnt)
        {
              $thislast = $cnt;
        }
        if($current = ((($currentpage - 1) * 20) + 1))
        {
              echo "<p class = 'paginationBold'>{$current} - {$thislast}</p>";
        }
        else
              echo "<a class = 'paginationLink' href = "insert url">{$current} - {$thislast}</p>";
    }
    Tried that and I got a blank page.. hmm. I even did the page = and went from there.
    EDIT-- The Echo was wrong there.. but After fixing that, it would repeat 1-19 a ton of times. $cnt should be getting the total from the database.
    Last edited by mfoland; 11-28-2012 at 02:09 AM. Reason: Fixed blank page part but new error came about

  • #14
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Didn't think you were just gonna copy paste, but I just revised it in an actual file (was just doing it off the fly but here you go)

    PHP Code:
    <?php
    $cnt
    100//Set to value in database
    $currentpage 1// Whatever you wanna use. Use $_GET so it's dynamic.
    $current 1;
    $limit 20//Or set to other value, or user defined.

    while($current <= $cnt)
    {
        
    $thislast $current $limit;
        if(
    $thislast $cnt)
        {
              
    $thislast $cnt;
        }
        if(
    $current == ((($currentpage 1) * 20) + 1))
        {
              echo 
    '<p class = "paginationBold">'.  $current ' - ' $thislast '</p>';
        }
        else
              echo 
    '<a class = "paginationLink" href = "insert url">' .  $current ' - ' $thislast '</p>';

        
    $current += $limit//Added this to update $current. Was missing.
    }
    ?>
    Last edited by KULP; 11-28-2012 at 05:32 AM.

  • Users who have thanked KULP for this post:

    mfoland (11-28-2012)

  • #15
    New Coder
    Join Date
    Dec 2010
    Posts
    28
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KULP View Post
    Didn't think you were just gonna copy paste, but I just revised it in an actual file (was just doing it off the fly but here you go)

    Code:
    <?php
    $cnt= 100; //Set to value in database
    $currentpage = 1; // Whatever you wanna use. Use $_GET so it's dynamic.
    $current = 1;
    $limit = 20; //Or set to other value, or user defined.
    
    while($current <= $cnt)
    {
        $thislast = $current + $limit;
        if($thislast > $cnt)
        {
              $thislast = $cnt;
        }
        if($current == ((($currentpage - 1) * 20) + 1))
        {
              echo '<p class = "paginationBold">'.  $current . ' - ' . $thislast . '</p>';
        }
        else
              echo '<a class = "paginationLink" href = "insert url">' .  $current . ' - ' . $thislast . '</p>';
    
        $current += $limit; //Added this to update $current. Was missing.
    }
    ?>
    I'm new to this.. That's why... On the insert url.. I can do page or current page? = $current, right? I had a pagination that was only previous and next and that it had my start=$next = $current + 20 or - 20

    EDIT.. $cnt I'll end up doing $cnt because as you can see in the beginning of the script that's what counts to the total!
    Last edited by mfoland; 11-28-2012 at 03:42 AM.


  •  
    Page 1 of 3 123 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •