douquan9826 2013-06-19 05:00
浏览 34
已采纳

分页MySql结果

I have a table that pulls tickets from a MYSQL database and displays them to the user. This part works wonderfully! But, once the user gets 5-10 tickets, the page becomes long and ugly. I am wanting to paginate the responses automatically to show maybe 5 tickets per page and show the rest on following pages.

I am using Bootstrap and have the following for the display of the pagination. "Although I fear this is only pseudo code" I added id's to each main element.

<ul class="pager" id="pagerUL">
    <li class="previous" id="previousUL">
        <a href="#">&larr; Previous</a>
    </li>
    <li class="next" id="nextUL">
        <a href="#">Next &rarr;</a>
    </li>
</ul>

Essentially the only code used to pull the database information on the page is:

<?php if(count($tickets) > 0) : ?>
     <?php foreach ($tickets as $ticket): ?>
        //table content 
     <?php endforeach ?>
<?php else : ?>
     <tr>
         <td colspan="7">No Tickets Created.</td>
     </tr>  
<?php endif; ?>

I thought we could add something to the <?php if(count($tickets) > 0) : ?> But honestly, I am not sure as I am not a expert at php and never even attempted or had the need to build pagination up until now. Any help, guidance, thoughts would be appreciated.

  • 写回答

1条回答 默认 最新

  • douyou3619 2013-06-19 05:09
    关注

    Pass the page number thru the URL and then grab it via php with $_GET.

     $page = $_GET['page'];
     $per_page = 10; // Define it as you please
     $start = $page*$per_page;
    
     $query = "SELECT * FROM table LIMIT ".$start.", ".$per_page;
     $result = mysql_query($query);
     $count = mysql_num_rows($result); 
     $i = 0;
    
     for($i=0;$i<$count;$i++)
     {
        //echo out what you want
        echo "";
     }
    
     $total_pages = ceil($count/$per_page);
    
     for($i=0;$i<$total_pages;$i++)
     {
        if($page != $i)
        {
           echo "<a href='/page.php?page=".$i."'>".$i."</a>";
        } else
        {
           echo $i;
        }
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题