I'm new to PHP and coding in general so apologies for this likely silly question. I know the answer will be extremly simple but try and I might, I just can't see it.
I'm trying to pull image path data from my database and concatenate with the code below so that I can enventually display it on my site using <?php print.... ?>
I have successfully done this. The problem I have now is setting the size of this image.
Please see my code below.
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> ";
I will be indebted to anybody who can help on this.
I have researched this question and came across some answers but just could not make them work with my problem.
I's just the image size I have an issue with, nothing else.
I ended up using the code below and then using CSS. This was inspired by #Ndianz's answer.
$ImagePath = " <img src = db_images_product/".$row['ImagePath']." ".'Class="ProductImage"'."/> ";
I have ran all this code through an editor and it runs fine with no errors.
To illustrate concatenation here is a link with many scenario and explenations to help you,
Click Here
This is the way you currently have it,
$Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> "
This is the way you would write that,
$Image_Path = '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';
I suggest you do this,
$Image_Path = '<img src="db_images_product/'.$row['ImagePath'].'">';
Then in your css file add the style to your img tag like this,
img {
height: 100px; //or whatever you want it to be.
}
You can use getimagesize() to grab the width and height then set the property on the image tag. the $row['ImagePath'] needs to be reachable by the script so you might have to play with this and add some ../ to get to the relative path of the image.
list($width, $height) = getimagesize($row['ImagePath']);
$Image_Path .= "<img src='db_images_product/{$row['ImagePath']}' height='{$height}' >";
You've just got some syntax errors. When you concatenate be careful about where your quote marks go. I'd use single quotes to open and close your PHP string so as not to conflict with the double quotes in your HTML. So pay close attention -- the single quote closes the PHP string right before the dot concatenates the $row variable. Then after the variable you add another dot to concatenate the end of the PHP string.
$Image_Path .= '<img src="db_images_product/"'.$row['ImagePath'].'" height="100"/>';
Related
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
So I'm trying to define $picurl1 so that it uses the value in $pic1. So in the end I want it to be:
<img src="./pictures/{definition of pic1}.png">
Right now I use this php code:
$pic1 = '<script src="pic.js"></script>';
$picurl1 = '<img src="./pictures/' + $pic1 + '.png'">';
Sorry if I'm not being very clear. I don't really know how to explain it. I hope you understand.
In other words, please tell me what I should change $picurl1 to.
By the way the script comes up with a random picture name without the '.png'.
Thanks in advance.
For starters, you're using the wrong operator to concatenate strings in PHP. I think you mean this:
$picurl1 = '<img src="./pictures/' . $pic1 . '.png'">';
More to the point, what is "definition of pic1"? Do you mean that the code in pic.js will randomly choose a file name, and you want its result to be the URL used in the img tag?
The problem you're encountering, then, is that PHP runs on the server while JavaScript runs on the client. So your PHP code can't use the result of pic.js because it won't have a result until the browser runs it, after the PHP code is done.
So you need to get that result client-side in JavaScript code.
How does pic.js create that result? That is, is there a function in pic.js? For now I'm going to assume there is, and I'm going to assume that function is called something like getFileName. (Just for the purpose of this example.)
After you included the JavaScript code, and after the img tag is in the document, you can call that function and set the src of the img tag to its results. To help us identify the img tag, let's give it an id:
<img src="default.gif" id="theImage" alt="This is a dynamic image" />
(I gave it a default value for the src since an empty value is invalid. I also have it an alt value for completeness.) To change its src value to the result of a function, you'd do something like the following:
document.getElementById('theImage').src = getFileName();
Remember, this is all client-side code. The only way you can use the "result" in PHP code is if the calculation is done in PHP, not in JavaScript.
You must consider that all the server side codes are executed before the client side codes (javascript, html, css , ...). so your code does not make any sense , you can not embed an undefined code inside another code that is executing sooner.
if your js code must return some thing, so remove php codes and simply use HTML instead
I tested this successfully:
$picName = "greenButterfly7"; //note no spaces inbetween green and butterfly
$picurl1 = "<img src='./pictures/" . $picName . ".png'>";
echo $picurl1;
or in pure HTML form:
<img src='pictures/greenButterfly7.png'>
or in embedded form (PHP inside HTML):
<img src='pictures/<?php echo $picName; ?>.png'>
I'm assuming the easiest way to do this will be with regex, but I just can't seem to find clear information on regex. I'm a beginner and all the information I'm finding is confusing.
I need to find every image in an HTML file, insert the folder extension, and then overwrite the file. I know how to do everything but the replacement. From my understanding, the code should look something like this:
preg_replace("^\"(.jpg|.jpeg|.gif|.png)$"....)
But I don't understand where to go from there. I need to keep the original value of whatever is between those things and add something to the beginning of it, so for example "image.jpg" would become "images/image.jpg".
$img = "<a href=\"hello.jpg\" /><a href=\"asdf.png\" /><a href=\"xkcd.gif\" />";
$img = preg_replace("/\"(\w+\.(jpg|jpeg|gif|png))\"/","\"images/$1\"",$img);
echo $img;
Output: <a href="images/hello.jpg" /><a href="images/asdf.png" /><a href="images/xkcd.gif" />
The regex can be improved using lookarounds, but I think they are overkill (and will make it more complex).
I have a folder where uplaoded files are saved. This page is used to retrieve those saved files. A picture called "flower.jpeg" is in the folder, and I am trying to display the picture using an image tag, as shown below. I have two options, but none of them is working.
<!DOCTYPE html>
<html>
<body>
<?php
$picture=flower.jpeg
$playfile='/upload/$picture';
?>
// <img href= '<?php $playfile ?>' width="800" height="600">
// <img src= '<?php echo $playfile; ?>' width="800" height="600"> <br>
<script>
document.write('Go Back To Chat');
</script>
</body>
</html>
flower.jpeg requires quotes and the $playfile assignment is not properly specified as, to my knowledge, strings within single quotes are not parsed for variables within PHP.
$picture='flower.jpeg';
$playfile='/upload/'.$picture;
Additionally correct use of the tag is as follows
<img src="<?php echo $playfile; ?>" width="800" height="600">
As the attributes need enclosing by double-quotes, you are just using PHP to echo out a variable containing a string.
An explanation of what is wrong. You're using the following code to build your link:
$picture=flower.jpeg
$playfile='/upload/$picture';
First off, you need to quote the picture
$picture = 'flower.jpg';
//or
$picture = "flower.jpg";
The next problem is how you're building the playfile. You need to exit single quotes to use a variable or use double quotes:
$playfile = '/upload/'.$picture;
//or
$playfile = "/upload/$picture";
//or even
$playfile = "/upload/".$picture;
The single quotes will take the text as is where the double quotes will evaluate variables in them. I suggest continuing to read up on the basics of PHP and strings.
Here are a few steps that may help you in finding if the path to the image (or the 'href' as you call it) is correct. No direct solution is possible as we do not have access to your file system, as Simon pointed out.
Check if the file extension of your picture is mentioned correctly. Check also that it is in the correct CAPS. For example, if you saved your picture using MS Paint, the extension will be *.PNG but your script may contain *.png; this may cause problems in some cases.
.jpg and .jpeg are two different extensions sometimes (in the sense of the above point). You may want to take that into consideration.
You may want to verify that the directory structure of the image is correct (i.e. verify that you are specifying the correct path to the file).
Hope this helped you in some way. The above steps are rough and vague. If they failed to help you, please comment, and someone will be able to help you.
I currently have this:
$imgpath = $domainurl.'images/games/'.$name.'_icon.jpg';
$img = (file_exists($imgpath) ? '<img src="1">' : '<img src="2">');
I want to show img 1 (original) if the file exists, and show img 2 (default) if the file does not exist.
Right now, the code will only show the 2nd image, even if the 1st one does exist.
You can see this at: http://crystalarcade.com/arcade/category/all
That looks for a file named, literally, $imgpath. Remove the single quotes. Furthermore, $domainurl sounds like it's a URL. file_exists may work on URLs depending on your PHP settings, but if possible, you should give it a path on the local filesystem.
You need to remove the qoutes '' from around $imgpath, and the () are not necessary-
$img = file_exists($imgpath) ? '<img src="1">' : '<img src="2">';
Usage of PHP shorthand is discouraged and should be avoided.
Please use proper IF statements.
See PSR 0 to PSR 2 php development standards
Side note:
at first you might think the shorthand writing is more compact and logic, but unlike with a full IF statement most people can't figure out which case will happen.
What Im trying to do is write a script that grabs the url of thumbnails attached to posts in wordpress. It sounds really easy(as I'm sure the solution is) but I can't seem to get it to work, I keep getting syntax errors no matter what I try. The problem line is the second echo(Img src...). Any help would be greatly appreciated.
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'archive-thumb');
$image_url = $image_url[0];
echo "<li class=\"recent-img-widget-li\"><a href='".get_permalink()."'>;
echo "<img src=\"".$image_url."\" width=\"120\" height=\"120\">";
echo "</a></li>";
Simply enough, you're not closing your first string after get_permalink(). Yo need another quote after the >.
You never close the first string. You just need a quote before the greater than on the first line (and possibly the second?). Look at the syntax highlighting that SO has.
A general guideline is to always look at the row above the one that is giving the error.
In this case you have forgotten to end the string in the last part of the first echo statement.
...ermalink()."'>;
Should be
...ermalink()."'>";
For one you should close that first echo. Missing the closing "