PHP native deve...LOGIN

PHP native development news station news list pagination

We told you about the function creation of the news list in the last class. In the last class, we had a paging function under the news list page! So today we will introduce to you the production process of paging implementation!

First we create a php file page.php, in this file we put the code for making paging!

The first step: Same, connect to the database!

// 显示所有的错误
error_reporting(E_ALL & ~E_NOTICE  );
// 连接mysql数据库
$link = mysqli_connect('localhost','root', 'root');
if (!$link) {
    echo "connect mysql error!";
    exit();
}
// 选中数据库 news为数据库的名字
$db_selected = mysqli_select_db($link, 'news');
if (!$db_selected) {
    echo "<br>selected db error!";
    exit();
}
// 设置mysql字符集 为 utf8
$link->query("set names utf8");

The second step: To implement the paging function, we need to query which data table in which list. We are paging news, so our SQL statement must check the news table

// 查询新闻表中的数据
$sql = "select * from new where 1 "; // 查询语句
$sql_count =  "select count(*) as amount from new where 1 "; // 统计总记录数
$sql .= "order by id asc";

The next step is to get the total number of records:

// 获取总记录条数
$result_amount = mysqli_query($link, $sql_count);
$arr_amount = mysqli_fetch_array(mysqli_query($link, $sql_count), MYSQL_ASSOC);
// 总记录条数
$amount = $arr_amount['amount'];

Then set the total number of pages and the total page number

// 每页的记录条数
$page_size = 4;
// 总页码
$max_page = ceil( $amount / $page_size );

Everyone knows that there is a formula algorithm for paging. We will calculate the previous page based on this formula. Page, next page, last page!

// 获取当前页码
$page = intval($_GET['page']); // 获取page值,并转成int
if( $page <= 0 || $page > $max_page){  // 如果page值小于0,或是大于最大页码
    $page = 1;
}
// 上一页
$pre_page = $page -1;
if( $pre_page < 1 ){ // 如果上一页小于1
    $pre_page = 1;
}

// 下一页
$next_page = $page + 1;
if( $next_page > $max_page ){ // 如果下一页大于最大页码
    $next_page = $max_page;
}
// 分页计算, 计算分页的offset
$offset = ($page - 1 ) * $page_size;
$sql .= " limit $offset, $page_size ";

At this point our paging code is finished, and then we introduce this paging file on the news list page

<?php
include_once "../common/page.php";
?>

Finally find the paging position below the news list page and output the paging

<div class="pagelist">
<a href="new_list.php">首页</a>
<?php
if( $page > 1 ){
    ?>
    <a href="new_list.php?page=<?php echo $pre_page;?>">上一页</a>
    <?
}
if( $page < $max_page ){
    ?>
    <a href="new_list.php?page=<?php echo $next_page;?>">下一页</a>
    <?
}
?>
<a href="new_list.php?page=<?php echo $max_page;?>">末页</a>
/  总页码 <font color="red"><?php echo $max_page;?></font>页 当前页码 <font color="red"><?php echo $page;?></font>页
</div>

1741.png

OK! The pagination is finished!

Note: Mine may be a bit troublesome here. You can also download the paging class from the website and call it directly! But you need to know the principle of paging implementation~

Next Section
<?php // 显示所有的错误 error_reporting(E_ALL & ~E_NOTICE ); // 连接mysql数据库 $link = mysqli_connect('localhost','root', 'root'); if (!$link) { echo "connect mysql error!"; exit(); } // 选中数据库 news为数据库的名字 $db_selected = mysqli_select_db($link, 'news'); if (!$db_selected) { echo "<br>selected db error!"; exit(); } // 设置mysql字符集 为 utf8 $link->query("set names utf8"); // 查询简历表中的用户信息 $sql = "select * from new where 1 "; // 查询语句 $sql_count = "select count(*) as amount from new where 1 "; // 统计总记录数 $sql .= "order by id asc"; // 获取总记录条数 $result_amount = mysqli_query($link, $sql_count); $arr_amount = mysqli_fetch_array(mysqli_query($link, $sql_count), MYSQL_ASSOC); // 总记录条数 $amount = $arr_amount['amount']; // 每页的记录条数 $page_size = 4; // 总页码 $max_page = ceil( $amount / $page_size ); // 获取当前页码 $page = intval($_GET['page']); // 获取page值,并转成int if( $page <= 0 || $page > $max_page){ // 如果page值小于0,或是大于最大页码 $page = 1; } // 上一页 $pre_page = $page -1; if( $pre_page < 1 ){ // 如果上一页小于1 $pre_page = 1; } // 下一页 $next_page = $page + 1; if( $next_page > $max_page ){ // 如果下一页大于最大页码 $next_page = $max_page; } // 分页计算, 计算分页的offset $offset = ($page - 1 ) * $page_size; $sql .= " limit $offset, $page_size "; ?>
submitReset Code
ChapterCourseware