简体   繁体   English

限制单页中的帖子数

[英]limit number of posts in single page

I wanna limit number of posts on page. 我想限制页面上的帖子数。 Here is code i am using :- 这是我正在使用的代码:-

<?php 
                            $query="select * from namedb";
                            $result=mysqli_query($connect,$query);
                            while($r=mysqli_fetch_assoc($result))
                            {
                            ?>
                        <tr>
                            <td><?php echo $r['title']?></td>
                            <td><?php echo $r['comment']?></td>
                            <td><?php echo $r['cgender']?></td>
                            <td><?php echo $r['cnumerology']?></td>
                            <td><?php echo $r['creligion']?></td>
                        </tr>
                        <?php } ?>

and using this code for pagination 并使用此代码进行分页

<?php
                                    $query="select * from namedb";
                                    $result=mysqli_query($connect,$query);
                                    $count=mysqli_num_rows($result);
                                    $pages=ceil($count/10);
                                    for($i=1;$i<=$pages;$i++) { ?>
                                        <a href="links.php?p=<?php echo $i; ?>">
                                        <?php echo $i;?></a>
                                <?php } ?>

There are 21 posts in my database and 20 posts are showing on my page but pagination is showing as 1 2 3 and 2nd & 3rd page is showing same data. 我的数据库中有21个帖子,页面上显示20个帖子,但分页显示为1 2 3,第二和第三页显示相同的数据。 how to fix it. 如何解决。

I wanna show 30 records per page and if page has less then 30 records then pagination will not show. 我想每页显示30条记录,如果页面少于30条记录,则不会显示分页。

There is nothing in your code that actually does any paging when you get the names from the database. 从数据库中获取名称时,您的代码中实际上没有任何页面调度。 Change the first part to the following code and keep the 2nd part of the code same. 将第一部分更改为以下代码,并保持第二部分相同。

$page = 1;
$per_page = 10;
if(isset($_GET['p']) && is_numeric($_GET['p']) )
    $page= $_GET['p'];
$offset = ($page -1) * $per_page;

$query="select * from namedb LIMIT $offset, $per_page  ";
$result=mysqli_query($connect,$query);
while($r=mysqli_fetch_assoc($result))
{ ?>
    <tr>
    <td><?php echo $r['title']?></td>
    <td><?php echo $r['comment']?></td>
    <td><?php echo $r['cgender']?></td>
    <td><?php echo $r['cnumerology']?></td>
    <td><?php echo $r['creligion']?></td>
    </tr>
<?php } ?>

I think what you're looking for is the MySQL LIMIT command. 我认为您正在寻找的是MySQL LIMIT命令。 For instance, by writing 例如,通过写

SELECT * FROM namedb LIMIT 0,30
SELECT * FROM namedb LIMIT 30,30
SELECT * FROM namedb LIMIT 60,30
...

you will get the first, second, third batch of 30 results. 您将获得第一,第二,第三批的30个结果。 Here, the first number is giving the offset (note that it starts with 0, so 30 actually gives the 31st result), while the second number tells how many entries will be returned. 在这里,第一个数字给出偏移量(请注意,它以0开头,所以30实际上给出了第31个结果),而第二个数字表示将返回多少个条目。

You can integrate this into the first part of your script by calculating the offset from the GET parameter p , like so: 您可以通过计算GET参数p的偏移量将其集成到脚本的第一部分中,如下所示:

 $length_of_page = 30;

 $page = intval($_GET["p"]);
 $offset = $page * $length_of_page;
 $query = "SELECT * FROM namedb LIMIT $offset, $length_of_page"

The intval function makes sure that you really insert a number into the query. intval函数可确保您确实在查询中插入了一个数字。 This is important to avoid SQL injection. 这对于避免SQL注入很重要。

Use following code 使用以下代码

<?php 
                            $query="select * from namedb limit 0,30";
                            $result=mysqli_query($connect,$query);
                            while($r=mysqli_fetch_assoc($result))
                            {
                            ?>
                        <tr>
                            <td><?php echo $r['title']?></td>
                            <td><?php echo $r['comment']?></td>
                            <td><?php echo $r['cgender']?></td>
                            <td><?php echo $r['cnumerology']?></td>
                            <td><?php echo $r['creligion']?></td>
                        </tr>
                        <?php } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM