对 php 中的数组应用分页

Applying pagination for array in php

我已将 table 数据放入数组中。我想拆分数组并应用分页。例如:在 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; 

我想让分页结果显示在一个table中,请帮忙

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

您可以从 here 下载 js 和 css

<!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>