laravel 手写最原生分页

1.直接上代码

控制器
    public function page(Request $request){
        //总条数
        $num=DB::table('mon_message')->count();
        //每页显示
        $shownum=4;
        $page=ceil($num/$shownum);
        $pageshow=$request['page']? $request['page'] :1;
        $offset=($pageshow-1)*$shownum;
        $prev=$pageshow-1<1? 1: $pageshow-1;
        $next=$pageshow+1>=$page? $page :$pageshow+1;
        $data=DB::table('mon_message')
            ->offset($offset)
            ->limit($shownum)
            ->orderBy('id','asc')
            ->get();
        return view('page',compact('data','prev','next','page'));
    }

2.视图

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>

    <ul class="pagination">
        <li><a href="{{url('page')}}/list_1.html">首页</a></li>
        <li><a href="{{url('page')}}/list_{{$prev}}.html">上一页</a></li>
        @for($i=1;$i<=$page;$i++)
        <li><a href="{{url('page')}}/list_{{$i}}.html">{{$i}}</a></li>
        @endfor
        <li><a href="{{url('page')}}/list_{{$next}}.html">下一页</a></li>
        <li><a href="{{url('page')}}/list_{{$page}}.html">尾页</a></li>
    </ul>
</center>
<center>
    @foreach($data as $k=>$v)
    <div class="col-md-6" style="border: 2px solid deeppink;margin-top: 20px" id="{{$v->id}}">
        <div class="row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
            <div class="col p-4 d-flex flex-column position-static">
                <strong class="d-inline-block mb-2 text-success">{{$v->id}}</strong>
                <a href="{{url('infomess')}}/{{$v->id}}"><h3 class="mb-0">{{$v->title}}</h3></a>
                <div class="mb-1 text-muted">{{$v->author}}</div>
                <p class="mb-auto">{{$v->talk}}</p>
                <a href="#" class="stretched-link"><img src="{{$v->img}}" style="width: 100%;height: 200px"></a>
            </div>
        </div>
        <a href="{{url('info')}}/{{$v->id}}" class="btn btn-primary">详情</a>
        <input type="button" value="删除" class="btn btn-danger" onclick="fun({{$v->id}})">
        <a href="{{url('up')}}/{{$v->id}}" class="btn btn-warning">修改</a>

    </div>
{{--        <img src="{{$v->img}}" alt="">--}}
    @endforeach
</center>
</body>
</html>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script>
function fun(id) {
    $.get("http://www.web5mon.com/del/"+id,function (status,data) {
        $('#'+id).remove();
    })
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值