-1

I need to limit how many posts appear on the homepage of my site.

It currently appears like this: http://prntscr.com/j5nhng

I want to be able to limit 3 posts per page. I do not know whether to be <href =. There may be some mistake, but I appreciate any help you can give me. From already thank you

My code if you need any variable:

<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>";


}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

0

You can do this with '[LIMIT][1]' in SQL. Just store the page number in a variable in your PHP, and get the number of results per page in another variable. Then calculate the first row to display based on your page. For example: if you want to display 10 results per page, and you are on the second page, then you should start displaying results from row 11.

Here is an example:

$page = $_GET['page']; // Let say it stores 2
$resultsPerPage = 10;

$startFrom = ($page-1)*($resultsPerPage); // Will display 10

AND then your SQL should look like this :

$comentarios_db = "SELECT * FROM commentaries WHERE noticia_id='$id' LIMIT ".$startFrom.", ".$resultsPerPage;
Mostafa Ghorbani
  • 397
  • 1
  • 3
  • 15
Niv Apo
  • 983
  • 1
  • 9
  • 18
-3

SELECT * FROM news ORDER BY id DESC LIMIT 3

sjake
  • 1
  • 1
  • 1
    It is just common courtesy to let the poster know why he was down voted. He is obviously new to SO. He seems to be eager to help--so we should help him by letting him know why he was down voted. – Barns Apr 15 '18 at 19:59
  • 2
    Drop-in code without an explanation of sorts, is of rather very low-quality. – Funk Forty Niner Apr 15 '18 at 19:59
  • 1
    @Funk Forty Niner I agree, with your reason. sjake should put more effort into answering the question. Simply adding code with no explanation is not helpful in most cases. – Barns Apr 15 '18 at 20:01
  • 2
    You really need to write a complete question. What are you trying to do, what's going wrong - including the error message. You've given us nothing to work with. – Steve Apr 15 '18 at 20:02
  • @Barns agreed on the (first) comment and then some ;-) – Funk Forty Niner Apr 15 '18 at 20:03