views:

197

answers:

5

Hi, I'm French so I maybe not speak english very well.

I'm trying to "put" the result of a foreach loop in a varible like that :

$msg = '<html><head>
        <title>Commande de photos</title>
 </head><body>
        <p>Voici la liste des photos demand&eacute;es :<br />
 <ul> HERE I WANT TO "PUT" THE RESULT OF THE FOREACH LOOP</ul>';

Here is my foreach loop:

foreach($tab as $val){
echo('<li>'.$val.'</li>');
}

Then the $msg enter in the composition of the funtion mail() like that:

mail($destinataire,$sujet,$msg,$headers);

So how can I do to include the result of foreach in a message because I have already a bug ! Thanks

Pico

+3  A: 
$msg = '<html><head><title>Commande de photos</title></head><body><p>Voici la liste des photos demand&eacute;es :</p><ul>';
foreach($tab as $val){
     $msg .= '<li>' . $val . '</li>';
}
$msg .= '</ul>';
mail($destinataire,$sujet,$msg,$headers);

The trick here is the .= concatenation operator. For example:

$x = 'abc';
echo $x; // echoes 'abc'
$x .= 'def';
echo $x; // echoes 'abcdef'
cpharmston
+2  A: 

Like so?

$list = '';
foreach($tab as $val){
    $list .= '<li>'.$val.'</li>';
}

$msg = '<html><head>
    <title>Commande de photos</title>
</head><body>
    <p>Voici la liste des photos demand&eacute;es :<br />
<ul>'.$list.'</ul>';

mail($destinataire,$sujet,$msg,$headers);
davethegr8
A: 
ob_start();
// here your loop echo'ing stuff
$content = ob_get_clean();

See php man pages for more info on ob_* functions

p4bl0
A: 

Thanks cpharmston !!! It's work perfectly !!

Pico
A: 

Not so much an answer - but davethegr8's answer worked for me! I am attemting my first php template system intergration using Savant3 and this helpped keep all the logic seperate from the template file. Thanks!

jakelbies