View Full Version : hard coding html attributes into php

10-03-2007, 09:26 AM
Hi all,

I have a variable that contains the full image URL including the html image tag. This was the only way I could figure out how to display my images in the variable.

Does anyone know how to hard code html tags into php code.

eg. this is my variable

echo "<td>".$thumb_row."<br><td>";

... the variable contains

<img scr="path/to/image/image.jpg">

I would like the variable to just contain the file name image.jpg

How is this done, thanks


10-04-2007, 04:15 PM
give the variable the file name:

$image = "imageName.jpg";
Then with the html:

<img scr="<?php echo $image; ?>" />

10-05-2007, 03:27 PM
Thanks for the help, I tried that and my code still works okay but the table , rows and cols are built by the code, see here.....


...last post.

When I add html into the php echo's the php gets confused by the extra quotes and throws up errors in the browser.

I know there must be a way to do this. Any further ideas. Thanks.


10-05-2007, 06:12 PM
All you need is this:

<img scr="path/to/image/<?php echo $image;?>" />

so say $image = 'image232.jpg";

them ur html will show

<img scr="path/to/image/image232.jpg"/>

10-05-2007, 08:21 PM
Sorry, you misunderstood me.

Yes, I understand how to code the way you are saying.

The table, row and columns are built by the php code.


echo "<table>";//start the table
for ($rows = 0; $rows < $num_rows; $rows++) {//loop for the table rows
echo "<tr>";
for ($cols = 0; $cols < $num_cols; $cols++) {//loop for the table columns
if ($num < $thumb) { // show records if available (reduce by one because the first record is zero
$thumb_row = mysql_result($result, $num, "thumbURL");//create new variables with the values of the current record
echo "<td>".$thumb_row."<br><td>";
else { // show an empty cell
echo "<td>&nbsp;</td>";
$num++; // raise the number by one for the next record
echo "</tr>"; // no more cols in this row so close the table row
echo "</table>"; // close the table

where I have for example....

echo "<td>".$thumb_row."<br><td>";

I want to be able to add html to position the images in the cells.

For example, I may want to add... <div align="center">

When I try to add to an echo that has "centre", the quotes conflict with the php and cause errors.

Do you get where I'm coming from!!

Cheers, Paul

10-05-2007, 08:37 PM
any quotes in a string has to be non literal

example... this would error...
$var = "<table cellpadding="4" cellspacing="1" border="0">";

this would work...

$var = "<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\">";

by adding a back-slash you are telling the PHP engine to treat the quotes as string-like

10-05-2007, 10:24 PM
Thanks David. Such a simple thing, I did not know that.

How are you? Not seen you on the forum lately!

Your imput is missed. Hope all is well!

Regards, Paul

10-06-2007, 11:43 AM
been busy going 0 > 60 in 5 seconds!

10-06-2007, 01:17 PM
You've been going 0 > 60 in 5 seconds for a while now. Maybe you should slow down before you crash. :)

Will you be parking the car in the forum car park anytime soon. Could do with some expert coaching of the php kind.

if (0 > 60 in 5 seconds ); {
echo "too fast";
elseif ( you dont slow down ); {
echo "you might crash";

Take care. :)

10-06-2007, 05:20 PM
still in novelty mode