duanjiu1003 2011-11-14 01:50
浏览 43
已采纳

如何使用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

  • 写回答

2条回答 默认 最新

  • doufei5315 2011-11-14 02:10
    关注

    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 ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大