PHP obter id de artigos.pai
Tenho um artigo.php page e um Single.php page. Artigo.php executa um loop foreach listando todos os artigos. A âncora href para cada artigo é:
<a href="single.php?id=<?php echo $article['id'];
Quando a ligação ao artigo estiver carregue no URL que se torna:
example.com/single.php?id=*ID*
Estou a ter dificuldade em apanhar o ID do artigo na única página para mostrar a linha MySQL específica desse id. Foi sugerido o seguinte:
$id = filter_var($_GET['id'] ?? false, FILTER_VALIDATE_INT);
if($id !== false){
//show the article, i.e. select * from .... where id = id ...
echo "WORKING";
}else{
//show the error like 404
echo "ERROR";
}
isto deve ser:
$id = $_GET($article['id'])
Estou a ter dificuldade em fazer com que isto resulte.
0
3 answers
Enviar o valor para outra página usando..
<a href="single.php?id=<?php echo $article['id'];?>">Link</a> //missing php close tag here
Depois obtê-lo usando
$id = $_GET['id'];
1
Author: Hek mat, 2017-02-21 13:56:31
ok lets try this.
on page 1 => article.php
# we assume
database query here
$query = mysqli_query(//query here);
// we then use a while loop
while($q = $query->fetch_array())
{
echo '<a href="single.php?id='.$q['id'].'">'.$q['article_name'].'</a>';
}
ok on page single.php
# we now have example.com/single.php?id=1 eg.
// there are many ways to grab the id
# option 1
// inside single.php
// method 1
$article_id = isset($_GET['id']) ? (int) $_GET['id'] : "";
// method 2
$article_id2 = "";
if(isset($_GET['id']))
{
$article_id2 = $_GET['id'];
}
// now you have the value from the GET method within your local variable scope
// so choose any of the method above
// both works
hope this helps?
1
Author: Moorexa, 2017-02-21 14:06:30
Como Hek mat disse que não viste as etiquetas de Fecho:
<a href="single.php?id=<?php echo $article['id'];?>">Link</a>
Mas o seu código também não está correcto. $_GET ['id' ] está a dar sempre um string "1"
não um int 1
e se o id não estiver definido, isso causaria um erro.
Então tenta isto.
if(isset($_GET['id']) && intval($_GET['id']) > 0){
$id = intval($_GET['id']); // now work with $id its an int now
//show the article, i.e. select * from .... where id = id ...
echo "WORKING";
}else{
//show the error like 404
echo "ERROR";
}
0
Author: Webdesigner, 2017-05-23 10:29:49