PDA

View Full Version : PHP and <img> problem



Earendil
20th September 2009, 02:44 AM
EDIT: I fixed the problem by wrapping the title and alt text in single quotes.

In my database, I have an image table with ID, SRC, TITLE, and ALT columns. My problem is with this line of code I have on the site to display the images.

echo " <img src=\"".$row['src']."\" title=".$row['title']." alt=".$row['alt']." /> ";

The image displays fine, but the title and alt text are both cut off at the first white space. So if the alt text is "Bob rocks" the alt just shows "Bob". I've already echo'ed $row['title'] and it's being retrieved as the full string from the database. I've even tried replacing spaces with &nbsp, but that gets included literally. Even randomly tried htmlspecialchars and htmlentities, but those obviously didn't help.

Oh, and I've tried just putting in something like title="static text" instead of with a variable, and that works fine.

So, there's obviously something wrong with... something. My DB collation is utf8_unicode_ci if that makes a difference.

PS: On an unrelated note, I just noticed my use of the <img> tag in my title adversely affected the new post sidebar thing. Whoever runs that might want to fix that...or not.

xAre
17th November 2009, 07:10 AM
Sounds odd...
Still having the problem?
I would try something like...

printf("<img src=\"%s\" title=\"%s\" alt=\"%s\" />",$row['src'],$row['title'],$row['alt']);

Doctor Rock
11th December 2009, 10:53 AM
echo "<img src=\"".$row['src']."\" title=".$row['title']." alt=".$row['alt']." /> ";
You have put " " around the src-attribute, but not the title or alt? Mayeb this caused the problem?

Since you'd have something like this:
<img src="image.jpeg" title=my image alt=a picture />

XHTML doesn't see how "image" and "picture" fit in without the quatation marks.

MaradoX-
28th February 2010, 10:35 AM
echo "<img src=\"".$row['src']."\" title=".$row['title']." alt=".$row['alt']." /> ";
You have put " " around the src-attribute, but not the title or alt? Mayeb this caused the problem?

Since you'd have something like this:
<img src="image.jpeg" title=my image alt=a picture />

XHTML doesn't see how "image" and "picture" fit in without the quatation marks.

Or do it like this:



echo ' <img src="' . $row[src] . '" title="' . $row[title] . '" alt="' . $row[alt] . '" /> ';