自动分页或类似的东西[关闭]

I'm fairly new to css, html, php, javascript etc, but I'm slowly but surely building a small blog site with the help of stackoverflow. Keep in mind I have only been coding for about 5 months.

What I'm looking for is some help with pagination, I've searched but I need someone to point me in the definite direction with my search or at least help me out a bit.

I want to "auto-paginate" my pages, so example; if I wanted 15 blog posts per page, on each page how do I automatically do this with pagination, php etc. I know I can manually do it but as I add more blog posts it becomes quite tedious over time, so I was looking for a way to "automatically do it" if there is a way..

Thanks in advance to everyone who understands and is willing to help!

You can do pagination with $_SESSION, this is what you need :

  • The posts themselves (or their ids).
  • How many posts to display per page.
  • What is the current post on screen.

For example, if you have 100 posts (from 0 to 99), you want to display 10 posts per page, and the current post is 60, then you will have to display posts from 60 to 69. These three vital pieces of data can be stored in $_SESSION variables :

  • $_SESSION[ "posts" ].
  • $_SESSION[ "per_page" ].
  • $_SESSION[ "current" ].

You have to use $_SESSION because $_SESSION data persists after page refresh.

Next is an example without a database, so the "posts" are stored in an array. In order to test it, copy-paste next code in a file, save the file as .PHP and open it in your browser :

<?php
session_start(); // NECESSARY TO USE $_SESSION.

// SAMPLE DATA.
$_SESSION[ "posts" ] = array( "aaa","bbb","ccc","ddd","eee","fff","ggg",
                              "hhh","iii","jjj","kkk","lll","mmm","nnn" );
$_SESSION[ "per_page" ] = 4; // PAGES PER PAGE.
if ( ! isset( $_SESSION[ "current" ] ) ) // IF THIS IS THE FIRST PAGE
   $_SESSION[ "current" ] = 0; // CURRENT BLOG.

if ( isset( $_POST[ "submit" ] ) ) // IF "PREVIOUS" OR "NEXT" WERE PRESSED
   { if ( ( $_POST[ "submit" ] == "Previous page" ) &&
          ( ( $_SESSION[ "current" ] - $_SESSION[ "per_page" ] ) >= 0 ) )
            $_SESSION[ "current" ] -= $_SESSION[ "per_page" ];
     if ( ( $_POST[ "submit" ] == "Next page" ) &&
          ( ( $_SESSION[ "current" ] + $_SESSION[ "per_page" ] ) <
             count( $_SESSION[ "posts" ] ) ) )
           $_SESSION[ "current" ] += $_SESSION[ "per_page" ];
   }
?>
<html>
  <body>
    Showing posts :
    <br/>
<?php
// DISPLAY ONE PAGE OF POSTS (FROM CURRENT TO CURRENT + PER_PAGE - 1).
for ( $i = $_SESSION[ "current" ];
      $i < ( $_SESSION[ "current"  ] + $_SESSION[ "per_page" ] ); $i++ )
  if ( isset ( $_SESSION[ "posts" ][ $i ] ) ) // IN CASE LAST PAGE IS ODD
     echo $_SESSION[ "posts" ][ $i ] . "<br/>";
?>
    <br/>
    <form method="post">
      <input type="submit" name="submit" value="Previous page"/>
      <input type="submit" name="submit" value="Next page"/>
    </form>
  </body>
</html>

To make this work with your already made blog site, get the ids of your posts and store them in the array, and, in the HTML section, use those ids to get the posts' data from database and display it on screen.