PHP - How to insert a variable link inside a href variable?

1

I'm trying this way:

<?php
  $link = "'index.php?id=echo ['post_id']; "
?>

<?php
while ($row = mysql_fetch_array($result)) {
 echo "<span class='survey-name'><a href='$link'>". $row['title'] ."</span>";
?>

But when you click on the link it just gives a refresh on the page, and does not point to the page of the variable element in question (), which is added by the mysql DB and exposed in the home, but clicking on the link would like it to be directed to the details page of this particular element that is already working, just not directing correctly.

SQL:

CREATE TABLE IF NOT EXISTS 'categories' (
  'id' int(3) NOT NULL,
  'name' text COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS 'posts' (
  'id' int(6) NOT NULL,
  'cat_id' int(3) NOT NULL,
  'title' varchar(255) COLLATE utf8_bin NOT NULL,
  'endereco' text COLLATE utf8_bin NOT NULL,
  'cidade' text COLLATE utf8_bin NOT NULL,
  'email' text COLLATE utf8_bin NOT NULL,
  'telefone' text COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

function get_posts($id = null, $cat_id = null){
    $posts = array();
    $query = "SELECT 'posts'.'id' AS 'post_id' , 'categories'.'id' AS 'category_id', 'title','endereco','cidade','email','telefone','categories'.'name' FROM 'posts' INNER JOIN 'categories' ON 'categories'.'id' = 'posts'.'cat_id' " ;
    if(isset($id)){
        $id = (int)$id;
        $query .= " WHERE 'posts'.'id' = {$id} ";
             }
    if(isset($cat_id)){
        $cat_id = (int)$cat_id;
        $query .= " WHERE 'cat_id' = {$cat_id} ";
             }         
        
    $query .= "ORDER BY 'post_id' DESC";
    
    $query = mysql_query($query);
    
    while($row = mysql_fetch_assoc($query)){
    $posts[] = $row;
   }
   return $posts;
}
    
asked by anonymous 15.07.2015 / 01:04

1 answer

2

It seems that something is missing in the definition of the link, first define the variable correctly, I believe it is $row and not only [post_id] and finally print with echo the value contained in $link .

<?php
   $posts = get_posts(); 

   $pagina = 'index.php?id=';
   $formato = "<span class='survey-name'><a href='%s%d'>%s</span>";

   foreach($post as $link){
      printf($formato, $pagina, $link['post_id'], $link['title']);
   }

Related

What are the braces for a SQL string?

    
15.07.2015 / 01:12