PHP - 如何在其他变量值中插入变量链接(href)? [关闭]

i'm trying this away:

<?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 i click the link it just refresh the page, instead of redirects to the variable element (), which is added in DB mysql and exposed at home, but clicking on the link like that would direct to the page of details of this specific element that is already working, just not targeting properly.

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;
}

</div>

in your PHP:

$link = 'index.php?id='.$row['post_id'];
print '<span class="survey-name"><a href="'.$link.'">'.$row['title'].'</span>';

Note: I guess that ['post_id'] is $row['post_id'] because ['post_id'] is nothing.

@Oscargeek has provided a nice example, but since it looks like you are struggling with how you concatenate variables, I thought I could add an answer using sprintf and printf for formatting strings:

$link = sprintf('index.php?id=%d', $row['post_id']);
printf('<span class="survey-name"><a href="%s">%s</a></span>', $link, $row["title"]);

As you can tell it's a bit easier(and cleaner imo) to use the type specifiers(%s and %d) when building the string. sprintf returns a formatted string while printf outputs it.

Good luck.

It looks like there are a couple of things going on here. First of all, ['post_id'] isn't a proper variable call in PHP. It would need to be $POST['id'] if it's a POST variable, or $arrayVar['post_id'] where arrayVar is the name of the variable that post_id is part of.

After that, string concatenation is simple. Just mindful of where your string ends and the variable begins (a.k.a, watch your quotes):

$link = "index.php?id=" . $arrayVar['post_id'];

or even:

$link = "index.php?" . "id=" . $post_id;

Last but not least, don't forget to close your HTML tags:

echo "<span class='survey-name'><a href='".$link."'>". $row['title'] ."</a></span>";

An anchor tag will reload the page if clicked and the href is blank.