tags:

views:

35

answers:

2

Hi

I am getting the image path from database in this foreach

foreach($image as $row){ 
  $value = $row['dPath'];
  $imgpath =base_url()."images/".$value;//this is not taken
  $imgpath =  base_url()."images/con_icon.jpg";//this$imgpath is taken

  echo $value;

when i give $imgpath as $imgpath = base_url()."images/con_icon.jpg"; it is accepted in

<img src="<?php echo $imgpath; ?>" and image is displayed

But when i give $imgpath as $imgpath =base_url()."images/".$value; but echo $value; results con_icon.jpg The image is not displayed what is the problem

EDIT:

echo $imgpath =base_url()."images"."/".$value;
 echo $img =  base_url()."images/con_icon.jpg";

gave me this

http://localhost/ssit/images/con_icon.jpg
http://localhost/ssit/images/con_icon.jpg 

then why cant i get this in my <img>

<img src="<?php echo $imgpath; ?>" name=b1 width=90 height=80 
 border=0 onmouseover=mouseOver() onmouseout=mouseOut()>
+3  A: 

make sure your $value does not contain extra whitespace at the front or end. use

$value = trim($value);

to remove whitespace. also echo is not the best way to quick-debug variables, use var_dump instead.

and please make sure to escape your imagepath to prevent XSS

edit

you cannot say <img src="<?php echo $imgpath; ?>" name=b1 width=90 height=80 border=0 onmouseover=mouseOver() onmouseout=mouseOut()> because you have whitespace at the end of your string. use <img src="<?php echo trim($imgpath); ?> … /> if you have to use it this way.

apart from that, quote your attributes: onmouseover="mouseOver", don't use parentheses after your event handler names (unless mouseOver() returns a function—i don't think you are doing that …). and you should use urlencode for your imagepath, to lock out all those malicious hackers who want to harm your users

knittl
@udaya: see, there's the extra space at the end. it should be `string(12) "con_icon.jpg"` and not `string(18) "con_icon.jpg[whitespace]"`
knittl
why the downvote⁉⁉?!? the answer is perfectly valid and even addresses the issue perfectly: whitespace in the filename
knittl
@knittl see my edit...
udaya
@udaya: **DON'T** use echo, use `var_dump`! from your var_dump result it's clearly visible you have erroneous whitespace in your imagepath. use trim to remove them
knittl
@knittl please edit your answer where should i include it?
udaya
@udaya: i think everything is in my answer. write `$value = trim($value)` before you start appending it to your imagepath. you can also write `$value = trim($row['dPath']);` in your foreach loop
knittl
@knittl when saw the generated source in webdeveloper i got the source as src="http://localhost/ssit/images/con_icon.jpg%3Cbr%20/%3E"i made the changes like trim($value);
udaya
@udaya, ok, stackoverflow stripped the html tag at the end. you should enclose code output in `backticks` even in comments. you have a line break `<br />` at the end of your string. i don't know how it went there, but you should probably clean up the value in your db (or where the image path is stored)
knittl
wow There vis a break in database itself i cleared that and it works now .....Really great effort yaar
udaya
A: 

Make sure that $value is not coming empty:

var_dump($value);

Also, you may try this instead:

$imgpath = get_bloginfo('template_url') . "/images/" . $value;
Sarfraz
var_dump retrns string(18) "con_icon.jpg "
udaya
@udaya: use the `trim` before value function as suggested by knittl
Sarfraz
@sarfraz: there's a linebreak tag at the end `<br />`, not whitespace. stackoverflow won't display it in comments, unless it's enclosed in backticks. his string is `con_icon.jpg<br />`
knittl
Ya it works now
udaya