Re: [PHP] putting variables in a variable

This is a discussion on Re: [PHP] putting variables in a variable within the PHP General forums, part of the PHP Programming Forums category; On Mon, Mar 28, 2011 at 7:06 AM, Hulf <ross@blue-fly.co.uk> wrote: > Hi, &...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-28-2008
Daniel Brown
 
Posts: n/a
Default Re: [PHP] putting variables in a variable

On Mon, Mar 28, 2011 at 7:06 AM, Hulf <ross@blue-fly.co.uk> wrote:
> Hi,
>
> I am making and HTML email. I have 3 images to put in. Currently I have
>
> $body .="
> <table>
> <tr>
> <td><img src=\"image1.jpg\"></td>
> </tr>
>
> <tr>
> <td></td>
> </tr>
> </table>
> ";
>
>
> ideally I would like to have
>
> $myimage1 = "image1.jpg";
> $myimage2 = "image2.jpg";
> $myimage3 = "image3.jpg";
>
>
> and put them into the HTML body variable. I have tried escaping them in
> every way i can think of, dots and slashes and the rest. Any ideas?


Consider HEREDOC syntax. I'm not sure why you're doing your
tables like that, but I'm sure you have your reasons. Also, I'm
certain that hardcoding the image names like that is just for example,
since it would be much easier to use an array or something similar (if
it's not coming from a database or otherwise dynamically-generated).

<?php

$myimage1 = "image1.png";
$myimage2 = "image2.png";
$myimage3 = "image3.png";

$body .=<<<EOT
<table>
<tr>
<td><img src="$myimage1"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><img src="$myimage2"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td><img src="$myimage3"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
</table>
EOT; // This has to be at position one on the line, no matter what.
?>

And, for good measure, the array way:

<?php

$images = array('image1.png','image2.png','image3.png');

echo "<table>\n";

foreach($images as $p => $v) {
$body .=<<<EOT
<tr>
<td><img src="$v"></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>

EOT; // Notice the blank line. That ensures a linebreak after the last </tr>
}

echo "</table>\n";
?>

--
</Daniel P. Brown>
Forensic Services, Senior Unix Engineer
1+ (570-) 362-0283
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:22 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0