如何使用html创建可分页网格

如何使用html创建可分页网格

问题描述:

I am pulling some data from mysql database using PHP, now I would like to place that data in a html table which I have completed, how can I make the table pageable if it goes over say 10 records? Is there a tutorial I can look into or any information where I can get this? Maybe a tool I can implement easily? I just haven't found anything online about this topic but perhaps anyone here can lead me in the correct direction

I am currently using just a simple <table></table> html

我使用PHP从mysql数据库中提取一些数据,现在我想把这些数据放在一个html表中 我已经完成了,如果超过10条记录,怎么能让表格可分页? 是否有我可以查看的教程或任何可以获得此信息的信息? 也许我可以轻松实现一个工具? 我只是没有在网上找到关于这个主题的任何内容,但也许这里的任何人都可以引导我走向正确的方向 p>

我目前只使用一个简单的&lt; table&gt;&lt; / 表&gt; code> html p> div>

You can achieve paging with MySQL's LIMIT keyword. You can then use a query string to tell the website which page to get.

First we need to set a default page number and define how many results we want to display in the page:

$items_per_page = 10;

$page = 1;

if(isset($_GET['page'])) {
    $page = (int)$_GET['page'];
}

The LIMIT keyword works by providing an offset and the number of rows you want to limit to. So now we need to figure out the offset:

$offset = ($page - 1) * $items_per_page;

Now we have all of the information we need to limit the results correctly based on the page number in our query string:

$query = "SELECT column_1, column_2 FROM your_table LIMIT {$offset}, {$items_per_page};";
$result = mysql_query($query) or die('Error, query failed');

while($row = mysql_fetch_assoc($result)) {
   echo $row['column_1'] . '<br />';
}

Now to show your different pages you just add the query string to the end of your page URI.

For example my_page.php?page=1 or my_page.php?page=2

Perhaps you could try to figure out how to create the paging links by yourself and post more if you can't get it to work.

You just need to find out the total rows in your query with COUNT in MySQL and you can do all of the maths from there ;)

You'll need some javascript in order to do paging..

Take a look at http://flexigrid.info/

In summary, what you should do is have your php script return the tabular data as JSON or XML, and then feed it to flexgrid.

You can also have flexgrid request records when they're needed using additional requests.