问答详情
源自:4-3 显示页码

那个计算偏移量哪里有点BUG

$pageoffset = ($showPage-1)/2;  

如果$showPage等于2的时候,页码会从0.5开始

提问者:Mcfly46 2019-02-20 17:04

个回答

  • 小小单身
    2019-06-21 12:57:34

    <!DOCTYPE html>

    <html>

    <head>

    <title></title>

    <meta charset="utf-8">

    <link rel="stylesheet" type="text/css" href="css/style.css">

    </head>

    <body>

    <?php

    include "conn.php";

    $page = isset($_GET['p']) ? $_GET['p'] : '';

    $page_size = 10;

    $page_from = ($page-1) * $page_size;

    $sql = "SELECT * FROM message LIMIT $page_from, $page_size";

    $res = $conn->query($sql);

    ?>

    <table class="one" border="1">

    <tr>

    <td>id</td><td>用户</td><td>内容</td><td>时间</td><td>操作</td>

    </tr>

    <?php while($row = $res->fetch_assoc()){ ?>

    <tr>

    <td><?php echo $row['id']; ?></td>

    <td><?php echo $row['username']; ?></td>

    <td><?php echo $row['mes']; ?></td>

    <td><?php echo $row['mestime']; ?></td>

    <td><a href="#">修改</a> | <a href="#">删除</a></td>

    </tr>

    <?php } ?>

    </table>

    <br /><br />

    <div class="page">

    <?php

    $page_banner = "";

    $sql = "SELECT * FROM message";

    $res = $conn->query($sql);

    $total_records = $res->num_rows;

    $total_pages = ceil($total_records / $page_size);


    $start = $page-5;

    $end = $page+4;

    if($page < 6){

    $start = 1;

    $end = 10;

    }

    if($page > $total_pages-4){

    $start = $total_pages-9;

    $end = $total_pages;

    }

    if($total_pages < 10){

    $start = 1;

    $end = $total_pages;

    }


    if($page > 1){

    $page_banner = "<a href='page.php?p=1'>首页</a>";

    $page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";

    }

    for($i=$start; $i<=$end; $i++){

    if($page == $i){

    $page_banner .= "<span class='current'>{$i}</span>";

    }else{

    $page_banner .= "<a href='page.php?p=".$i."'>".$i."</a>";

    }

    }

    if($page < $total_pages){

    $page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>";

    $page_banner .= "<a href='page.php?p=$total_pages'>尾页</a>";

    }

    echo $page_banner;

    echo "共".$total_pages."页";

    ?>

    </div>

    </body>

    </html>


  • 小小单身
    2019-06-21 12:53:43

    听的也有点蒙,他这个好像是根据每一页显示的5个页数计算的。你可以根据$showpage的值来重新计算一下$pageoffset的值。我写了个分页,可以参考一下

    <!DOCTYPE html><html><head>	<title></title>	<meta charset="utf-8">	<link rel="stylesheet" type="text/css" href="css/style.css"></head><body>	<?php	include "conn.php";	$page = isset($_GET['p']) ? $_GET['p'] : '';	$page_size = 10;	$page_from = ($page-1) * $page_size;	$sql = "SELECT * FROM message LIMIT $page_from, $page_size";	$res = $conn->query($sql);	?>	<table class="one" border="1">		<tr>			<td>id</td><td>用户</td><td>内容</td><td>时间</td><td>操作</td>		</tr>		<?php while($row = $res->fetch_assoc()){ ?>			<tr>				<td><?php echo $row['id']; ?></td>				<td><?php echo $row['username']; ?></td>				<td><?php echo $row['mes']; ?></td>				<td><?php echo $row['mestime']; ?></td>				<td><a href="#">修改</a> | <a href="#">删除</a></td>			</tr>		<?php } ?>	</table>	<br /><br />	<div class="page">	<?php	$page_banner = "";	$sql = "SELECT * FROM message";	$res = $conn->query($sql);	$total_records = $res->num_rows;	$total_pages = ceil($total_records / $page_size);	$start = $page-5;	$end = $page+4;	if($page < 6){		$start = 1;		$end = 10;	}	if($page > $total_pages-4){		$start = $total_pages-9;		$end = $total_pages;	}	if($total_pages < 10){		$start = 1;		$end = $total_pages;	}	if($page > 1){		$page_banner = "<a href='page.php?p=1'>首页</a>";		$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>";	}	for($i=$start; $i<=$end; $i++){		if($page == $i){			$page_banner .= "<span class='current'>{$i}</span>";		}else{			$page_banner .= "<a href='page.php?p=".$i."'>".$i."</a>";		}	}	if($page < $total_pages){		$page_banner .= "<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>";		$page_banner .= "<a href='page.php?p=$total_pages'>尾页</a>";	}	echo $page_banner;	echo "共".$total_pages."页";	?>	</div></body></html>