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

-1

This code returns several values for the database:

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

... and I need to send these values via email with PHPMailer . I need some help to get to the following result:

  

You viewed the property with code 1234

     

You viewed the property with code 5678

     

You have viewed the property with code 9012

     

You have viewed the property with code 3456

How could I for example put these values inside a variable separated by a space and then give an explode and do what you want of them?

    
asked by anonymous 17.11.2014 / 00:24

1 answer

5

There's a lot of it.

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 = ', ';    
}


Here you do not need explode , after all, if you use array , use the right type already.

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

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


Now, if the output is exactly what is in the body of the question, this would suffice:

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

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

    
17.11.2014 / 00:26