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

Asked

Viewed 6,725 times

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 clicking on the link it only gives a refresh on the page, and does not direct to the page of the variable element in question (), which is added by DB mysql and exposed at home, but when clicking on the link I would like you to direct to the details page of this specific 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;
}

1 answer

2


It seems that something is missing in the definition of the link, first set the variable correctly, I believe it is $row and not just [post_id] and finally prints 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 braces {} in an SQL string for?

  • Okay, you directed me to another page, but error 404... I will try to explain better: It is a portal where register information of places and generates an id (post_id), and I want that when click on the title of each place (<span>), direct to a page with more details of this place, this page is ready and returning the information correctly, but the direction is not.

  • Then the link was generated wrong, can show where the [post_id]? the file to redirect is the index.php? @Paulolima

  • How can I show you more clearly my SQL? posting here has gotten extremely bad to view.. Thanks @rray

  • Puts the php code that picks up the post_id, use the edit link in your question.

  • see if this way the question became clearer (sorry ignorance in some questions, I am begginer in PHP).

  • @Paulolima, no problem the query you make of php could add it in the question?

  • I believe that’s it, right?

  • I changed the answer see if this is it @Paulolima.

Show 3 more comments

Browser other questions tagged

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