setting the img src via php? - php

Here is my problem.
If I do:
$imagePath = "images/spalt_images/body_images/525A.JPG";
?>
<img src="<?php $imagePath ?>" alt="front image" class="productImage"/>
<?php
Then my image does not show up.
However, if I do:
<img src="images/spalt_images/body_images/525A.JPG" alt="front image" class="productImage"/>
Then my image shows up just fine. Why would it not work with php?
Thanks

You need to echo it:
<?php echo $imagePath ?>
or use a short tag (not recommended, but that's another discussion):
<?=$imagePath ?>

You need to echo $imagepath otherwise it won't 'print' it out.

Change <?php $imagePath ?> in your code to <?=$imagePath?>, or add an echo if your server doesn't allow shorttags.

Related

HTML - PHP echo image

How do I properly echo images combining HTML and PHP? $offer['picture'] has link saved as example.com/picture.png. I've tried many different options, but nothing works. Can anyone help me out?
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
?>
<img src="<?php echo $image ?>">
<?php
}
If there is only example.com/picture.png in $offer['picture'], problem is that images linked incorrectly. You should add http:// before image link to make browser sure you are loading image by absolute path.
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
?>
<img src="http://<?php echo $image ?>">
<?php
}
Well it depends on your directory structure.
suppose your images are in example.com/assets/images/photo-1.jpg
your code should be
<?php foreach($json['offers'] as $offer) {
$image = $offer['picture']; ?>
<img src="http://www.example.com/assets/images/<?php echo $image; ?>">
<?php } ?>
In fact, you need to concatenate or hard code the path and image name will be appended dynamically.
Try this syntax,
<?php
foreach($json['offers'] as $offer) {
$image = $offer['picture'];
echo "<img src='$image' />";
}
?>

I'm trying to echo an image in a PHP variable in CodeIgniter

Hello I'm trying to echo an image on a view page in CodeIgniter but nothing is displayed on the page.
Here I'm making the variable:
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
And here I'm trying to echo the image:
<img src="<?php echo $image;?>">
Intro
This is the most basic php, so what's the addition of this question? Please read the basics of echo here: http://php.net/manual/en/function.echo.php (Example 1).
Learn the basics first!
Solution
1. Assign full html tag to variable and echo full html:
<?php
$image = '<img src="../../images/stoel.jpg" alt="Foo">';
echo $image;
?>
2. Or assign image path to variable and echo concat string:
<?php
$path = '../../images/stoel.jpg';
echo '<img src="' . $path . '" alt="Foo">';
?>
3. Or assign image path to variable and echo only this with php:
<?php
$path = '../../images/stoel.jpg';
?>
<img src="<?= $path; ?>" alt="Foo">
You are inserting full Image tag into src attribute.
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
and
<?php echo $image; ?>
or
<?php $image = "../../images/stoel.jpg"; ?>
and
<img src="<?php echo $image;?>">
<?php $image = "<img src='../../images/stoel.jpg' alt='img' />"; ?>
you try like this
<?php echo $image; ?>
You are already assign full image code in variable so you just need to print your variable:
<img src="<?php echo $image;?>">
To
<?php echo $image;?>
Make sure your images out side of the application folder then you can do something like
application
images
images > stoel.jpg
system
index.php
Use Base url from the url helper
<img src="<?php echo base_url('images/stoel.jpg');?>" />
Make sure you have set the base_url in config.php
$config['base_url'] = 'http://localhost/yourproject/';
You can also use HTML Helper img();
I think this is what you mean. Since you are echoing inside the src attribute, you do not need to store the whole <image>, just the path will do.
<?php
$image = "../../images/stoel.jpg";
?>
<img src="<?php echo $image;?>">

How to have image src a variable?

I want to keep src attribute in a variable. I am working on php and tried the following method but it doesn't display the image on html page.
Code:
<?php $path="C:/horizontal.jpg"; ?>
<image src="<?php echo $path; ?>" style="width:304px;height:228px" />
I think you can use this technique:
<?php $path="http://" . $_SERVER["SERVER_NAME"];?>
<img src="<?php echo $path.'/projectName/image.jpg'; ?>" style="width:304px;height:228px" />
Where:
$_SERVER["SERVER_NAME"]: to get the server name, for example: www.example.com.
Also you can use $_SERVER["SERVER_ADDR"], in this case you can get the address IP of your server, for example: "127.0.0.1".
I hope this information helps you.
Good Luck.
<?php $path="localhost/projectName/";?>
<img src="<?php echo $path.'image.jpg'; ?>" style="width:304px;height:228px" />

Displaying an image using a php variable

I'm trying to display images by taking their file path from an sql table, but i'm having a lot of troubles.
Here is whats going on:
$image is a variable containing the text "itemimg/hyuna.png" which is path to an image.
$image = 'itemimg/hyuna.png';
I assumed I would be able to display the image outside of the php block like so:
<img src= "<? $image ?>" alt="test"/>
This doesn't work though for some reason.
So I thought maybe it's not able to read the variable outside the php block(i'm a beginner), so for testing i did:
<h1> "<? $image ?>" </h1>
It displays itemimg/hyuna.png as a h1 banner.
Meaning it's accessing the varible fine.
So I thought maybe the path is wrong. So I tried:
<img src= "itemimg/hyuna.png" alt="test"/>
This displays the image perfectly.
So now I'm stuck scratching my head why the first bit of code displays nothing but the text "test" from "alt="
Extra question:
How do I go about assigning a value from an sql cell to a variable?
I attempted the following with no luck:
$q = "select * from item where id=$id";
$results = mysql_query($q);
$row = mysql_fetch_array($results, MYSQL_ASSOC);
$image = ".$row['image'].";
item is a table with a collumn: image which contains file paths to images
First of all, you should not use PHP Shorttags.
When you use the PHP Shorttags you have to say:
<img src="<?=$image ?>" alt="test" />
But i would encourage to escape the Content off the variable like this:
<img src="<?php echo htmlspecialchars($image); ?>" alt="test" />
Your extra question:
This should lead to an syntax error because the string could not be parsed, just use $image = $row['image'];
Try this
<img src= "<?php echo $image ?>" alt="test"/>
try
<img src= "<?= $image ?>" alt="test"/>
or
<img src= "<? echo $image; ?>" alt="test"/>
try this
<img src= "<?php echo $image ?>" alt="test"/>

PHP echo function inside a HTML link

I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
<img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>
Turn
<?php $image=troll/GrannyTroll.jpg?>
into
<?php echo "troll/GrannyTroll.jpg"; ?>
?
Or provide more details on what you are trying to achieve.
Also, you might consider urlencode-ing some of those URL parameters.
Edit:
So you might try setting the variable beforehand:
<?php $image = "troll/GrannyTroll.jpg"; ?>
<img src="<?php echo $picture; ?>" width="100" height="94" />
So now i understand what you are trying to do.
One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:
$image = 'troll/GrannyTroll.jpg';
The second error is that you do it in the wrong order, you have to define $image first, before you use it.
That's what I believe you want to do:
<?php
$image = "troll/GrannyTroll.jpg";
?>
<img src="<?php echo $image; ?>" width="100" height="94"/>

Categories