1

I have this code:

if ($topic['user']==$_SESSION['display'])
{
    echo '<div class="bottomright"><a href="?id=<?php echo $topic_id; ?>&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id=<?php echo $topic_id; ?>&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id=<?php echo $topic_id; ?>&part=7"><img src="../assets/icons/Trash.png" /></a></div>';
}

When I hove over the image the link shows as:

?id=%3C?php%20echo%20$topic_id;%20?%3E&part=3

Rather than:

?id=3&part=3

Why is this not working?

I tried this:

if ($topic['user']==$_SESSION['display'])
{
    echo '<div class="bottomright"><a href="?id=$topic_id&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id=$topic_id&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id=$topic_id&part=7"><img src="../assets/icons/Trash.png" /></a></div>';
}

Now I get:

?id=$topic_id&part=3
Prix
  • 19,417
  • 15
  • 73
  • 132
Anthony
  • 471
  • 1
  • 5
  • 12
  • You should read: [**What is the difference between single-quoted and double-quoted strings in PHP?**](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Prix Sep 15 '13 at 17:59
  • [Also you do not need to edit your question with what worked for you, just mark the answer as the right answer, see here how.](http://stackoverflow.com/about) – Prix Sep 15 '13 at 18:00

3 Answers3

2

Your code is already within PHP tags, you don't have to add an additional <?php to the link's parameters.

echo '<div class="bottomright"><a href="'. $topic_id .'&part=5"...

Simply terminate your string and concatenate the relevant variables into the string.

Here is a very simplified example:

<?php

$user_name = "Anthony";

echo "Hello ". $user_name ."! How are you?";
//----------^ terminating the string

?>

This will result in:

Hello Anthony! How are you?

Here is the relevant documentation dealing with string concatenation.

Lix
  • 47,311
  • 12
  • 103
  • 131
1

You are echoing a php statement, it won't be interpreted, but displayed as it is. Instead, you should concatenate the string, like this:

if ($topic['user']==$_SESSION['display'])
{
    echo '<div class="bottomright"><a href="?id='
        . $topic_id
        . '&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id='
        . $topic_id
        . '&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id='
        . $topic_id 
        . '&part=7"><img src="../assets/icons/Trash.png" /></a></div>'; 
}
George Marques
  • 820
  • 7
  • 21
0

You don't need to "echo" within a PHP statement. Just concatenate the string (here id) as shown here:

if ($topic['user']==$_SESSION['display']){ echo '<div class="bottomright"><a href="?id='.$topic_id.'&part=5"><img src="../assets/icons/Comments-edit.png" /></a><a href="?id='.$topic_id.'&part=6"><img src="../assets/icons/Lock.png" /></a><a href="?id='.$topic_id.'&part=7"><img src="../assets/icons/Trash.png" /></a></div>'; }

Also, you should use "& amp;" (without that space, don't know how to format that here) instead of "&" in your links.

djot
  • 2,952
  • 4
  • 19
  • 28