php mysql页码,MySQL / PHP-页码分页有时仅显示10页

这篇博客讨论了在PHP和MySQL环境中遇到的页面加载问题。作者当前的代码将所有50页内容一次性显示在同一页面上。目标是限制每页仅显示10条记录,并实现分页导航。通过调整代码,实现了根据当前页数动态显示相应页面范围的功能,允许用户逐页浏览剩余内容。主要涉及PHP变量、SQL查询和分页逻辑。
摘要由CSDN通过智能技术生成

I am having difficulties with my MYSQL / PHP dashboard. - Currently i am having 50 pages, but currently they are all showing on the same page.

http://imgur.com/wDfTWUa - as you can see in the attached file. - I only want 10 pages to be shown, and be able to click through the rest of the pages without seeing 4 rows of pages.

Exsampel when you are on ?page=1, if you are on page ?page=10

Hope you can help me.

Code:

include 'config.php';

$sidenr = $_GET['page'];

$sidenr2 = ($sidenr -1) * 10;

echo $sidenr2;

echo "
";

$query100 = mysqli_query($conn, "SELECT * FROM `test` LIMIT $sidenr2,10") or die(mysqli_error($conn));

while($row = mysqli_fetch_array($query100))

{

echo $row['id']."
";

}

$result = mysqli_query($conn, "SELECT * FROM test");

$num_rows = mysqli_num_rows($result);

$sideantal = $num_rows / 10;

echo "Der skal være antal sider: ". $sideantal;

echo "
antal rækker ". $num_rows . "
";

?>

for ($number = 1; $number <= $sideantal; $number++) {

echo "

". $number. "";

}

?>

解决方案

function getPageRange($current, $max, $total_pages = 10) {

$desired_pages = $max < $total_pages ? $max : $total_pages;

$middle = ceil($desired_pages/2);

if ($current <= $middle){

return [1, $desired_pages];

}

if ($current > $middle && $current <= ($max - $middle)) {

return [

$current - $middle,

$current + $middle

];

}

if ($current <= $max ) {

return [

$current - ($desired_pages - 1),

$max

];

}

}

list($min,$max) = getPageRange($sidenr, $sideantal);

foreach (range($min, $max) as $number) {

echo "

". $number. "";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值