Problems to display a foreach (php) inside an html code

Asked

Viewed 337 times

0

Hello, everybody.

I have a little problem and I can’t get out of it. Here’s the problem:

I need to generate a pdf (report) with some data coming from a mysql query. So far, so good! But all this code that will appear in the pdf is being generated via PHP script mixed with HTML. In the middle of this code, I call a function and I need to display its result within an HTML snippet, here that is the problem, I can’t get the correct syntax of the foreach loop that is inside this HTML. Follow the code excerpt:

$montagem = "<!DOCTYPE html>
<html lang='pt-br'>
<head>
<meta charset='utf-8'>
</head>
<body>

<h1 style='font-size: 12px; text-align: center;'>ATESTADO ZOOSSANITÁRIO PARA 
CAPRINOS</h1>

<table style='width: 100%; margin: 0 20px;'>
<h1 style='font-size: 13px; text-align: center;'>IDENTIFICAÇÃO DOS ANIMAIS</h1>

<table cellspacing='0px' cellspadding='0px' style='width: 100%; margin: 0 20px;'>
<tr style='text-align: center; vertical-align: middle;'>
<td rowspan='2' style='border: 1px solid #000; border-right: 0 none; padding: 0;'>Ordem</td>
<td rowspan='2' style='border: 1px solid #000; border-right: 0 none;'>Espécie</td>
<td rowspan='2' style='border: 1px solid #000; border-right: 0 none;'>Raça</td>
<td colspan='2' style='border: 1px solid #000; border-right: 0 none; border-bottom: 0 none;'>Nº por Sexo</td>
<td rowspan='2' style='border: 1px solid #000;'>Total</td>
</tr>
<tr style='text-align: center;'>
<td style='border: 1px solid #000; border-right: 0 none;'>Macho</td>
<td style='border: 1px solid #000; border-right: 0 none;'>Fêmea</td>
</tr>
foreach ($caprinos2 as $caprino) { 
<tr class='text-center'>
<td>echo $caprinos2[capregistro]</td>
<td>$caprinos2[especie]</td>
<td>$caprinos2[raca]</td>
<td>$caprinos2[qtdmacho]</td>
<td>$caprinos2[qtdfemea]</td>
<td>$caprinos2[municipio]</td>
</tr>
}
</table>
</body>
</html>";

Someone would have an idea how to display the foreach loop result?

1 answer

4


PHP is PHP, HTML is HTML and string is string.

Foreach will never perform within the string, you have to concatenate, in PHP to do this use .= thus:

$foo = 'Olá ';
$foo .= 'Mundo';

echo $foo; //Vai exibir "Olá, Mundo"

The script should look like this:

$montagem = "<!DOCTYPE html>
<html lang='pt-br'>
<head>
<meta charset='utf-8'>
</head>
<body>

<h1 style='font-size: 12px; text-align: center;'>ATESTADO ZOOSSANITÁRIO PARA 
CAPRINOS</h1>

<table style='width: 100%; margin: 0 20px;'>
<h1 style='font-size: 13px; text-align: center;'>IDENTIFICAÇÃO DOS ANIMAIS</h1>

<table cellspacing='0px' cellspadding='0px' style='width: 100%; margin: 0 20px;'>
<tr style='text-align: center; vertical-align: middle;'>
<td rowspan='2' style='border: 1px solid #000; border-right: 0 none; padding: 0;'>Ordem</td>
<td rowspan='2' style='border: 1px solid #000; border-right: 0 none;'>Espécie</td>
<td rowspan='2' style='border: 1px solid #000; border-right: 0 none;'>Raça</td>
<td colspan='2' style='border: 1px solid #000; border-right: 0 none; border-bottom: 0 none;'>Nº por Sexo</td>
<td rowspan='2' style='border: 1px solid #000;'>Total</td>
</tr>
<tr style='text-align: center;'>
<td style='border: 1px solid #000; border-right: 0 none;'>Macho</td>
<td style='border: 1px solid #000; border-right: 0 none;'>Fêmea</td>
</tr>";

foreach ($caprinos2 as $caprino) { 

    $montagem .= "<tr class='text-center'>
    <td>echo $caprinos2[capregistro]</td>
    <td>$caprinos2[especie]</td>
    <td>$caprinos2[raca]</td>
    <td>$caprinos2[qtdmacho]</td>
    <td>$caprinos2[qtdfemea]</td>
    <td>$caprinos2[municipio]</td>
    </tr>";

}
$montagem .= "</table>
</body>
</html>";
  • Hello, William. Thank you for your help! But by making the following adjustments he is accusing the following error in the variables to be displayed: "Notice: Undefined index: capregistro in"

  • @Vandrépaulo This error has nothing to do with the adjustments. If you get google translator will know what it means, the problem is in your variable $caprinos2 that you didn’t even post in the question, ie it has nothing to do with the foreach.

  • True, William, true, true. This last problem was in the index of the variable I used to receive the function created at the beginning of the file, which I did not mention in the initial problem. I already solved, without the help of Google Translator. Thank you very much for the tip, it was of great value to my problem! A strong hug!

Browser other questions tagged

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