-1

I have this code

foreach ($codes as $code => $total)  {
    printf("%s <font color='blue'><b>Total clicks = %d<br></b></font>", $code, $total); 
}

Which gives me the output

01 output 1 Total clicks = 1
02 output 2 Total clicks = 1
03 output 3 Total clicks = 1
03 output 4 Total clicks = 1
03 output 5 Total clicks = 1

etc

How can I assign the output to a variable to use in a mailout i.e.

$message5 = each line of output

so that my mail out shows the same ouput

01 output 1 Total clicks = 1
02 output 2 Total clicks = 1
03 output 3 Total clicks = 1
03 output 4 Total clicks = 1
03 output 5 Total clicks = 1

a member suggested using sprintf so I tried this

foreach ($codes as $code => $total)  {
    printf("%s <font color='blue'><b>Total clicks = %d<br></b></font>", $code, $total); 
    $message5 = sprintf("%s <font color='blue'><b>Total clicks = %d<br></b></font>", $code, $total); 
}

But it only gives me the last line of output user3783243 suggests that

$message5 =  overwrites on every iteration 

How can I stop the overwrite?

Please note this issue has now been resolved

6
  • sprintf only gives me the last line of output Commented Nov 3, 2023 at 17:59
  • Use $message5 .= sprintf(... Commented Nov 3, 2023 at 18:05
  • $message5 = overwrites on every iteration Commented Nov 3, 2023 at 18:30
  • yes how to stop the overwriting? Commented Nov 3, 2023 at 18:40
  • What was already sent .= assigns to existing variable. Commented Nov 3, 2023 at 18:48

3 Answers 3

0

Let's try with this

    $message = '';
            
    foreach ($codes as $code => $total)  {
        $message .= sprintf("%s <font color='blue'><b>Total clicks = %d<br></b></font><br>", $code, $total); 
    }

    echo $message;
Sign up to request clarification or add additional context in comments.

7 Comments

works in the echo on screen but does not give the output in the mailout
what is the mailout? how are you trying to use it? assign each line to a dynamic variable? or the full data in a single variable?
what is the mailout? maybe is better to describe the whole scenario you have in mind - WDYT?
trying to use $message5 to assign each line of output in the email, so I have $message = $message1. " ".$message2. " ". $message3. " ". $message5, my email line is mail($emailaddress[$z], $emailsubject, $message, $headers);
@mark.four It should work. Are you sure you changed = to .=?
|
0

As I adhere from the comments and the questions you want to concatenate each message separately

$final_message = []; 
foreach ($codes as $code => $total)  {
    printf("%s <font color='blue'><b>Total clicks = %d<br></b></font>", $code, $total); 
   // message5 or 4 it doesn't matter it will get each message on each iterate
    $message5 = sprintf("%s <font color='blue'><b>Total clicks = %d<br></b></font>", $code, $total); 
    $final_message[] = $message5;
}
var_dump($final_message); // $final_message[0], $final_message[1] etc..

3 Comments

Mostafa Ezzat, thanks, works on the screen, but Still only gives the last line on the mail line, mail($emailaddress[$z], $emailsubject, $message, $headers);
try $final_message = imploade("", $final_message); mail($emailaddress[$z], $emailsubject, $final_message , $headers);
0

If I assume your $codes variable matches below data, then it would work without any issue... if still doesn't work please show us whole code to check...

$codes = array(
    '01 output 1' => 1,
    '02 output 2' => 1,
    '03 output 3' => 1,
    '03 output 4' => 1,
    '03 output 5' => 1,
);

$message5 = '';

foreach ( $codes as $code => $total )
{
    $message5 .= sprintf( "%s <font color='blue'><b>Total clicks = %d<br></b></font>", $code, $total ); 
}

mail( $emailaddress[$z], $emailsubject, $message5, $headers );

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.