在php中应用数组的分页

时间:2015-02-03 11:22:48

标签: 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; 

我希望分页结果显示在表格中,请帮助。

1 个答案:

答案 0 :(得分:0)

下面是根据您的需要简化的代码,您只需循环遍历您的数组并使这些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>