php - 在php中对数组应用分页

标签 php mysql arrays pagination

我已将表数据放入数组中。我想拆分数组并应用分页。例如:在 1 页中获取 2 行。 是否可以?请帮忙!

/*Fetching data from database*/
$sql2=mysql_query("SELECT date,name FROM main WHERE ((ward='$ward') AND(date BETWEEN '$startdate' AND '$enddate'))");

$array = array();

while($row = mysql_fetch_assoc($sql2))
{

  // add each row returned into an array
  $array[] = $row;
} 

// show all array data
print_r($array);?></br><?php
$pages = array_chunk($array, 2);
print_r($pages);

for($i=0; $i< count($pages)+1; $i++): 
echo $i; 
endfor; 

我想将分页结果显示在表格中,请帮忙。

最佳答案

下面是根据您的需要简化的代码,您只需循环遍历数组并使这些 TR 动态

js和css可以从here下载

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta name="viewport" content="width=device-width, maximum-scale=1"/>
        <title>Pagination Example</title>

        <link href="css/simplePagination.css" type="text/css" rel="stylesheet"/>
        <script src="js/jquery-1.10.2.min.js"></script>
        <script src="js/jquery.simplePagination.js"></script>

        <script>
            jQuery(function($) {
                var items = $("#content tbody tr");

                var numItems = items.length;
                var perPage = 2;

                // only show the first 2 (or "first per_page") items initially
                items.slice(perPage).hide();

                // now setup pagination
                $("#pagination").pagination({
                    items: numItems,
                    itemsOnPage: perPage,
                    cssStyle: "light-theme",
                    onPageClick: function(pageNumber) { // this is where the magic happens
                        // someone changed page, lets hide/show trs appropriately
                        var showFrom = perPage * (pageNumber - 1);
                        var showTo = showFrom + perPage;

                        items.hide() // first hide everything, then show for the new page
                             .slice(showFrom, showTo).show();
                    }
                });
            });
        </script>
    </head>
    <body>
        <h1>Pagination Example</h1>
        <div id="pagination"></div>
        <table id="content">
            <tbody>
                <tr>
                    <td>Window</td>
                    <td>John</td>
                </tr>
                <tr>
                    <td>Door</td>
                    <td>Chris</td>
                </tr>
                <tr>
                    <td>Floor</td>
                    <td>Michael</td>
                </tr>
                <tr>
                    <td>Car</td>
                    <td>James</td>
                </tr>
                <tr>
                    <td>Bike</td>
                    <td>Steven</td>
                </tr>
            </tbody>
        </table>
    </body>
</html>

关于php - 在php中对数组应用分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28297696/

相关文章:

php - 获取具有特定值的最新数据库条目

php - yii2 session 错误

Jquery数组融合

php - 如何从 while 循环中的 while 循环中获取数据

php - 从单一表单将数据插入到多个 SQL 表中

php - Laravel 自定义模型类 - 字段没有默认值

arrays - 计算 scala 数组中 Double.NaN 值的数量

c++ - 数组和指针有问题

php - 我如何在 php 中使用 MaxMind 的数据库

php - 如何将 htaccess 重定向匹配与 query_string 一起使用?