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

huangapple go评论59阅读模式
英文:

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[&#39;page&#39;]) ? $_GET[&#39;page&#39;] : 1);
$perPage = (isset($_GET[&#39;per-page&#39;]) &amp;&amp; ($_GET[&#39;per-page&#39;]) &lt;= 50 ? $_GET[&#39;per-page&#39;] : 5);
$start = ($page &gt; 1) ? ($page * $perPage) - $perPage : 0;

$sql = &quot;select * from movies limit &quot;.$start.&quot; , &quot;.$perPage.&quot; &quot;;

$total = $db-&gt;query(&quot;select * from movies&quot;)-&gt;num_rows;

$pages = ceil($total / $perPage);

And here I am looping through the pages in the HTML code below the PHP one.

&lt;center&gt;
						&lt;ul class=&quot;pagination&quot;&gt;
							&lt;?php for($i = 1; $i &lt;= $pages; $i++): ?&gt;
							&lt;li&gt;
								&lt;a href=&quot;?pages = &lt;?php  echo $i; ?&gt;&quot;&gt;&lt;?php echo $i; ?&gt;&lt;/a&gt;
							&lt;/li&gt;
						&lt;?php endfor; ?&gt;
						&lt;/ul&gt;
					&lt;/center&gt;

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.

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

答案1

得分: 1

看起来你在链接中使用了错误的变量名。应该是:

&lt;a href=&quot;?page=&lt;?php echo $i; ?&gt;&quot;&gt;

(注意,是 page 而不是 pages。还要删除会在 URL 中被编码为 %20 的空格)

但要非常小心将用户提交的数据放入你的查询中,搜索“SQL 注入”以获取更多信息。

英文:

Looks like you are using the wrong variable name in your link. It should be:

&lt;a href=&quot;?page=&lt;?php  echo $i; ?&gt;&quot;&gt;

(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.

huangapple
  • 本文由 发表于 2020年1月6日 20:43:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定