Send by php mail function data from a table

Asked

Viewed 1,069 times

0

Hi Dev I am there some three days trying to work out a way to send data from a table by function mail php. Sending data from just one column is basically easy, but it gets complicated when I want to send data from more than one column. That’s because I need to make one while to call all data, but how will I send this called data in the body of the function email mail? In the example below note that the fourth argument of the email function is precisely the variable that contains a set of other variables.

But when the email is submitted only the last element of the variable $html is warrant.

$buscando = mysql_query("SELECT * FROM guardapedidos WHERE id_cliente = '$idDoCliente'")
or die(mysql_error());
while ($linha = mysql_fetch_array( $buscando )) {


  $nomeProdutoG = $linha['nomeProdutoG'];
  $quantidadeG = $linha['quantidadeProdutoG'];
  $precoProdutoG = $linha['precoProdutoG'];
  $subTotalG = $linha['subTotalG'];
  $totalG = $linha['totalG'];


  echo  $html = "
     <b>Nome</b> $nomeProdutoG.<br>
     <b>Quantidade</b> $quantidadeG.<br>
     <b>Preço</b> $precoProdutoG.<br>
     <hr>
   ";

}

$envio = mail($destino, $assunto, $html, $headers); 
  • Study on String operators, will help you in your current problem and in future projects.

  • Valdiney France, I’m having a problem sending email with this function, could you tell me how you set up your smtp? Thanks.

1 answer

2

What’s happening is that you’re creating the variable again $hmtl each iteration of loop, you need to concatenate the records, try so.

$buscando = mysql_query("SELECT * FROM guardapedidos WHERE id_cliente = '$idDoCliente'")
or die(mysql_error());
$html = ''; // cria variável $html fora do loop
while ($linha = mysql_fetch_array( $buscando )) {


  $nomeProdutoG = $linha['nomeProdutoG'];
  $quantidadeG = $linha['quantidadeProdutoG'];
  $precoProdutoG = $linha['precoProdutoG'];
  $subTotalG = $linha['subTotalG'];
  $totalG = $linha['totalG'];

  // concatena a cada iteração
  $html .= " 
     <b>Nome</b> $nomeProdutoG.<br />
     <b>Quantidade</b> $quantidadeG.<br />
     <b>Preço</b> $precoProdutoG.<br />
     <hr />
   ";

}

$envio = mail($destino, $assunto, $html, $headers);

Browser other questions tagged

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