how to do how much clicking on the title goes to a full text page

Asked

Viewed 25 times

-2

i’m blogging using html,css and php but I don’t know how to do it for when I click on the title of the report be directed to a page with full text, same as the news portals. that when you click on the title of the report you go to a page with a different link and the full text. any doubt to answer the question and only ask.

<section class="postagens">

    <?php 

        /*sistema de pesquisa no blog*/
        if(isset($_POST['procurar'])){
        
            $pesquisa = $_POST['titulos'];          

            $sql = $pdo->prepare("SELECT * FROM posts WHERE titulo LIKE '%$pesquisa%'");

            $sql->execute(array($pesquisa));

            $info = $sql->fetchAll();
        }

    ?>

     <div class="container">
            <?php 
            
                /*pega todos as postagens no banco de dado*/
                foreach ($info as $key => $value) {
                    $titulos = $value['titulo'];
                    $resumo = $value['resumo'];
                    $imagem = $value['imagem']; 
            ?>
                    <div id="box">
                        <?php echo '<p> <img  src=" ' .$imagem. ' "></p> ' ?>
                        <?php echo "<h3>".$titulos."</h3>"?>
                        <?php echo '<p class="resumos">'.$resumo.'</p>' ?>
                    </div>
            <?php
                }
            ?>
        <div class="clear"></div>
     </div>
     
</section>
  • then, you select the report, then add the information on the page.

1 answer

0

Good night.

This is very common for news sites, I recommend you pass the news id through a GET and create a specific page to display news.

In this code that seems to me to be your home, it would only be necessary to involve your title with a link (something like <a href='url_da_pagina?id=id_noticia'>), but I’ll add some tips to improve your code.

On your home page

Starting with your database search. Assuming $_POST['titulos'] is a user input, I recommend using bindValue() this helps prevent SQL injections, my recommendation is NEVER PUT USER INPUT DIRECTLY INTO YOUR QUERY.

<?php 
/*sistema de pesquisa no blog*/
   if(isset($_POST['procurar']))
   {
    $pesquisa = $_POST['titulos'];          
    $sql = $pdo->prepare("SELECT * FROM posts WHERE titulo LIKE '%$pesquisa%'");
    $sql->execute(array($pesquisa));
    $info = $sql->fetchAll();
  }       
?>

I recommend you switch to that:

<?php 
   /*sistema de pesquisa no blog*/
   if(isset($_POST['procurar']))
   {
      //$pesquisa = $_POST['titulos'];          
      $sql = $pdo->prepare("SELECT * FROM posts WHERE titulo LIKE :pesquisa");
      $sql->bindValue(':pesquisa', "%{$_POST['titulos']}%");
      $sql->execute();
      $info = $sql->fetchAll();
   }       
?>

Now to link to the page:

<div class="container">
   <?php 
      /*pega todos as postagens no banco de dado*/
      foreach ($info as $key => $value)
      {
         /*$titulos = $value['titulo'];
         $resumo = $value['resumo'];
         $imagem = $value['imagem'];*/
         echo "<div id='box'>";
               echo "<p> <img  src='{$value['imagem']}'></p>";
               echo "<h3><a href='meu_link?id_noticia={$value['id_noticia']}'>{$value['titulo']}</a></h3>";
               echo "<p class='resumos'>{$value['resumo']}</p>";
         echo "</div>";
       }
    ?>
   <div class="clear"></div>
</div>

The important part is to surround your title with a tag <a>, where it is written 'meu_link' is the url for the new news page and the ?id_noticia={$value['id_noticia']} is to assemble the GET, otherwise I just changed the structure of the code to get more organized (in my opinion).

On your news page (that will have to be created)

Now to take this news specifically:

<?php 
       /*Buscar notícia em especifico */
       if(isset($_GET['id_noticia']))
       {      
          $sql = $pdo->prepare("SELECT * FROM posts WHERE id_noticia = :id");
          $sql->bindValue(':id', $_GET['id_noticia']);
          $sql->execute();
          $ret = $sql->fetchAll();
       }
       else
       {
          //Se não tiver o $_GET['id_noticia']
       }       
 ?>

Then just mount the html with the result

Browser other questions tagged

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