一聚教程网:一个值得你收藏的教程网站

热门教程

php Ajax分页简单应用实例

时间:2022-06-24 21:37:41 编辑:袖梨 来源:一聚教程网

简单的mysql数据表结构

 代码如下 复制代码

CREATE TABLE messages
(
msg_id INT PRIMARY KEY AUTO_INCREMENT,
message VARCHAR(150)
);

JavaScript 代码

这里是ajax前段利用jquery来处理

 代码如下 复制代码


load_data.php

这里是获取由ajax发送的数据然后经过php查询mysql返回信息

 代码如下 复制代码

if($_POST['page'])
{
$page = $_POST['page'];
$cur_page = $page;
$page -= 1;
$per_page = 15; // Per page records
$previous_btn = true;
$next_btn = true;
$first_btn = true;
$last_btn = true;
$start = $page * $per_page;
include"db.php";
$query_pag_data = "SELECT msg_id,message from messages LIMIT $start, $per_page";
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$msg = "";
while ($row = mysql_fetch_array($result_pag_data))
{
$htmlmsg=htmlentities($row['message']); //HTML entries filter
$msg .= "

  • " . $row['msg_id'] . " " . $htmlmsg . "
  • ";
    }
    $msg = "
      " . $msg . "
    "; // Content for Data
    /* -----Total count--- */
    $query_pag_num = "SELECT COUNT(*) AS count FROM messages"; // Total records
    $result_pag_num = mysql_query($query_pag_num);
    $row = mysql_fetch_array($result_pag_num);
    $count = $row['count'];
    $no_of_paginations = ceil($count / $per_page);
    /* -----Calculating the starting and endign values for the loop----- */

    //Some Code. Available in download script

    }
    ?>

    热门栏目