user1135192
user1135192

Reputation: 91

problems with echo img tag

I'm having problems echoing the src and alt attribute in the img tag. It doesn't display the image at all. Does any one know how I should be structuring this?

echo '<td rowspan="7">' . <img src=$row[url] alt=$row[caption] height="250" width="300"/> . '</td>';

Thanks

Upvotes: 0

Views: 2762

Answers (6)

Wes Crow
Wes Crow

Reputation: 2967

If I understand what you are after this should work:

echo '<td rowspan="7"><img src="{$row[url]}" alt="{$row[caption]}" height="250" width="300"/></td>';

Upvotes: 1

Adrian Serafin
Adrian Serafin

Reputation: 7715

echo '<td rowspan="7"><img src=".$row[url].'" alt='".$row[caption].'" height="250" width="300"/></td>';

Upvotes: 0

jprofitt
jprofitt

Reputation: 10964

You need to enclose the <img> tag in the quotes (and don't forget the double quotes around attribute values):

echo '<td rowspan="7"><img src="' . $row['url'] . '" alt="' . $row['caption'] . '" height="250" width="300"/></td>';

Also, rather than concatenating the string, you can use commas to echo. It's a trivial performance boost (users will almost certainly never notice), but good to know nonetheless!

echo '<td rowspan="7"><img src="', $row['url'], '" alt="', $row['caption'], '" height="250" width="300"/></td>';

Don't forget to sanitize your $row data so a stray " doesn't break your site.

Upvotes: 3

PachinSV
PachinSV

Reputation: 3780

I suggest you to separate the html tags from your php code, like this:

<td rowspan="7"><img src="<?php echo $row[url] ?>" alt="<?php echo $row[caption] ?>" height="250" width="300"/></td>

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270607

Your quoting is all messed up:

To fix your method with single-quoting and concatenation, use:

echo '<td rowspan="7"><img src="' . $row['url'] . '" alt="' . $row['caption'] . '" height="250" width="300"/></td>';

Or all as a double-quoted string, enclosing the variables in {} and placing single quotes around all attributes:

echo "<td rowspan='7'><img src='{$row[url]}' alt='{$row['caption']}' height='250' width='300'/></td>";

Upvotes: 2

Amber
Amber

Reputation: 526593

Your img HTML still needs to be in a string if you're trying to concatenate it:

echo '<td rowspan="7">' . "<img src=$row[url] alt=$row[caption] height=\"250\" width=\"300\"/>" . '</td>';

or simpler:

echo "<td rowspan=\"7\"><img src=$row[url] alt=$row[caption] height=\"250\" width=\"300\"/></td>";

Upvotes: 2

Related Questions