Pagination.

Results 1 to 15 of 15
  1. #1
    1/11/1995 ~ 23/11/2011 rebora2007 is offline
    Grand MasterRank
    Nov 2010 Join Date
    Care much ?Location
    2,346Posts

    Pagination.

    Hi I'm trying to make a pagination in PhP/Mysql.
    I don't think I really need to give any code snippets do I?

    Anyway I did google a lot but codes weren't clean or even unreadable and since RZ have talented coders I chose to ask for help here

    Basically all I want is to know how to create a pagination for a blog page. Like 3 blog news on the first page, 3 others on the 2nd.
    I tried a lot but meh, I failed.

    Thanks.


  2. #2
    Ginger by design. jMerliN is offline
    Grand MasterRank
    Feb 2007 Join Date
    2,500Posts

    Re: Pagination.

    Limit

  3. #3
    1/11/1995 ~ 23/11/2011 rebora2007 is offline
    Grand MasterRank
    Nov 2010 Join Date
    Care much ?Location
    2,346Posts

    Re: Pagination.

    Well I know that... The thing is, I want it to have pages.
    If I only limit, page 2 won't automatically show 3 other blog news on it.

  4. #4
    Watching from above Negata is offline
    LegendRank
    Apr 2004 Join Date
    FinlandLocation
    5,455Posts

    Re: Pagination.

    You get the page number as a parameter from the request or whatever, use that for your SQL query as page * news_per_page as OFFSET and news_per_page as LIMIT.

  5. #5
    (oO (||||) (||||) Oo) jM2.me is offline
    Grand MasterRank
    Aug 2009 Join Date
    USA (Fuck Yeah)Location
    2,527Posts

    Re: Pagination.

    news_per_page = 10
    current_page = 2

    mysql_query = SELECT * FROM news LIMIT news_per_page OFFSET (news_per_page * (current_page - 1))

    This should give you an idea.

    Edit: That's pretty much what Negata wrote.

  6. #6
    Grand Master Justei is offline
    Grand MasterRank
    Oct 2007 Join Date
    /f241Location
    1,904Posts

    Re: Pagination.

    You can use limit as well, very simply since limit has a start and end parameter, so for example:

    http://bla.com?page=3
    then you know that the page is 3 right, and you wish to show 10 posts/whatever on your site.

    Then all you do is have the start to page * 10 (30), and then have the 2nd paremeter to 30+10.

    Should be fine, unless I am totally off, which I might be just now since I am totally lost in my head lol, so please confirm this sum1.

  7. #7
    Sorcerer Supreme Hexadecimal is offline
    Member +Rank
    Dec 2010 Join Date
    424Posts

    Re: Pagination.

    This is a pagination I made for one of my projects. Feel free to use it;

    PHP Code:
    if(!@$_GET['p'])
    $Page 1;
    else
    $Page = @$_GET['p'];

    $FirstLimit = ($Page 1) * 10;
    $SecondLimit 10;

    $Query mysql_query("YOUR QUERY HERE LIMIT ".$FirstLimit.", ".$SecondLimit);

    $Fetch mysql_fetch_array($Query);

    $Count mysql_num_rows(mysql_query("YOUR QUERY HERE"));

    $Max_Pages ceil($Count/10); 

    echo 
    '<center>

    Page '
    .$Page.' of '.$Max_Pages.'

    <br />

    '
    ;

    for(
    $i 1$i != $Max_Pages 1$i++)
    {

    echo 
    '<a href="SITE URL?p='.$i.'">'.$i.'</a> &nbsp; ';

    }

    echo 
    '</center>'
    Enjoy,

    -BGxApixen

  8. #8
    1/11/1995 ~ 23/11/2011 rebora2007 is offline
    Grand MasterRank
    Nov 2010 Join Date
    Care much ?Location
    2,346Posts

    Re: Pagination.

    Thanks guys! You all helped, I appreciate.

    So that's what I've done. I use classes for mysql queries request etc:

    PHP Code:
           if(!$_GET['page'])
           {
           
    $page 1
           }
           
           else
           { 
           
    $page $_GET['page']; 
           }
           
           
    $firstlimit = ($page -1) * 3;
           
    $secondlimit 3;
           
           
    $reqCount $bdd->query('SELECT COUNT(*) FROM billet');
           
    $donnees $reqCount->fetch();
           
           
    $maxPages ceil($donnees['COUNT(*)']/3);
           
           
    $req $bdd->query('SELECT * FROM billet ORDER BY ID DESC LIMIT '.$firstlimit.' ,'.$secondlimit.''); 
    Thanks again !

  9. #9
    Software Person TimeBomb is offline
    ModeratorRank
    May 2008 Join Date
    United StatesLocation
    1,252Posts

    Re: Pagination.

    As I normally do, I must advocate that you protect your SQL queries from SQL injection. Your code would at best error out if I injected the GET variable, and at worst I could delete every single database in your mysql server.
    I highly suggest using the mysqli PHP library. It has both OOP and non-OOP styles to it, and you can bind parameters. It is much more secure and moderately more powerful.
    GL.

  10. #10
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Re: Pagination.

    Quote Originally Posted by timebomb View Post
    As I normally do, I must advocate that you protect your SQL queries from SQL injection. Your code would at best error out if I injected the GET variable, and at worst I could delete every single database in your mysql server.
    I highly suggest using the mysqli PHP library. It has both OOP and non-OOP styles to it, and you can bind parameters. It is much more secure and moderately more powerful.
    GL.
    As much as I fully agree with you, the $firstlimit variable is protected from SQL injections since it's integer type.

    In PHP,
    PHP Code:
    var_dump('-- UNION(SELECT * FROM users WHERE 1=1)' 1); // prints int(-1) 
    The only reason it's safe is the abstract implicit conversion from string to integer.

    So this is technically escaping the input,
    PHP Code:
    $firstlimit = ($page -1) * 3
    MySQLi would give you more explicit safety which is guaranteed over later versions, while this implicit conversion may change.

    If this were a real programming language it would be kind of funny...

    You might expect PHP to change this functionality over time. For instance, in a later version "hello" - 1 might return "hell", like in python where "hello" * 3 = "hellohellohello"

    Other than that possibility, it's currently safe from injection as-is in PHP 5.3, but MySQLi is better as the MySQL extension hasn't been improved or updated to use features in modern MySQL 5 DBs.

    MySQL extension is old. Coding new software in something that's going out of style fast is a bad idea. The MySQL extension is seriously, for good reason, going to be deprecated in a future release of PHP. Coding in something you know is old and is going to be deprecated and later outdated and finally removed, is not wise at all.

    Quote Originally Posted by http://marc.info/?l=php-internals&m=131031747409271
    Greetings PHP geeks,

    Don't panic! This is not a proposal to add errors or remove this popular extension. \
    Not yet anyway, because it's too popular to do that now.

    The documentation team is discussing the database security situation, and educating \
    users to move away from the commonly used ext/mysql extension is part of this.

    This proposal only deals with education, and requests permission to officially \
    convince people to stop using this old extension. This means:

    - Add notes that refer to it as deprecated
    - Recommend and link alternatives
    - Include examples of alternatives

    There are two alternative extensions: pdo_mysql and mysqli, with PDO being the PHP \
    way and main focus of future endeavors. Right? Please don't digress into the PDO v2 \
    fiasco here.

    What this means to ext/mysql:

    - Softly deprecate ext/mysql with education (docs) starting today
    - Not adding E_DEPRECATED errors in 5.4, but revisit for 5.5/6.0
    - Add pdo_mysql examples within the ext/mysql docs that mimic the current
    examples, but occasionally introduce features like prepared statements
    - Focus energy on cleaning up the pdo_mysql and mysqli documentation
    - Create a general "The MySQL situation" document that explains the situation

    The PHP community has been recommending alternatives for several years now, so \
    hopefully this won't be a new concept or shock to most users.

    Regards,
    Philip

    As the quote in my signature below describes:

    "Success is easy to handle: You've solved the wrong problem. Work hard to improve."
    Last edited by s-p-n; 26-10-11 at 07:39 PM.

  11. #11
    Grand Master Ron is offline
    Grand MasterRank
    Apr 2005 Join Date
    Location
    8,988Posts

    Re: Pagination.

    PHP Code:
    else 
           {  
           
    $page $_GET['page'];  
           } 
    What happens when I do this?

    /list.php?page=lolnotanumber

    Just some food for thought.


  12. #12
    :-) s-p-n is offline
    DeveloperRank
    Jun 2007 Join Date
    Next DoorLocation
    2,097Posts

    Re: Pagination.

    Quote Originally Posted by Ron View Post
    PHP Code:
    else 
           {  
           
    $page $_GET['page'];  
           } 
    What happens when I do this?

    /list.php?page=lolnotanumber

    Just some food for thought.

    PHP Code:
    var_dump(('lolnotanumber' 1) * 3); 
    prints:
    Code:
    int(-3)
    Results in:
    Code:
    SELECT * FROM billet ORDER BY ID DESC LIMIT -3 ,3
    But injections are aside from the real point here. MySQL extension cannot take advantage of new database features in MySQL 5

  13. #13
    Grand Master Ron is offline
    Grand MasterRank
    Apr 2005 Join Date
    Location
    8,988Posts

    Re: Pagination.

    Point was he needs to incorporate error handling, ex. load the first page if $page isn't numeric.

  14. #14
    1/11/1995 ~ 23/11/2011 rebora2007 is offline
    Grand MasterRank
    Nov 2010 Join Date
    Care much ?Location
    2,346Posts

    Re: Pagination.

    I'll do that, I was aware though, but my "blog" was just local so I didn't care much about injections. I don't even use variables when I connect to my db

  15. #15
    Infraction Baɴɴed holthelper is offline
    Grand MasterRank
    Apr 2008 Join Date
    1,765Posts

    Re: Pagination.

    when i was making a ranking script i always had the problem of negative numbers messing with the results of the query so i just slapped abs() on it and BAM simple fix.



Advertisement