1

I am trying to display an image on my webpage using a PHP script to determine which image is displayed. The image link is as follows:

<a href="gallery.php?image=image01">......</a>

My PHP script is thus:

<?php 
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">'; 
?>

So what I am trying to achieve in terms of HTML is:

<img src="images/gallery/image01.jpg">

The result I am getting is '"; ?>' displayed on the page. Any help would be much appreciated!

5
  • 1
    It appears that your server is not actually executing the PHP, and just spitting out raw text. Commented Sep 26, 2014 at 17:54
  • I didn't DV, but based on the looks of it none of the current answers address the OP's issue. Commented Sep 26, 2014 at 18:02
  • nope @durbnpoisn i think its because he is not correctly closing the quotes. Commented Sep 26, 2014 at 18:09
  • Are these happening at the same time or when you click the link <a href="gallery.php?image=image01">......</a>, it will reload to execute $result = $_GET['image']; echo '<img src="images/gallery/'.$result.'.jpg">';? Commented Sep 26, 2014 at 18:10
  • Actually it looks like the quotes are closed just fine in the code example the OP has provided. The problem may lie somewhere else in the OP's code @Lal. Commented Sep 26, 2014 at 18:10

6 Answers 6

2

You have to change your code like this

<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result; ?>.jpg">
Sign up to request clarification or add additional context in comments.

Comments

2
<?php
    $result = filter_input ( INPUT_GET , 'image' );
    if (isset($result) && !empty($result)) {
        echo '<img src="images/gallery/'.$result.'.jpg">';
    }
?>

Comments

1

You used echo wrong, here is how you should use it.

<?php 
$result = $_GET['image'];
?>

<img src="images/gallery/<?php echo $result ?>.jpg">

Comments

0

I would change the gallery.php to this:

<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">

That would simply it a little bit. You should echo out the result to see what you are getting when the variable is passed to the gallery page.

1 Comment

You would need to close out your php here... <?php $result = $_GET['image']; ?> <img src="images/gallery/<?php echo $result; ?>.jpg"> as in @ExCluSiv3 answer.
0
echo"<img src='{$image}'>";

$image = uploads/myImage.jpg

I think this is the simplest code. To use a php variable while echoing out html, use curly {} brackets to insert any php variable. For instance, a file upload...

<?php

    if(isset($_POST['submit'])){

        $filename=$_FILES['file']['name'];
        $temp_dir=$_FILES['file']['tmp_name'];
        $image = "img/".$filename;
        }
?>

Comments

0
 <?php if($row2['pack1']==1){ echo "<img src=".BASE_URL."images/1seo.png";  } ?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.