1

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.

4
  • sorry, my code is $Image_Path .= " <img src = db_images_product/".'$row['ImagePath']'." ".'height="100"'."/> "; Jul 30, 2014 at 22:20
  • 2
    $Image_Path .= '<img src="db_images_product/'. $row['ImagePath'] . '" height="100"/>';
    – Ronnie
    Jul 30, 2014 at 22:22
  • Have you tried getimagesize()? Jul 30, 2014 at 22:24
  • I can't believe that that '$row['ImagePath']' is printing out a real value, and not that exact text. Jul 30, 2014 at 22:26

4 Answers 4

0

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"'."/> ";
1
  • The reason none of the other answers worked is because everyone one of them has syntax errors.
    – wuno
    Jul 31, 2014 at 23:53
-1

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.
}
3
  • Thanks a lot; your code helped me come up with a solution. I ended up using the code below and using CSS. It works like a charm! $ImagePath = " <img src = db_images_product/".$row['ImagePath']." ".'Class="ProductImage"'."/> "; Jul 31, 2014 at 23:02
  • @RolexOdhiambo Awesome! I am glad to hear it. I dont know why someone down voted me but please upvote me and accept my answer if it helped you.
    – wuno
    Jul 31, 2014 at 23:52
  • Thanks for accepting. It really upsets me someone down voted my answer when they all still have syntax errors. GRR!
    – wuno
    Aug 1, 2014 at 16:02
-1

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}' >";
-1

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"/>'; 
3
  • Did you just.. copy Ronnie's comment?
    – James
    Jul 30, 2014 at 22:32
  • Hiya, this may well solve the problem... but it'd be good if you could provide a little explanation about how and why it works :) Don't forget - there are heaps of newbies on Stack overflow, and they could learn a thing or two from your expertise - what's obvious to you might not be so to them.
    – Taryn East
    Jul 30, 2014 at 23:13
  • 1
    @TarynEast fair! I added some explanation.
    – Josh KG
    Jul 30, 2014 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.