在php中限制每页的帖子数

siotufzp  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(222)

我需要限制多少帖子出现在我的网站主页上。
当前显示如下: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>";

}
?>
jecbmhm3

jecbmhm31#

从新闻顺序中选择*按id描述限制3

cpjpxq1n

cpjpxq1n2#

您可以使用sql中的“limit”来实现这一点。只需将页码存储在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 comentarios WHERE noticia_id='$id' LIMIT ".$startFrom.", ".$resultsPerPage;

请记住,它将从第11行开始,因为我们将查询限制为偏移量10

相关问题