[英]Limit posts per page in PHP
我需要限制我網站主頁上顯示的帖子數量。
目前看起來是這樣的: http://prntscr.com/j5nhng
我希望能夠限制每頁 3 個帖子。 不知是否可以<href =.
可能有一些錯誤,但我很感激你能給我的任何幫助。 從已經謝謝你
我的代碼,如果你需要任何變量:
<title>Noticias</title>
<?php
// Connects to the database
include('admin/config.php');
// Selects the ' News ' table where the news data gets
$selecionar_db = "SELECT * FROM news ORDER BY id DESC";
// Faz a Conexão com o banco de dados
$final = mysql_query($selecionar_db)
// Message if you have an error with the database
or die ("<h1>Erro ao Conectar-se ao Banco de dados</h1>");
// Picks up the values from the "news" table
while ($news=mysql_fetch_array($final)) {
$id = $news["id"];
$titulo = $news["titulo"];
$categoria_id = $news["categoria"];
$autor = $news["autor"];
$views = $news["views"];
$texto = $news["texto"];
$date = $news["date"];
// Altera o Formato da data da noticia
$date2 = strtotime($date);
$data = date('d/m/Y', $date2);
$hora = date('H:i', $date2);
// Pega o número de Comentários que a noticia possui
$comentarios_db = "SELECT * FROM comentarios WHERE noticia_id='$id'";
$comentarios_db = mysql_query($comentarios_db);
$comentarios = mysql_num_rows($comentarios_db);
// Faz a seleção da Categoria
$categoria_db = "SELECT * FROM categorias WHERE id='$categoria_id'";
$categoria_resultado = mysql_query($categoria_db);
$categoria_final = mysql_fetch_assoc($categoria_resultado);
$categoria = $categoria_final['categoria'];
echo "<h1><a href=\"noticia.php?id=$id\">$titulo</a></h1> <p>Postado por <b>$autor</b> em <b>$data</b> ás <b>$hora</b> <p>$texto</p>";
}
?>
您可以使用 SQL 中的“[LIMIT][1]”來執行此操作。只需將頁碼存儲在 PHP 中的一個變量中,然后在另一個變量中獲取每頁的結果數。 然后根據您的頁面計算要顯示的第一行。 例如:如果你想每頁顯示 10 個結果,而你在第二頁,那么你應該從第 11 行開始顯示結果。
這是一個例子:
$page = $_GET['page']; // Let say it stores 2
$resultsPerPage = 10;
$startFrom = ($page-1)*($resultsPerPage); // Will display 10
然后你的 SQL 應該是這樣的:
$comentarios_db = "SELECT * FROM commentaries WHERE noticia_id='$id' LIMIT ".$startFrom.", ".$resultsPerPage;
SELECT * 來自新聞 ORDER BY id DESC LIMIT 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.