2015-12-16 56 views
0

One record 10 records loop 我有麻煩顯示正確的記錄,結束後,我需要使用分頁。 v_news.php 如何顯示每頁10條記錄,然後如何使用分頁?

<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循環我有一個錯誤的非法串$行[ '']偏移。我不知道該怎麼做。

回答

0

首先嚐試在v_news.php中修復第二行代碼。 你忘了使用「回聲」。

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

變化:

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

這是我的錯。我重寫了這幾次。 –

0

fetch_assoc()取一排,不是全部。

您可以使用while循環,一旦fetch_assoc()停止返回新行,該while循環就會結束。

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

對於分頁,你只需要改變參數LIMIT

LIMIT 10,10將是第二頁面數據(10點總的行開始行10)。

也作爲bobiczeq指出,你應該確保echo聲明出現在<?php $row['id_article']; ?>否則這只是空的。

+0

我使用fetch_all()而不是fetch_assoc()。但現在它向我展示了未定義的索引:id_artcle,title,itd。 –

+0

做'var_dump($ row);'看看它顯示的是什麼'$ row'在v_news.php – skrilled

+0

它顯示我「null」 –

0

我找到了正確的答案。

$rows = mysqli_fetch_all($result, MYSQLI_BOTH); 

現在它工作正常。

+0

你在回答中提問。這不是它在這裏的工作原理。如果你有另一個問題,那麼你需要問另一個問題,而不是在你的答案。 –

+0

我編輯你的答案,刪除你的分頁尋求幫助。 –

相關問題