Hide non-existing image from page using echo function - php

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.

Related

Trying to use in line styling in the middle of a PHP echo

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>

Show image, if it's set in database

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
}
?>

PHP: Echo IMG src= php echo

I have the following code working in another document
<img src="<?php echo $option_upload_url . '/' . $shortname . '_banner1_imagen.'.$get_banner1_imagen_ext; ?>" alt="<?php bloginfo('name'); ?>" Style="width:100%" />
but now i took to next expression:
echo "<img src=\"$option_upload_url/$shortname_banner1_imagen$get_banner1_imagen_ext\" >";
but seems like the parth isnt correctly implemented, tried diferent things i found on the web but couldnt manage to find a solution...
EDIT:
The image isnt loading when i use the second code.
Try it like this, php isn't understanding which are your variable names, and you forgot the . between _imagen and $get_banner1_imagen_ext
echo "<a href=\"".stripcslashes($get_ads_code_one)."\" title=\"".bloginfo('name')."\">
<img src=\"$option_upload_url/{$shortname}_banner1_imagen.{$get_banner1_imagen_ext}\" /></a>";
I would prefer going with the single ' at the place of escaping ", keep " in the html like this:
echo '<a href="'.stripcslashes($get_ads_code_one).'" title="'.bloginfo('name').'">
<img src="'.$option_upload_url.'/'.$shortname.'_banner1_imagen.'.$get_banner1_imagen_ext.'" /></a>';
You need to have your PHP variables be outside the quotes for them to not be taken as literals.
echo "<img src='" . $option_upload_url . "/" . $shortname_banner1_imagen . $get_banner1_imagen_ext . "' >";

How to set this if isset function

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='' />";

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