如何使用数据提供程序实现php-mysql数据分页 [英] How to implement php-mysql data pagination using data providers

查看:77
本文介绍了如何使用数据提供程序实现php-mysql数据分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.我对此提出了另一个问题,但我想我已经使其复杂了一点.在这里,我将尝试简化我的问题:

Ok. i had created another question about this but i guess i had complicated it a bit.Ill try to simplify my problem here:

假设您有以下代码:

<?php
    if($_POST['page']){
        $page = $_POST['page'];
        $cur_page = $page;
        $page -= 1;
        $per_page = 15;
        $previous_btn = true;
        $next_btn = true;
        $first_btn = true;
        $last_btn = true;
        $start = $page * $per_page;
        include"dbconnect.php";

        $query_pag_data = "SELECT msg_id,message from test 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']);
            $msg .= "<li><b>" . $row['msg_id'] . "</b> " . $htmlmsg . "</li>";
        }
        $msg = "<div class='data'><ul>" . $msg . "</ul></div>"; // Content for Data

        /* --------------------------------------------- */
        $query_pag_num = "SELECT COUNT(*) AS count FROM student";
        $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----------------------------------- */
        if ($cur_page >= 7) {
            $start_loop = $cur_page - 3;
            if ($no_of_paginations > $cur_page + 3)
                $end_loop = $cur_page + 3;
            else if ($cur_page <= $no_of_paginations && $cur_page > $no_of_paginations - 6) {
                $start_loop = $no_of_paginations - 6;
                $end_loop = $no_of_paginations;
            } else {
                $end_loop = $no_of_paginations;
            }
        } else {
            $start_loop = 1;
            if ($no_of_paginations > 7)
                $end_loop = 7;
            else
                $end_loop = $no_of_paginations;
        }
        /* ----------------------------------------------------------------------------------------------------------- */
        $msg .= "<div class='pagination'><ul>";

        // FOR ENABLING THE FIRST BUTTON
        if ($first_btn && $cur_page > 1) {
            $msg .= "<li p='1' class='active'>First</li>";
        } else if ($first_btn) {
            $msg .= "<li p='1' class='inactive'>First</li>";
        }

        // FOR ENABLING THE PREVIOUS BUTTON
        if ($previous_btn && $cur_page > 1) {
            $pre = $cur_page - 1;
            $msg .= "<li p='$pre' class='active'>Previous</li>";
        } else if ($previous_btn) {
            $msg .= "<li class='inactive'>Previous</li>";
        }
        for ($i = $start_loop; $i <= $end_loop; $i++) {

            if ($cur_page == $i)
                $msg .= "<li p='$i' style='color:#fff;background-color:#006699;' class='active'>{$i}</li>";
            else
                $msg .= "<li p='$i' class='active'>{$i}</li>";
        }

        // TO ENABLE THE NEXT BUTTON
        if ($next_btn && $cur_page < $no_of_paginations) {
            $nex = $cur_page + 1;
            $msg .= "<li p='$nex' class='active'>Next</li>";
        } else if ($next_btn) {
            $msg .= "<li class='inactive'>Next</li>";
        }

        // TO ENABLE THE END BUTTON
        if ($last_btn && $cur_page < $no_of_paginations) {
            $msg .= "<li p='$no_of_paginations' class='active'>Last</li>";
        } else if ($last_btn) {
            $msg .= "<li p='$no_of_paginations' class='inactive'>Last</li>";
        }
        $goto = "<input type='text' class='goto' size='1' style='margin-top:-1px;margin-left:60px;'/><input type='button' id='go_btn' class='go_button' value='Go'/>";
        $total_string = "<span class='total' a='$no_of_paginations'>Page <b>" . $cur_page . "</b> of <b>$no_of_paginations</b></span>";
        $msg = $msg . "</ul>" . $goto . $total_string . "</div>";  // Content for pagination
        echo $msg;
    }

该代码是较大的分页解决方案的一部分,但我的问题是:

that code is part of a larger pagination solution, but my problem is this:

在上面的代码中,我正在同一位置查询数据并实现分页算法;

In the code above, am querying data and implementing pagination algorithms on the same place;

这是我在每个分页教程/演示/中找到的方法.

This is the approach ive been seeing in every paging tutorial/demo/walk out there.

在我的情况下,我已经有需要以json格式分页的数据,因此我将不会查询数据库.基本上,例如,如何实现对数组的分页?看看那里的样本,他们都使用LIMIT进行分页,没有它我无法想象任何分页方法,但是我无法重新查询数据库.

In my situation, i already have the data i need to paginate in a json format, so i will not be querying the database. basically, how can implement pagination over arrays, for example? looking at the samples out there, they all use LIMIT to paginate, and my mind cant conceive any pagination method without it, but i cannot requery the db.

有人试图对mysql结果集以外的内容进行分页吗?

Has anyone tried to paginate something other that mysql resultsets?

推荐答案

一种方法:

使用APC/Eaccelerator缓存来缓存结果并将数据解析到不同的页面.因此,下次有人请求该页面时,不要查询数据库,而是从高速缓存中检索数据并根据页码对其进行切片

Use APC/Eaccelerator cache to cache the result and parse the data to different pages. So the next time someone request the page, don't query DB, but retrieve the data from the cache and slice it depending on the page-number

PHP:

$start = ($this->pageNo - 1) * $pageSize;
$page = array_slice($data, $start, $this->pageSize);

另一种方式:

使用jquery插件或任何其他javascript插件来实现分页,该分页将转储所有数据一次并将其解析到不同的页面中.一个非常有用的链接: http://th3silverlining.com/2010 /04/15/pajination-a-jquery-pagination-plugin/ http://tablesorter.com/docs/example-pager.html

这篇关于如何使用数据提供程序实现php-mysql数据分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆