News System Doubt

0

I made a news system, there at index.php appears a brief summary of the news, there when the person clicks on "See more" she sees the full news. But "See more" is a 'href'. And I do not know how to handle GET method in php.

Code that is in index.php:

<div>
<?php
$noticias = listaNoticia($conexao);
foreach ($noticias as $noticia){
?>
<div class="panel panel-primary">
<div class="panel-heading"><?=date("d/m/Y",strtotime($noticia['data']))."  -   ".$noticia['titulo'] ?></div>
<div class="panel-body">
<?=substr($noticia['noticia'],0 , 60)?><a href="noticia-visualizada.php?id=<?=$noticia['id']?>">Ver mais...</a>
</div>
</div>
<?php
}
?>
</div>

The news is being saved in a database, through a little Panel I created. But when the person clicks on "See more", it gives "NOT FOUND". I would like to know if you have how to use the post method in this case, or what is the solution to generate the page with the news ID ...

Page code news-viewed.php:

<?php
include ("conexao.php");
$id = $_GET['id'];
$query = "select * from sistemanoticias where id = $id";

$resultado = mysqli_query($conexao,$query);
$visu = mysqli_fetch_assoc($resultado);
?>
    
asked by anonymous 07.11.2017 / 11:13

1 answer

1

First check the snippet:

<?=substr($noticia['noticia'],0 , 60)?><a href="noticia-visualizada.php?id=<?=$noticia['id']?>">Ver mais...</a>

You are linking correctly to the news id, otherwise try to put a echo

<?php echo substr($noticia['noticia'],0 , 60); ?><a href="noticia-visualizada.php?id=<?php echo $noticia['id']; ?>">Ver mais...</a>

And on page noticia-visualizada.php The code is correct, after you get the id with $_GET you will fetch the news information from the database and format it in HTML . Test the excerpt to start the news:

<?php
include ("conexao.php");
$id = $_GET['id'];
$query = "SELECT * FROM sistemanoticias WHERE id = ".$id;
if ($result = mysqli_query($conexao, $query)) {

    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row['id'], $row['titulo']); //ou
        echo "Id: ".$row['id']." - Titulo: ".$row['titulo'];
    }

    mysqli_free_result($result);
}

GET vs. POST

GET and POST create an array (for example, array (key => value, key2 = > value2, key3 = > value3, ...)). This array contains key / value pairs, where keys are the names of form controls and values are the data of user input.

Both GET and POST are treated as $_GET and $_POST . These are superglobals, which means they are always accessible regardless of range - and you can access them from any function, class or file without having to do anything special.

$_GET is an array of variables passed to the current script through the URL parameters. Information sent from a form with the GET method are visible to all (all variable names and values are displayed in the URL).

$_POST is an array of variables passed to the current script via the HTTP POST method. Information sent from a form with the method POST are invisible to others (all names / values are embedded in the body of the HTTP request).

    
07.11.2017 / 11:27