SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question PHP Paging With Custom Pagesize

    Hello,

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

    And noticed this line:
    Code:
    $rowsPerPage = 20;
    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

  2. #2
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    None, just make sure its an integer (whole number) and >= 1.

    Also make sure you filter it in the query using mysql_real_escape_string so you don't risk getting hacked (easily).

  3. #3
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by simshaun View Post
    None, just make sure its an integer (whole number) and >= 1.

    Also make sure you filter it in the query using mysql_real_escape_string so you don't risk getting hacked (easily).
    better yet use a prepared query

  4. #4
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are assuming the query will never change?

    What about if you need to implement joins, change what columns you select, change sort orders... etc?

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You are assuming the query will never change?
    Makes no difference - a query can be modified in PDO as much as it could in MySQL.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm. either I am misunderstanding or I'm stupid.

    Prepared query being prepared in sql or prepared query as in using pdo to escape values for you?

  7. #7
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Makes no difference - a query can be modified in PDO as much as it could in MySQL.
    What is a PDO?


    Thanx

  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cancer10 View Post
    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)?
    Nothing will happen. You just track the user selected value like this:
    PHP Code:
    if(isset($_GET['perpage'])){
       
    $rowsPerPage $_GET['perpage'];
    }
    else{
        
    $rowsPerPage 20;

    What is a PDO?
    http://www.php.net/pdo
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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

  10. #10
    SitePoint Evangelist simshaun's Avatar
    Join Date
    Apr 2008
    Location
    North Carolina
    Posts
    438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that shows that the pagination is coded wrong.

  11. #11
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    725
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

  12. #12
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have found that this generic paging function works no matter if mysql, mysqli, or pdo:

    Code:
    <?php
    function pager($numpages, $page, $self, $limit, $queryvars = '')
    {
    $maxPage = ceil($numpages / $limit);
    echo "whitaker ===".$maxPage."===".$page."===".$self."===".$queryvars;
    echo "maxpage ==== ".$maxPage;
    if ($maxPage > 5)
    {
    
    $spage = $page;
    $epage = $page + 4;
    	$spage = $page;
    	$epage = $page + 4;
    	$menu7 = '';
    	if($page != 1)
    	{
    	$menu7 .= '<a href="'.$self.'?page='.($page - 1).'&'.$queryvars.' ">&lt;&lt; PREV </a>';
    	}
    	if ($epage < $maxPage)
    		{
    		for ($i = $spage; $i <= $epage; $i++)
    		{
    		if ($i == $page)
    		{
    		$menu7 .= '<class="selected">'.$i.'';
    		}
    		else
    		{
    		$menu7 .= '<a href="'.$self.'?page='.$i.'&'.$queryvars.'">'.$i.'</a>'."\n";
    		}
    		}
    		}
    	else
    		{
    		 for ($i = $spage; $i <= $maxPage; $i++)
    		{
    		if ($i == $page)
    		{
    		$menu7 .= '<class="selected">'.$i.'';
    		}
    		else
    		{
    		$menu7 .= '<a href="'.$self.'?page='.$i.'&'.$queryvars.'">'.$i.'</a>'."\n";
    		} //bottom else
    		} //for
    		} //top else
    	if ($page < $maxPage) 
    	{
    	$menu7 .= '<a href="'.$self.'?page='.($page + 1).'&'.$queryvars.'"> NEXT &gt;&gt;</a>';
    	}
    }
    else
    {
    	$menu7 = '';
    	if($page != 1)
    	{
    	$menu7 .= '<a href="'.$self.'?page='.($page - 1).'&'.$queryvars.'">&lt;&lt; PREV </a>';
    	}
    	for ($i = 1; $i <= $maxPage; $i++)
    	{
    	if ($i == $page)
    	{
    	$menu7 .= '<class="selected">'.$i.'';
    	}
    	else
    	{
    	$menu7 .= '<a href="'.$self.'?page='.$i.'&'.$queryvars.'">'.$i.'</a>'."\n";
    	}
    	}
    	if ($page < $maxPage) 
    	{
    	$menu7 .= '<a href="'.$self.'?page='.($page + 1).'&'.$queryvars.'"> NEXT &gt;&gt;</a>';
    	}
    
    }
    return $menu7;
    }
    ?>
    It's called like this:
    Code:
    $menu = '';
    $menu = pager($_SESSION['total_records'], $page, $self, $limit, $queryvars );
    queryvars would be something for the querystring like
    Code:
    $queryvars = "lname=$lname";


Bookmarks

Posting Permissions

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