如何每页显示10条记录,然后如何使用分页


How to display 10 records per page and then how to use pagination?

一条记录10条记录循环我在正确显示记录方面遇到了问题,之后我需要使用分页。v_news.php

<div class="news">
        <a href="index.php?ogloszenie='<?php $row['id_article']; ?>'">
    <div class="newstitle">
       <?php echo $row['title'];?>
    </div>
    <div class="newsauthor">
        <?php echo $row['username'];?>
    </div>
            <div class="newscomment">
                comments: 56
            </div>
    <div class="newsdate">
        <?php echo $row['timestamp'];?>
    </div>
        </a>
</div>
m_news.php

<?php
if(!isset($_SESSION['id'])){
    header("Location: index.php");
    exit();
}else{
    
    $result = $Database->query("SELECT * FROM article LIMIT 0, 10");
    if($result->num_rows != 0){
        $rows = $result->fetch_assoc();
        foreach ($rows as $row){
        include("views/v_news.php");
    
    } 
        
    
    }else{
        echo "No results";
    }
    
}

对于foreach循环,我有一个错误,非法字符串偏移量为$row["]。我不知道该怎么办。

首先尝试修复v_news.php中的第二行代码。你忘了使用"echo"。

<a href="index.php?ogloszenie='<?php $row['id_article']; ?>'">

更改为:

<a href="index.php?ogloszenie='<?php echo $row['id_article']; ?>'">

fetch_assoc()获取一行,而不是全部。

您可以使用while循环,一旦fetch_assoc()停止返回新行,该循环就会结束。

while($row = $result->fetch_assoc()){
    include("views/v_news.php");
}

对于分页,您只需要更改LIMIT参数

即CCD_ 3将是第二页数据(从行10开始总共10行)。

正如bobiczeq所指出的,您应该确保echo语句出现在<?php $row['id_article']; ?>中,否则它只是空的。

我找到了正确的答案。
$rows = mysqli_fetch_all($result, MYSQLI_BOTH); 

现在它工作正常。