1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Paging With Custom Pagesize

Discussion in 'PHP' started by cancer10, Nov 5, 2008.

  1. #1
    Hello,

    I was going through this tutorial:
    http://www.php-mysql-tutorial.com/php-mysql-paging.php

    And noticed this line:
    $rowsPerPage = 20;
    Code (markup):
    Here, we are defining the Rows per page in the code itself. I was wondering, if I allow my visitors to set this value from the webpage itself, what bugs would that carry in my code (assuming I am using the exact code in the link above)?

    Thanx
     
    cancer10, Nov 5, 2008 IP
  2. money4leads

    money4leads Well-Known Member

    Messages:
    117
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #2
    ok, try this

    first the form to post rows per page
    
    <form method="POST" action="<?echo$_SERVER['PHP_SELF'];?>">
    Results per page: <input type="text" name="rowsperpage" maxlength="3" value="">
    
    <input type="submit" value="Show Results">
    
    
    HTML:

    second the php code to get these numbers

    
    
    if(isset($_POST["rowsperpage"]))
    { $rowsperpage=$_POST["rowsperpage"]; }
    else { 
    
    if(isset($_GET["r"]))
    { $rowsperpage=$_GET["r"]; }
    else 
    {
    $rowsperpage="20"; } }
    
    
    PHP:
    now r should be added at the end of your link as $rowsperpage value;

    
    
    $nextpage = " <a href=\"$_SERVER['PHP_SELF']?page=$page&r=$rowsperpage\">Next Page</a> ";
    
    
    PHP:
     
    money4leads, Nov 5, 2008 IP
  3. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    If you choose any value other than 20, you will need to construct the SQL statement accordingly.

    Eg: If your per page no. is 5, then the Y(duration) of your SQL LIMIT should be 5

    
    $sql="SELECT * FROM table LIMIT 0,5";
    //or 
    $sql="SELECT * FROM table LIMIT 0,$per_page";
    
    PHP:
     
    rohan_shenoy, Nov 5, 2008 IP
  4. cancer10

    cancer10 Guest

    Messages:
    364
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The reason why I asked u this question was, I currently have 8 records in my DB. The navigation/Pagination works fine if my customer enters any value in the PageSize textbox except the value 1, 6 and 7.

    1) If they enter the value 1 in the PageSize textbox then the nav pagination loops from 0 to 7. Clicking on the [7th] hyperlink, gives a record not found error.

    2) If they enter the value 6, then the page displays only 6 rows. (Not sure why)

    3) If they enter the value 7, then the the page displays only 7 rows. (Not sure why)

    For the remaining values, it displays all the 8 records. Which is fine.

    Any idea why is this happening?

    Thanx
     
    cancer10, Nov 6, 2008 IP
  5. money4leads

    money4leads Well-Known Member

    Messages:
    117
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    103
    #5
    try without for loop

    
    
    if ($pageNum > 1)
    {
    	$page = $pageNum - 1;
    	$prev = " <a href=\"$self?page=$page&r=$r\">[Prev]</a> ";
    
    	$first = " <a href=\"$self?page=1&r=$r\">[First Page]</a> ";
    }
    else
    {
    	$prev  = ' [Prev] ';       // we're on page one
    	$first = ' [First Page] '; // we're on page one
    }
    
    if ($pageNum < $maxPage)
    {
    	$page = $pageNum + 1;
    	$next = " <a href=\"$self?page=$page&r=$r\">[Next]</a> ";
    
    	$last = " <a href=\"$self?page=$maxPage&r=$r\">[Last Page]</a> ";
    }
    else
    {
    	$next = ' [Next] ';      // we're on the last page
    	$last = ' [Last Page] '; // we're on the last page
    }
    
    // print the page navigation link
    echo "<p align=center>" , $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages" . $next . $last , "</p>";
    
    
    
    
    PHP:
     
    money4leads, Nov 6, 2008 IP