我正在使用bootstrap分页,以及显示我的结果的组合PHP / mysql方法(例如,每个页面的结果.php?results = 1)。这工作正常,但实际上在页面底部显示分页div,即哪一个是活动的,无论您是否可以单击以下或之前的项目(如果这些页面上确实有结果)
所以我已经针对几乎我认为会影响任何事情的所有组合做了一个特定的案例,但它肯定没有效率。谁能建议一个更好的方法呢?有些情况会很明显,比如我在第一页,我不想在页面中显示第0页和第1页,其他则不太明显,就像我在第3页,并且有没有更多的页面,我希望第3页在中间,因为有5个数字显示,但你不能点击4和5.
我正在使用插件的精确副本显示here,除了我在第一页/最后一页之外添加左右增加/减少一个。
<div id="pagination" style="width: 340px; margin-left: auto; margin-right: auto;">
<? if ($results == 1) { ?>
<ul class="pagination">
<li class="disabled"><a href="#"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li class="disabled"><a href="#"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<li class="active"><a href="#"><? echo $results; ?> <span class="sr-only">(current)</span></a></li>
<li <?if ($num_rows < $num_res) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
<li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 3); ?>"><? echo $results + 3; ?></a></li>
<li <?if ($num_rows < ($num_res * 4)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 4); ?>"><? echo $results + 4; ?></a></li>
<li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li <?if ($num_rows < ($num_res)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
</ul>
<? } ?>
<? if ($results == 2) { ?>
<ul class="pagination">
<li><a href="<? echo $url."&results=".(1); ?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
<li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?> <span class="sr-only">(current)</span></a></li>
<li <?if ($num_rows < ($num_res * 2)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 3); ?>"><? echo $results + 3; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
</ul>
<? } ?>
<? if (($results == 3) && (($last_page == 3))) { ?>
<ul class="pagination">
<li><a href="<? echo $url."&results=".(1); ?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
<li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?><span class="sr-only">(current)</span></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
</ul>
<? } ?>
<? if (($results > 2) && ($results < ($last_page - 1))) { ?>
<ul class="pagination">
<li><a href="<? echo $url."&results=".(1); ?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
<li class="active"><a href="<? echo $url."&results=".($results); ?>"><? echo $results; ?><span class="sr-only">(current)</span></a></li>
<li><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
<li><a href="<? echo $url."&results=".($results + 2); ?>"><? echo $results + 2; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
</ul>
<? } ?>
<? if (($results > 2) && ($results == ($last_page - 1))) { ?>
<ul class="pagination">
<li><a href="<? echo $url."&results=".(1); ?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 3); ?>"><? echo $results - 3; ?></a></li>
<li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?></a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
<li class="active"><a href="<? echo $url."&results=".($results ); ?>"><? echo $results ?><span class="sr-only">(current)</span></a></li>
<li><a href="<? echo $url."&results=".($results + 1); ?>"><? echo $results + 1; ?></a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($results + 1); ?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li <?if ($num_rows < ($num_res * 3)) { echo "class=\"disabled\""; } ?>><a href="<? echo $url."&results=".($last_page); ?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
</ul>
<? } ?>
<? if (($results > 2) && ($results == $last_page) && ($results != 3)){ ?>
<ul class="pagination">
<li><a href="<? echo $url."&results=".(1); ?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<li><a href="<? echo $url."&results=".($results - 4); ?>"><? echo $results - 4; ?> </a></li>
<li><a href="<? echo $url."&results=".($results - 3); ?>"><? echo $results - 3; ?></a></li>
<li><a href="<? echo $url."&results=".($results - 2); ?>"><? echo $results - 2; ?></a></li>
<li><a href="<? echo $url."&results=".($results - 1); ?>"><? echo $results - 1; ?></a></li>
<li class="active"><a href="<? echo $url."&results=".($results ); ?>"><? echo $results ?><span class="sr-only">(current)</span></a></li>
<li class="disabled"><a href="#"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li class="disabled"><a href="#"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
</ul>
<? } ?>
</div>
如果没有人感觉像是帮助了第一部分,我明白,就我所知,它是有效的。
但我也想知道在我的class="disabled"
上是否有办法删除hrefs?或者我只需要在每一个上加上if语句?感谢。
编辑:基于@ Scopey的建议。我改变了一些我认为合适的东西,以及几个变量名称。
$numberOfPages = ceil($num_rows / $num_res);
$numberOfPages = (int)$numberOfPages;
$url = explode('&', $_SERVER['REQUEST_URI']);
function myFilter($string) {
return strpos($string, 'results=') === false;
}
$url = array_filter($url, 'myFilter');
$url = implode('&', $url);
?>
<ul class="pagination">
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . 1;
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-double-left"></i> </a></li>
<li<?php if($results === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== 1){
echo $url.'&results=' . ($results - 1);
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-left"></i> </a></li>
<?php
// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;
// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i <= $pageCount; $i++)
{
// Echo out just the beginning of the <li> tag as we don't yet
// know if this needs to be disabled...
echo '<li';
// Added to show if current page is active
if($i === $results) {
echo ' class="active"';
}
// Check if:
// - This current page is greater than the amount of pages of
// results we have
// - OR, this is the currently selected page
if($i > $numberOfPages || $results === $i)
{
// <li> tag needs the disabled class....
echo ' class="disabled"';
}
// Finish the <li> tag and start generating the link
echo '><a href="';
// Opposite logic from above... Only if this is a page we know about
// AND this is not the current page.
if($i < $numberOfPages && $results !== $i)
{
// The link will be your page, with the get param, and the
// current page number we're printing
echo $url.'&results=' . $i;
}
else
{
// Otherwise just a # (no link)
echo '#';
}
// Finish this page... Print the page number ($i)
echo '">' . $i . '</a></li>';
}
?>
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . ($results + 1);
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-right"></i> </a></li>
<li<?php if($results === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($results !== $numberOfPages){
echo $url.'&results=' . $numberOfPages;
} else { echo '#'; }
?>"> <i class="fa fa-lg fa-angle-double-right"></i> </a></li>
<?php
@Scobey,除了一些东西之外,这个和预期的一样好用,和我原来的帖子一样。在我的例子中,我能够将当前页面放在中间。例如,&lt;&lt; 1 | 2 | 3 | 4 | 5&gt;&gt;,这就是为什么我有很多不同的语句,包括+1和-1,+ 2,-2等。
其次,我的目的是始终显示5个页面选项,而不是至少五个。所以,如果我在第6页,我只会看到&lt;&lt; 4 | 5 | 6 | 7 | 8>&gt;,而不是1-8。可能没有必要这样做,这是一件好事。但是,我不想达到我在分页中有超过50个不同页码的地步。
答案 0 :(得分:3)
一般情况下,动态分页仅在您有一些事情时才有效
想象一下,您有一个名为Results
的数据库表,它在您的分页页面上有一大堆您想要的结果
<强> 1。计算所有可能的结果
这可以通过简单的SQL语句实现:
SELECT count(*) FROM Results
<强> 2。每页金额
这是您自己决定的价值。
第3。当前页码
默认情况下通常为1,但是当更改分页时,它会作为get参数传递。
<强> 4。表格中的一小部分结果
可实现:
SELECT * FROM Results LIMIT 0, 20
此示例在结果表中检索前20个结果...
<强> 5。分页
现在,使用20个结果作为默认值&#34;每页计数&#34;
$countPerPage = 20;
// Get the total number of results
$result = pg_query('SELECT count(*) FROM Results'); // I'm using PostgreSQL for this example
$totalResultCount = (int)pg_fetch_result($result, 0, 0);
// The ceil function will round floats up.
$numberOfPages = ceil($totalResultCount / $countPerPage);
// Check if we have a page number in the _GET parameters
if(!empty($_GET) && isset($_GET['page']))
{
$page = (int)$_GET['page'];
}
else
{
$page = 1;
}
// Check that the page is within our bounds
if($page < 0)
{
$page = 1;
}
elseif($page > $numberOfPages)
{
$page = $numberOfPages;
}
// Build the query for the results...
$query = 'SELECT * FROM Results LIMIT ' . ($page - 1) * $countPerPage . ', ' . $countPerPage;
$results = pg_fetch_all(pg_query($query));
// Deal with printing your results etc...
?>
<ul class="pagination">
<li<?php if($page === 1): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== 1){
echo 'page.php?page=' . $page - 1;
} else { echo '#'; }
?>">«</a></li>
<?php
// Print the pagination...
// Minimum of 5 pages in the pagination, even if there aren't 5 pages...
$pageCount = ($numberOfPages < 5) ? 5 : $numberOfPages;
// Loop through from page 1 until the last page ($pageCount)
for($i = 1; $i < $pageCount; $i++)
{
// Echo out just the beginning of the <li> tag as we don't yet
// know if this needs to be disabled...
echo '<li';
// Check if:
// - This current page is greater than the amount of pages of
// results we have
// - OR, this is the currently selected page
if($i > $numberOfPages || $page === $i)
{
// <li> tag needs the disabled class....
echo ' class="disabled"';
}
// Finish the <li> tag and start generating the link
echo '><a href="';
// Opposite logic from above... Only if this is a page we know about
// AND this is not the current page.
if($i < $numberOfPages && $page !== $i)
{
// The link will be your page, with the get param, and the
// current page number we're printing
echo 'page.php?page=' . $i;
}
else
{
// Otherwise just a # (no link)
echo '#';
}
// Finish this page... Print the page number ($i)
echo '">' . $i . '</a></li>';
}
?>
<ul class="pagination">
<li<?php if($page === $numberOfPages): ?> class="disabled"<?php endif; ?>><a href="<?php
if($page !== $numberOfPages){
echo 'page.php?page=' . $page + 1;
} else { echo '#'; }
?>">»</a></li>
<?php
<强>声明强>
我只是坐在这里写了这篇文章,我还没有测试过,但总的想法就在那里。希望这是有道理的。我已经没时间了,没有时间测试或改进它,但如果您有兴趣我可以稍后解决。
希望这有帮助!