php和js实现分页的实例

<?php

$servername = "120.79.210.170:3306";
$username = "";
$password = "";
$dbname = "examplatform";
//实现数据库的连接
function conn()
{
    $conn = mysqli_connect("120.79.210.170:3306", "", "", "examplatform");
    mysqli_query($conn, "set names utf8");
    return $conn;
}


//获得结果集
function doresult($sql){
    $result=mysqli_query(conn(), $sql);
    return  $result;
}

//结果集转为对象集合
function dolists($result){
    return mysqli_fetch_array($result, MYSQLI_ASSOC);
}
//获取记录总数
function totalnums($sql) {
    $result=mysqli_query(conn(), $sql);
    return $result->num_rows;
}
//关闭数据库
function closedb()
{
    if (! mysqli_close()) {
        exit('关闭异常');
    }
}
?>
<?php

include '../MyPhp/mySqlConnectPage.php';

// 总记录数
$sql = "SELECT 
questions.qtitle ,examanswer.respond,questions.qanswer
from examanswer,qpools,questions,testpaper
WHERE qpools.tno=examanswer.tno and testpaper.tno=examanswer.tno and
qpools.eno=examanswer.id
and   questions.qno=qpools.qno and examanswer.etno='3' and testpaper.tname='$paperName'
ORDER BY  examanswer.tno,examanswer.mno,examanswer.id";
$totalnums = totalnums($sql);

// 每页显示条数
$fnum = 5;

// 翻页数
$pagenum = ceil($totalnums / $fnum);

// 页数常量
@$tmp = $_GET['page'];
//当前页数
$current_page   = empty($_GET['page']) ? 1 : $_GET['page'];
//上一页
$previous_page  = $current_page - 1;
//下一页
$next_page      = $current_page + 1;


if ($previous_page == 0) {
    $previous_page = 1;
}

if ($next_page > $pagenum) {
    $next_page = $current_page;
}

//防止恶意翻页
if ($tmp > $pagenum)
    echo "<script>window.location.href='allPaperCheck.php'</script>";

//计算分页起始值
if ($tmp == "") {
    $num = 0;

} else {
    $num = ($tmp - 1) * $fnum;

}

// 查询语句,获取需要的数据
$sql = "SELECT mno,mname,  tname, qtitle,respond,qanswer ,correct,class,tno FROM examcorrectinfo  where tname='$paperName'    
 LIMIT " . $num . ", $fnum";
$result = doresult($sql);

$i=0;
//将数据存入二维数组
while (! ! $rows = dolists($result)) {
    $fillsum[0]=$rows['qtitle'];
    $fillsum[1]=$rows['respond'];
    $fillsum[2]=$rows['qanswer'];
    $fillsum[3]=$rows['mno'];
    $fillsum[4]=$rows['mname'];
    $fillsum[5]=$rows['tname'];
    $fillsum[6]=$rows['correct'];
    $fillsum[7]=$rows['class'];
    $fillsum[8]=$rows['tno'];
    $fillsums[$i++]=$fillsum;
}
//对变量进行JSON编码;
$fillsumArray= json_encode($fillsums, JSON_UNESCAPED_UNICODE);
?>
<script>
        //将PHP数组变量转化为就是数组变量
    let fillnum=<?=$fillsumArray ?>;
       //获取总页数
    let pageNum=<?=$pagenum?>;
     //获取试卷名称
    let paperName='<?=$paperName?>';
     //获取当前页数
    let page=<?=$current_page?>;
     //获取上一页
    let prePage=<?=$previous_page?>;
     //获取下一页
    let next_page=<?=$next_page?>;


   //显示分页连接
    function createSpan() {

        let MyDiv =document.getElementById('page');
        let k=0;

        //上一页
        let previous=document.createElement("li");
        let previousPage=document.createElement("a");
        previousPage.innerText='«';
        previousPage.setAttribute
        ('href','allPaperCheck.php?page='+prePage+'&paperName='+paperName+'');

        //首页
        let first=document.createElement("li");
        let firstPage=document.createElement("a");
        firstPage.innerText='首页';
        firstPage.style.fontSize='10px';
        firstPage.setAttribute
        ('href','allPaperCheck.php?page=1&paperName='+paperName+'');

        MyDiv.appendChild(first);
        first.appendChild(firstPage);
        MyDiv.appendChild(previous);
        previous.appendChild(previousPage);

        //打印链接
        for (let i = 0; i < pageNum; i ++) {
            let record=document.createElement("li");
            let connect=document.createElement("a");
            let k=i+1;

            connect.setAttribute
            ('href','allPaperCheck.php?page='+k+'&paperName='+paperName+'');
            connect.innerText=k;
            MyDiv.appendChild(record);
            record.appendChild(connect);

        }
        //下一页
        let next=document.createElement("li");
        let nextPage=document.createElement("a");
        nextPage.innerText='»';
        nextPage.setAttribute
        ('href','allPaperCheck.php?page='+next_page+'&paperName='+paperName+'');
        MyDiv.appendChild(next);
        next.appendChild(nextPage);

        //尾页
        let last=document.createElement("li");
        let lastPage=document.createElement("a");
        lastPage.style.fontSize='10px';
        lastPage.innerText='尾页';

        lastPage.setAttribute
        ('href','allPaperCheck.php?page='+pageNum+'&paperName='+paperName+'');
        MyDiv.appendChild(last);
        last.appendChild(lastPage);




       //显示当前页码,让当前页变色
    function changeColor() {

        $('a').each(function(){
            $content=$(this).text();
            if(page==$content)
            {
                $(this).addClass('active');
            }
        });
    }

    $(document).ready(function () 
        createSpan();
        changeColor();
    });

</script>

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值