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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器