Anthony
Anthony

Reputation: 481

Link not sending to the right page

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

Upvotes: 1

Views: 59

Answers (3)

Lix
Lix

Reputation: 47976

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.

Upvotes: 2

George Marques
George Marques

Reputation: 875

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

Upvotes: 1

djot
djot

Reputation: 2947

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.

Upvotes: 0

Related Questions