Hey guys I have this code and I am trying to display an image if the src="" is empty.
I am using !empty to try to display my placeholder image as you can see here:
<img src="'.
(!empty( $cardata["PictureRefs"]))
? (explode(',', $cardata["PictureRefs"])[0])
: "db-content/theme/media/img/no-image.jpg" . '" data-src="'.
(explode(',', $cardata["PictureRefs"])[0]) .'" alt="'.$cardata["Variant"].'"/>
<img class="ms-thumb" src="'.
(!empty( $cardata["PictureRefs"]))
? (explode(',', $cardata["PictureRefs"])[0])
: "db-content/theme/media/img/no-image.jpg" .'" alt="'.$cardata["Variant"].'" />
For some reason this isn't working on my page, it just breaks the page.
Any idea where I might be going wrong? I am just left with the external url that the PDO is fetching from the DB displaying in text on the page.
CODE:
<?php $src = "db-content/theme/media/img/no-image.jpg";
if(!empty( $cardata["PictureRefs"])){
$src = explode(',', $cardata["PictureRefs"])[0];
}
$dataSrc = explode(',', $cardata["PictureRefs"])[0]; ?>
<?php echo '
<div class="ms-slide">
<img src="'.$src.'" data-src="'.$dataSrc.'" alt="'.$cardata["Variant"].'"/>
<img class="ms-thumb" src="'.(explode(',', $cardata["PictureRefs"])[0]).'" alt="'.$cardata["Variant"].'" />
</div>' ?>
It just displays the 404 for the image it was trying to receive in the first place.
Although I don't know what is going wrong, I refactored the code for the first image in order to make it easier to read and help others to understand:
$src = "db-content/theme/media/img/no-image.jpg";
if(!empty( $cardata["PictureRefs"])){
$src = explode(',', $cardata["PictureRefs"])[0];
}
$dataSrc = explode(',', $cardata["PictureRefs"])[0];
echo '<img src="'.$src.'" data-src="'.$dataSrc.'" alt="'.$cardata["Variant"].'"/>';
Are you using PHP 5.5 or later? Array dereferencing in this way (from the result of the explode function) requires PHP 5.5 or later.
It may be useful to view the source of the resulting page and post that.
Related
I have Tried Many Solution , Inside and Outside PHP Code , None of them are working
I am able to fetch Image Path from Array But Not able to assign it ..
I Tried following solutions :
foreach ($data as $item) {
echo "Image Path is ".$item[0]["Image"] ; //--> Returning - img01.jpg
$imageNumber= "img01";
//sol 1.
echo '<img src="'.$item[0]["Image"]'">';
//sol 2.
// echo "<img src=\"{$imageNumber}.jpg\">";
//sol 3.
//echo '<img src="'.$coverlink.'" alt="Cover">';
}
//sol 4.
// <img src="<?php echo $imageNumber ?>.jpg">
//sol 5.
// <img src="<?php echo $item[0]["Image"]; ?>">
I need some guidence what I am missing ?Any Help would be appreciated
From what you've posted, it seems like $item[0]["Image"] returns the image name along with the extension. E.g: img01.jpg.
So, you can simply put this within the img src.
foreach ($data as $item) {
echo "<img src='".$item[0]["Image"]."' />";
}
If your question is explained clearly, then this should work just fine.
How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)
How do I set the src= of a <img> element, according to the end of my link?
Example 1:
Link:
http://www.endertec.com.br/hf/see-img.php?img=http://www.endertec.com.br/hf/do.php?imgf=325356456.png
<img> element in see-img.php file:
<img src="http://www.endertec.com.br/hf/do.php?imgf=325356456.png"/>
Example 2:
Link:
http://www.endertec.com.br/hf/see-img.php?img=http://www.endertec.com.br/hf/do.php?imgf=342424234.png
<img> element in see-img.php file:
<img src="http://www.endertec.com.br/hf/do.php?imgf=342424234.png"/>
I.E.: I want to know if there is any script that I can use in see-img.php do what I was demonstrating above file.
Edited
Try this:
<?php
//$str = 'http://www.endertec.com.br/hf/see-img.php?img=http://www.endertec.com.br/hf/do.php?imgf=342424234.png';
$str = 'http://www.endertec.com.br'.$_SERVER['REQUEST_URI'];
$src = str_replace('see-img.php?img=' , '' ,stristr($str , 'see-img.php?img='));
echo '<img src="'.$src.'"/>';
?>
In PHP you could do this:
$img_url = isset($_POST['img']) ? $_POST['img'] : null;
$img_file = isset($_POST['imgf']) ? $_POST['imgf'] : null;
Then with that $img_url you can do this:
if (!empty($img_url) && !empty($img_file)) {
echo '<img src="' . $img_url . '?imgf=' . $img_file . '"/>';
}
The idea is to get the URL values which are known as post values, assign them to a variable & then do a check where you echo the values into <img src= tags.
But that is the best that I can do based on the lack of detail in your question. But the concept is solid and should work in PHP.
You could try using a built in php function know as sttrpos, it will return an int with the position where the substring you need begins. Then a normal php substring will do the trick.
I have a Magento store and I would like to replace the src value of an img based on whether the referring URL contains a specific querystring or querystring value (adnetwork=as) and if not simply leave the image as it is.
Is this simple enough to do, I've searched everywhere for the answer, and even asked on various forums to no avail.
Here's what I have so far, seems like the correct syntax, just Firebug reports that it is unable to load the image URL, and it simply shows "". The paths are correct and have been tested.
<?php
// Path for default image source
$logo = $this->getSkinUrl('images/logo.gif');
// Get the referrer url from the $_SERVER array, or false if it's empty
$referrer = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];
// If the referrer exists
if($referrer) {
// parse the url for the query
$query_str = parse_url($referrer, PHP_URL_QUERY);
// If there's a query string
if(!empty($query_str)) {
// Parse it and put the array into $components
parse_str($query_str, $components);
// If the "adnetwork" component is set
if(!empty($components['adnetwork'])) {
// Set the $logo var to the adnetwork image source
$logo = $this->getSkinUrl('images/logo-no-number.gif');
}
}
}
?>
<div class="header">
<img class="shop24" src="<?php echo $this->getSkinUrl('images/shop-24.gif') ?>" alt="Shop Online 24 Hours a Day"/>
<div class="shopping-bag"><?php echo $this->getChildHtml('topcart')?></div>
<div id="fplogo"><img src="<?php echo $logo; ?>" alt="Sale Basins - Clickbasin.co.uk" /></div>
<img src="<?php echo $this->getSkinUrl('images/side_logo_promo.gif') ?>" alt="Promotion" class="side-logo-promo"/>
<?php echo $this->getChildHtml('topMenu') ?>
<?php //<?php echo $this->getLogoSrc() ?>
</div>
I hope you can help guys. Thanks.
Shouldn't you be simply echoing $logo? you are assigning the image to $logo but you never set anything to $logo.
I am printing an image using an ID which is generated. however i wanted to do a check to see if this image exists and if it doesnt print no-image.jpg instead...
<img src="phpThumb/phpThumb.php?src=../public/images/'.$row["id"].'/th.jpg&w=162" alt="" width="162" />
It would be great if this could be kept on one line is possible. Any help would be appreciated.
What Kristopher Ives says, and file_exists:
echo (file_exists("/path/file/name/here") ? "web/path/goes/here" : "no_image.jpg")
btw, your snippet is unlikely to work, as you seem to be combining plain HTML output and PHP without putting the PHP into <? ?>
My recommendation would actually be to abstract the decision making from the html tag itself, in a separate block of php logic that is not outputting html...here is an abbreviated example that assumes you are not using a template engine, or MVC framework.
<?php
$filename = 'th.jpg';
$filePath = '/public/images/' . $row['id'] '/';
$webPath = '/images/' . $row['id'] . '/';
//Logic to get the row id etc
if (!file_exists($filePath . $filename)) {
$filename ='no-image.jpg';
$webPath = '/images/';
}
?>
<img src="<?php echo $webpath . $filename;?>" />
Wow, this question gets asked and answered a lot:
http://en.wikipedia.org/wiki/Ternary_operation
You could do it by:
<img src="<?php ($row['id'] != 0) ? "../public/{$row['id']}.jpeg" : 'no_image.jpg'; ?> >