I am dynamically grabbing image links from a database and inserting them into image tags. This php code checks if there is a link or not. If there is a link, that link is inserted into the img code. If there is no link, a generic "image coming soon" picture is used in its place.
echo '<tr>
<td width="80" height="46">
<a href="displayitem.php?item_id='.$top_product_id.'">';
if (!is_null($top_link) || $top_link == "")
{
echo'<img src = "'.$top_link.'" alt="" width="64" height="63" />';
//echo $top_link;
}
else
{
echo'<img src = "imagen/imagecomingsoon.png" alt="" width="30%" height="40%" />';}
echo'</td><td width="108">'.$final_name.'';
echo'</td>
</tr>';
I have a strange bug that in the event there is no image to link, instead of using the generic image, I get a blank. When I inspect the blank box, I see that the img tag is not generating properly. There are no quotes, or even an equals sign after the src designation. It looks like this.
<img src alt="" width="64" height="63">
Whey would the entire equals sign dissapear along with the quotes and link in the event I want to use a generic image?
You should replace !is_null($top_link) || $top_link == "" with !is_null($top_link) && $top_link != "". But i would replace the whole condition:
if (!empty($top_link)) {
echo '<img src = ......';
}
This would let you not care about different types of emptiness in php.
After inspecting your code I guess your IF Condition is going wrong, try this
if (!is_null($top_link) || $top_link != " ")
I have a suggestion but i am not sure that's the problem
Try this : echo'<img src = '".$top_link."' alt="" width="64" height="63" />';
i put the ' before the "
And replace the || with &&
Related
echo '<a href="LP_Teacher_Response_G4.php Lesson_id=$Lesson_id&user_id=$user_id&Lesson_Class_id=$Lesson_Class_id&class_id=$class_id">
<img height="62" src="../Images/Lock.png" width="76" /></a>';
Hi,
I am not sure why my parameters do not show in the link, instead it outputs the code statement, played around with it but not sure what needs to be done.
Try this way
echo "<img height=\"62\" src=\"../Images/Lock.png\" width=\"76\" />";
In my code I show some images in this way:
...
<?php echo '<img src="' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="' .$imagetwo. '" alt="" /> '?></a></div>
...
where $imageone, $imagetwo have path from Mysql database
e.g.
$imagesone = "images/exposition/02-05-2017-11-28-00-foto 2_b.JPG";
This code is working in the same place where I have my images folder, but now I need to put the same code in a subfolder page and I'd like to use the same image.
So, I need to put dinamically "../" before my varible, something like this:
...
<?php echo '<img src="../' .$imageone. '" alt="" /> '?></a></div>
<?php echo '<img src="../' .$imagetwo. '" alt="" /> '?></a></div>
...
but it's not working.
Any suggestion?
EDIT This is my solution:
if ($images1 != ""){
$imageone = "../".$images1;
}
if ($images2 != ""){
$imagetwo = "../".$images2;
}
in this way I fixed my code and it's working!
Since you're using double quotation marks after src, you can just write down <img src="../$imageone" alt="" />
There's no need to use simple quotation marks to write the variable because the double quotation marks gets the variable number.
I have a string, an img url, in a php block called $str. And I want to set that string as the img src in an img src but its not working.
<img src="<?php $str ?>" height="50px" width="50px">
how else can i set the src to the $str string?
<img src="<?php echo $str;?>" height="50px" width="50px">
Well, I found correct answer for this problem. Nothing works well from above answers, that codes only print out source string to html page on site.
This works for me (I have my function that return source string of picture):
require_once("../../my_coded_php_functions.php");
<?php echo '<img src="' . getSourcePathOfImage() . '" />' ?>
This site(article) helped me to find and understand solution:
http://php.net/manual/en/faq.html.php
<?php echo '<img src="'.$str.'" height="50px" width="50px">' ?>
Other ways, although is not recommended. You may try :
<img src="<?=$str?>" height="50px" width="50px">
this will work, only if (in php.ini)
short_open_tag = On
I am trying to set size of an image in PHP but it's not working..
echo "<img src=".$media."width=200 height=200";
$media is the src link. Without the width and height attributes, it works perfectly well. I think that 200 must be enclosed in double quotations but I am unable to do that. Any ideas on how to solve this problem?
width is currently concatenated with your file name. Use:
echo '<img src="'.$media.'" width="200" height="200" />';
The /> closing tag is necessary to correctly render your image element.
The quotes around tag names are recommended. Omitting these quotes will only cause issues when:
The attribute value contain spaces, or
You forgot to add a space after the attribute value
Yet another alternative, just for kicks:
echo <<<EOL
<img src="{$media}" width="200" height="200" />
EOL;
aka a HEREDOC.
That is because that is not proper HTML.
In your code you'd get something like:
<img src=image.jpgwidth=200 height=200
And what you need is:
<img src="image.jpg" width="200" height="200" />
So do:
echo '<img src="' . $media . '" width="200" height="200" />';
echo '<img src="' . $media . '" width="200" height="200" />';
You should be able to make it work with the following code
echo "<img src=\"".$media."\" width=200 height=200>";
how to insert this javascript code :
thejavascript
inside img src in php
<img src="http:'.$iv[$j].'.jpg" height="220" width="200" alt="Image '.ucwords($kw).'" "thejavascript">
Thanks
You can use this jQuery script to add and onClick event in your balise img but you must adding an attribute id into your image. An id must be unique in your html page.
$("img#idImage").click(function({
window.open('welcome.html','welcome');
}));
$kw = $ucwords($kw);
"<img id='idImage' src='http:{$iv[$j]}.jpg' height='220' width='200' alt='Image {$kw}' />";
But the best will be to separate attributes height and width into a CSS stylesheet.
Insert the following HTML outside PHP brackets and it should work according to the way you posted it. I'm making a few assumptions, one being that the link you posted wraps the image code and that the PHP variables turn the image into valid code.
<a href="javascript:void(0);" onclick="window.open('welcome.html','welcome')">
<img src="http:<?=$iv[$j]; ?>.jpg" height="220" width="200" alt="Image <?=ucwords($kw); ?>">
</a>
More simple, replace "thejavascript" (with the "") by :
onclick="window.open(\'welcome.html\',\'welcome\')"
A bit different approach, onclick event is on img tag.
$javascript="window.open('welcome.html','welcome')";
$img='<img src="http:'.$iv[$j].'.jpg" height="220" width="200" onclick="'.$javascript.'">';
echo $img;