英文:
pagination php is not working, getting stuck on the first one, even tho if i echo the variables that store the page data are correct
问题
我正在尝试为我的CRUD PHP应用程序添加分页,但它不正常工作。
尝试将数据从我的SQL数据库限制为每页5条,但它卡在第一页。如果我点击第二页,它仍然显示第一页的数据。
$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$perPage = (isset($_GET['per-page']) && ($_GET['per-page']) <= 50 ? $_GET['per-page'] : 5);
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$sql = "select * from movies limit ".$start." , ".$perPage." ";
$total = $db->query("select * from movies")->num_rows;
$pages = ceil($total / $perPage);
在下面的HTML代码中,我正在循环遍历页面。
<center>
<ul class="pagination">
<?php for($i = 1; $i <= $pages; $i++): ?>
<li>
<a href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
</ul>
</center>
我在页眉中使用Bootstrap类来进行分页。为什么我点击第二页时仍然停留在第一页?在我的网站项目中,您可以在URL栏中看到我在第二页,但仍然显示第一页的数据。在我的数据库中有10个元素,所以应该是5和5。
英文:
I am trying to add pagination to my CRUD PHP app but it's not working properly.
Trying to limit the data from my SQL database to 5 per page, but it's getting stuck to the first page. If I press the second one, stays on the first page data.
$page = (isset($_GET['page']) ? $_GET['page'] : 1);
$perPage = (isset($_GET['per-page']) && ($_GET['per-page']) <= 50 ? $_GET['per-page'] : 5);
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$sql = "select * from movies limit ".$start." , ".$perPage." ";
$total = $db->query("select * from movies")->num_rows;
$pages = ceil($total / $perPage);
And here I am looping through the pages in the HTML code below the PHP one.
<center>
<ul class="pagination">
<?php for($i = 1; $i <= $pages; $i++): ?>
<li>
<a href="?pages = <?php echo $i; ?>"><?php echo $i; ?></a>
</li>
<?php endfor; ?>
</ul>
</center>
I am using bootstrap class for pagination which is added with a script in the head. Any idea why I stay at the first page even when I press the second one?
Here is a photo from my web project, in the url bar u can see i am at the second page, but still showing the data from first. In my database i have 10 elements, so it should be 5 and 5.
答案1
得分: 1
看起来你在链接中使用了错误的变量名。应该是:
<a href="?page=<?php echo $i; ?>">
(注意,是 page
而不是 pages
。还要删除会在 URL 中被编码为 %20 的空格)
但要非常小心将用户提交的数据放入你的查询中,搜索“SQL 注入”以获取更多信息。
英文:
Looks like you are using the wrong variable name in your link. It should be:
<a href="?page=<?php echo $i; ?>">
(note, page
not pages
. Also, remove the spaces which get encoded as %20 in the url)
Be vary careful of putting user-submitted data into your query though, google "SQL injection" to find out more.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论