使用while循环创建分页 [英] Create pagination using while loop

查看:129
本文介绍了使用while循环创建分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用while循环创建分页。
我已经显示数据限制为10.但我不知道如何显示下面的时间计算分页数。
任何解决方案?
谢谢。

i want to create pagination using while loop. I already show data limit to 10. But i have no idea how to show pagination number in time calculation below. Any solutions? Thank you.

<table border=0 width=800 cellspacing=1 cellpadding=2>
<tr height=22>
   <td align=center bgColor=red width=5%>No.</td>
   <td align=center bgColor=red width=15%>Date</td>
   <td align=center bgColor=red width=20%>Time</td>
   <td align=center bgColor=red width=15%>Status</td>
</tr>

<?php
    $start_hour = "05";
    $start_min = "00";
    $end_hour = "15";
    $end_min = "00";

    $start = new DateTime("$start_hour$start_min");
    $end = new DateTime("$end_hour$end_min");   
    $article_num = 1;
    $resultPerPage = 10;
    $pageNum = 1;

    while (($start < $end) && ($article_num <= $resultPerPage)) {
        $start_time = $start->format('H:i');
        $key_time = $start->format('Hi');
        $start->modify('+10 minute');
        $end_time = $start->format('H:i');

        $link_reserv = "#";

        echo ("<tr>");
        echo ("<td align=center>$article_num</td>");
        echo ("<td align=center>2013/12/16</td>");
        echo ("<td align=center>$start_time - $end_time</td>");
        echo ("<td align=center><input type=button value=Book&nbsp;Now onclick=\"window.location.href='$link_reserv';\"></td>");
        echo ("</tr>");

        $article_num++;
    }
?>
</table>


推荐答案

完整代码

<?php
$conn=mysql_connect("localhost","root","");
$sdb=mysql_select_db("pagination",$conn);

$tableName="emp_details"; //page name
$targetpage = "pagination.php";
$limit = 3; //limit of dispaly

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}

// Get page data
$sel = mysql_query("select * FROM $tableName LIMIT $start, $limit");
while($row1=mysql_fetch_array($sel))
{
$id=$row1['emp_id'];
$name=$row1['emp_name'];
$doj=$row1['doj'];
$experince=$row1['experince'];
$salary=$row1['salary'];
?>
<tr >
<td height="20"><?php echo $id;?></td>
<td><?php echo $name; ?></td>
<td><?php echo $doj; ?></td>
<td><?php echo $experince ?>year</td>
<td><?php echo $salary ?></td>

</tr>
<?php } ?>
<tr><td colspan="5" >
<?php

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;


$paginate = '';
if($lastpage > 1)
{

$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }


// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}

// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}

$paginate.= "</div>";


}
echo $total_pages.' Results';
// pagination
echo $paginate;
?></td></tr>
</table>

这篇关于使用while循环创建分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆