Doubt in news system

Asked

Viewed 41 times

0

I made a news system, ai na index.php aparece um breve resumo da notícia, ai quando a pessoa clica em "Ver mais" ela vê a notícia completa. But "See more" is a 'href'. And I don’t quite 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 that I created. But when one clicks on "See more", it gives "NOT FOUND". I wonder if you have how to use post method in this case, or what 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);
?>

1 answer

1


First check if the excerpt:

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

Is linking correctly with 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 the page noticia-visualizada.php The code is correct, after getting the id with $_GET you will find in the database the information of the news and format on HTML. Test the excerpt to print 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 a matrix (e.g., matrix (key => value, Chave2 => value2, Chave3 => Valor3, ...)). This array contains key/value pairs, where the keys are the names of the form controls and the values are the data of user input.

So much GET as POST are treated as $_GET and $_POST. These are superglobal, 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 using 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 body of the HTTP request).

  • Look at my DIT. I connected with the database to find the news. But it continues giving not found. link that is being generated: http://site.com.br/noticia-visualizedphp?id=7

  • 1

    Now it’s gone. Thank you very much

Browser other questions tagged

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