Select limiting query, and does not display the latest queries

Asked

Viewed 91 times

0

In this script, it is limited to the display of 6 queries in the SELECT variable $quantidade_pg = 6, I used the IF to inform that all news situations other than 2 (unpublished) should be displayed.
The detail is that in my SELECT, in order of creation from the latest to the oldest, the detail is that when limiting 6 news, and is with the oldest date does not display these news.
Example: I have a total of 10 news, 1 per week, but the last 6 news is displayed in order of the most recent the oldest, but when unpublishing 6 news, in the middle weeks, leaving the 2 latest news and the 2 latest news, should be shown, but in this script only displays the latest 2 not displaying the older ones. It seems that IF is limited to SELECT, and I understand that it has 6 queries.
I searched the net to limit in while or Aldo type, or INNER JOIN in SELECT. Someone could help me. I have this code:

        $pagina = (isset($_GET['pagina']))? $_GET['pagina'] : 1;
        //Selecionar os noticias a serem apresentado na página da mais recente a antiga
        $result_noticias = "SELECT * FROM noticias ORDER BY created DESC";

        //Selecionar os noticias a serem apresentado na página
        $resultado_noticias = mysqli_query($conn, $result_noticias);

        //Contar o total de noticias
        $total_noticias = mysqli_num_rows($resultado_noticias);

        //Seta a quantidade de noticias por pagina
        $quantidade_pg = 6;

        //calcular o número de pagina necessárias para apresentar os noticias
        $num_pagina = ceil($total_noticias/$quantidade_pg);

        //Calcular o inicio da visualizacao
        $incio = ($quantidade_pg*$pagina)-$quantidade_pg;

        //Selecionar os noticias a serem apresentado na página
        $result_noticias = "SELECT * FROM noticias ORDER BY created DESC limit $incio, $quantidade_pg";
        $resultado_noticias = mysqli_query($conn, $result_noticias);
        $total_noticias = mysqli_num_rows($resultado_noticias);
                <?php while($row_noticias = mysqli_fetch_assoc($resultado_noticias)){?>
">

...

But in my bank I have the news table and the column situaca_noticia_id (id1/id2).

I have the other table situaca_noticia, in it the columns (id/name), thus (id1/published)-(id2/unpublished).

I would like when the ID is 2 (not published), not to show the news at while.
My knowledge is lacking.

  • If you have a foreign key of the news table in the table situacao_noticia you could use an INNER in the query and only return what did not have the ID 2.

1 answer

1


Try to put your entire article inside an if/Else. No if you check if the news id is 2, if it is, just continue, if not, have the Else block run with the article inside.

  • Thank you @ikaro - that’s right. <?php if($row_noticias['situacao_noticia_id']=2) { ? > While.... <? php } Else {} ?>

  • Face magic, if the answer helped in the solution, mark as right for others to see and help them too ;)

  • Editing the right way. I created a table with 2 columns, table=situacao_noticia - id column (Auto increment, name column (Published/unpublished). E before the div <article> inserts an if <?php if($row_noticias['situacao_noticia_id']!= 2) { ? > != (other than 2) after </article> uses an empty Else {} . Only to help with understanding and correcting the first comment.

  • Solved in a very simple way, I searched about SELECT and simply removed the following items: $num_pagina = ceil($total_noticias/$quantidade_pg); $incio = ($quantidade_pg*$pagina)-$quantidade_pg; E removed the variable &incision of SELECT and added the condition WHERE, which is up to obvious: $result_noticias = "SELECT * FROM noticias WHERE situacao_noticia_id = 1 ORDER BY created DESC limit $quantidade_pg"; E removed the IF, no matter the date, always displays the published ones of ID 1 and displays the amount of 6. Thanks to those who helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.