How to put the data of a while inside a variable?

Asked

Viewed 1,575 times

-1

This code returns me several values of the database:

while($TiV = $frm_interesse->fetch()){    
    echo $TiV['CODIGO'];    
}

... and I need to send these values by e-mail with the PHPMailer. I need some help to get the following result:

You have viewed the property with code 1234

You have viewed the property with code 5678

You viewed the property with code 9012

You viewed the property with code 3456

How could I for example put these values inside a variable separated by a space and then explode and do whatever I want with them?

1 answer

5


There’s a lot of shape.

Here one in text format, to use for example in the body of an email:

$separador = '';
$codigos = '';
while( $TiV = $frm_interesse->fetch() ) {  
    $codigos .= $separador. $TiV['CODIGO'];
    $separador = ', ';    
}


In this one you don’t need explode, after all, if you’re going to use array, already use the right type.

$codigos = array();
while( $TiV = $frm_interesse->fetch() ) {  
    $codigos[] = $TiV['CODIGO'];
}

// visualização:
echo implode( ', ', $codigos );


Now, if the exit is exactly what is in the body of the question, would that suffice:

$codigos = '';
while( $TiV = $frm_interesse->fetch() ) {  
    $codigos .= 'Você visualizou o imóvel com código '.$TiV['CODIGO'].PHP_EOL;
}

(the line break PHP_EOL you adapt as output - email, page, etc.)

  • I don’t know if it’s a good question but how do I give a echo in $codigos within the script of PHPMailer?

  • You don’t have to echo, you insert the $variable in the body of the email. $corpodoemail = "Olá, visitante! " . $codigos; and then according to the Phpmailer documentation you insert $corpodoemail in the right place. There are examples of using Phpmailer here on the site itself, just a quick search.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.