I'm trying to show an image (or rather a link to an image) stored in a database and I'd like to get the image to show only if the link is set in the database.
Currently, if the link is not set (value is null), it shows a broken link.
Is there a way to for example use an if-statement and echo a HTML-code?
Something like this:
(The value have been fecthed to array $current in this example:)
<?php
if(isset($current['image']) {
echo "<img src='<?php echo $current['image'];
?>' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'})">
You used <?php twice, you have problem with quotes, brackets, etc.
<?php
if (!empty($current['image'])) {
echo "<img src='" . $current['image'] . "' class='left' style='max-height:20em; max-width:15em; margin-right:1em; margin-top:0;'>";
} else {
// here you can write for example default no-image image or whatever yo want, if you want
}
Nevermind, got it.
-Solution:
<?php if(isset($current['image'])): ?><img src="<?php echo $current['image']; ?>" class="left" style="max-height:20em; max-width:15em; margin-right:1em; margin-top:0;})">
<?php endif; ?>
<?php
if(isset($current['image'])) {
?>
<img src='<?php echo $current['image'];?>' class='left' style='max-height:20em; max-width:15em;
margin-right:1em; margin-top:0;'>
<?php
}
?>
Related
Is there anyway I can get in-line styling within a PHP echo statement. I want to set the height and the width of an image, as when I try to apply the height and `width externally it makes no difference as the image loads before the style sets in.
I have tried this but doesn't seem to be making any difference what so ever...
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)){
echo "<img src='profiles/no-image.png' 'width=500' 'height=600' >";
} else {
echo "<img src='".$row['imagePath']."' 'width=500' 'height=600' >";
};
?></p>
That doesn't work because you are not setting the quotes right.
This should do the trick:
echo "<img src='profiles/no-image.png' width='500' height='600' >";
You can apply styles the same way:
echo "<img src='profiles/no-image.png' style='width:500px;height:600px;'>";
Here is an alternative. Use PHP like a template engine. This approach does not use echo statements to output HTML. Instead, dynamic elements are introduced as needed.
<p><b>Profile Picture: </b>
<?php
$picture = $row['imagePath'];
if (empty($picture)) :
?>
<img src="profiles/no-image.png" width="500" height="600" >
<?php else : ?>
<img src="<?= $row['imagePath'] ?>" width="500" height="600" >
<?php endif ?>
</p>
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;?>">
I am trying to display an image that is in a folder "upload" by getting the image name from the database.
while($row = mysqli_fetch_array($result))
{
$pic = $row['image'];
echo $row['item'] ;
echo $row['location'];
echo $row['description'];
echo $row['forum'];
echo $row['datetime'];
echo $row['username'];
?>
</br>
<img src="upload/<?php echo $pic ?>"/>
<?php echo $row['image']; } ?>
"upload/<?php echo $pic ?>"
</body>
</html>
As you can see it display everything except the img src.
This is my database (ignore BLOB that was a test). I can't seem to figure out where I'm going wrong.
Thanks
My recommendation is to store the full relative path in the database like this:
uploads/folder/file.jpg
My preferred MySQL field type is 'varchar(255)' the your echo in te PHP code will look like:
echo '<img src="'. $row['image'].'" />';
You are trying to print the image source outside the while-loop. The while-loop will only exit when $row is empty, so $row['image'] is also empty.
1) view source (or use firebug) to see the image tag and see what is the src given there.
2) try the url: "http://{LOCAL}/projects/projectviewposted.php/upload/happyball(1).jpg" and see if you can open the image
Try this
<img src="upload/<?php echo $pic; ?>"/>
<?php echo $row['image']; } ?>
"upload/<?php echo $pic ?>"
Else note down the link of the folder path. It might be that the folder path is not correct.
Hope this helps
You can try :
while($row = mysqli_fetch_array($result))
{
$pic = $row['image'];
echo $row['item'] ;
echo $row['location'];
echo $row['description'];
echo $row['forum'];
echo $row['datetime'];
echo $row['username'];
echo '<br />
<img src="upload/'.$pic.'"/>
'.$row['image'];
}
Well after 3 days I figured it out... my header location was incorrect!
../project/projectviewposted.php/ A stupid extra slash at the end!!
Thanks for your help and suggestions!
I have the following line:
<td><img src="Photos/<? echo $rows['photo1']; ?>" height="200" /></td>
I don't always have a photo. I would like to hide the image space. It looks like it is broken or if the url is wrong.
<td><?php echo (!empty($rows['photo1']) ? '<img src="Photos/' . $rows['photo1'] . '" height="200" />' : '') ?></td>
To have no <img> tag appear for blank values, I'd use something like this:
<?php if (($rows['photo1'] !== "") || ($row['photo1']))
{
echo "<td><img src='Photos/" . $rows['photo1'] . "' height='200' /></td>";
}
?>
if (image is not blank, and it exists) {
then, echo the img tag and the variables
}
Alternatively, If you want to display a different image for blank values:
<td>
<img src="Photos/<?php if ($rows['photo1'] !== ""){echo $rows['photo1'];}
else {echo "defaultimg.jpg";}?>" height="200" />
</td>
Hope this helps :)
Perhaps this is more to your liking:
<img src="Photos/<? echo $rows['photo1']; ?>" height="200" onerror="this.style.display='none'" />
It sets the image to not show when there is a problem (such as a broken URL).
This has the advantage of avoiding ternaries, separates PHP from HTML, and keeps things clear without excessive documentation. Hope it helps.
I have the following code that sets a background if user has uploaded to database. If user has NOT uploaded an image then the result is a blank img src=''
I need to set this as an if isset function so I can plug in an alternate image if user has not uploaded anything.
Here is the current code:
<div id="background"><?php echo isset($background_image) && file_exists(ROOT.$background_image)?"<img src='$background_image' alt='' />":'';?></div>
Your code's a little dirty, opening php and closing it mid-html tag is only going to make it confusing for you in the future.
You're echoing back an isset which is just echo'ing back a boolean.
Try this;
$background_image = ""; // Not sure what you're using here - their username? Dump it in here anyway.
if (file_exists($background_image))
{
echo " <div id=\"background\">
<img src=\"{$background_image}\" alt=\"\" title=\"\" />
</div>";
}
Hope this helps.
Eoghan
I'm not sure what you mean, but a another aproach to what you are trying to do would be:
<div id="background">
<?php
$optionalImage = 'background.png';
$userImage = getUserImage();
if(empty($userImage)) {
$userImage = $optionalImage;
}
?>
<img src="<?php echo $userImage; ?>" />
</div>
Is it really neccessary to set the blank source of the image?
But a understandable and corrected code of what you are attempting is this
<div id="background">
<?php
echo "<img src='";
echo isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '';
echo "' alt='' />";
?>
</div>
The problem was that, either you were echo entire <img> tag, or just display ' '(Blank) with attached endings.
The short form:
echo "<img src='".(isset($background_image) && file_exists(ROOT.$background_image) ? $background_image : '')."' alt='' />";