如何在mPDF中的$ html中传递if语句?

问题描述:

How can I pass the if statement in the $html? I'm building pdf from the values of a form and I'm not sure how I can pass this if statement with the html.

I'm using mPDF version 6 to generate pdf.

I've researched for a while but didn't couldn't figure out the right way. So, I really appreciate your suggestions. Thanks in advance.

<?php
   include('mPDF/mpdf.php');

   $name = $_POST["name"];
   $age = $_POST["age"];

   $html = '<html>
               {if (!empty($_POST['name'])) echo 'Name: ' . $name;}<br>
               {if (!empty($_POST['age'])) echo 'Age: ' . $age;}
            </html>';
   $mpdf = new mPDF();
   $mpdf->WriteHTML($html);
   $mpdf->Output();
   ?>

This is the form in case you need a look:

<form method="post" action="generatePDF.php">   
   Name: <input type="text" name="name" id="name"  /><br>
   Age: <input type="text" name="age" id="age"  /><br>
</form>

如何在 $ html code中传递 if code>语句 >? 我正在从表单的值构建pdf,我不知道如何使用html传递此 if code>语句。 p>

我正在使用 mPDF code>版本6来生成pdf。 p>

我已经研究了一段时间,但却无法找到正确的方法。 所以,我非常感谢你的建议。 在此先感谢。 p>

 &lt;?php 
 include('mPDF / mpdf.php'); 
 
 $ name = $ _POST [“name”];  
 $ age = $ _POST [“age”]; 
 
 $ html ='&lt; html&gt; 
 {if(!empty($ _ POST ['name']))echo'Name:'。  $ name;}&lt; br&gt; 
 {if(!empty($ _ POST ['age']))echo'Oge:'。  $ age;} 
&lt; / html&gt;'; 
 $ mpdf = new mPDF(); 
 $ mpdf-&gt; WriteHTML($ html); 
 $ mpdf-&gt; Output(); 
?  &gt; 
  code>  pre> 
 
 

这是您需要查看的表格: p>

 &lt; form method =“  post“action =”generatePDF.php“&gt;  
名称:&lt; input type =“text”name =“name”id =“name”/&gt;&lt; br&gt; 
年龄:&lt; input type =“text”name =“age”id =“age  “/&gt;&lt; br&gt; 
&lt; / form&gt; 
  code>  pre> 
  div>

I will rather change this to below, so that I don't' have check for conditions in HTML. Perform all conditions beforehand and have the variables ready which can be used in HTML, to generate the PDF:

<?php
   include('mPDF/mpdf.php');

   $name = !empty($_POST["name"]) ? 'Name: ' . $_POST['name'] : '';
   $age = !empty($_POST["age"]) ? 'Age: ' . $_POST["age"] : '';

   $html = '<html>' . 
               $name . '<br>' .
               $age . 
            '</html>';
   $mpdf = new mPDF();
   $mpdf->WriteHTML($html);
   $mpdf->Output();
   ?>

First you need to use Double Quote

$html=" code "; // because variable execute only in double quote 

Second you need to use if statement outside the string

if (!empty($_POST['name'])){ $prnt_name='Name: ' . $name;}
if (!empty($_POST['age'])) { $prnt_age= 'Age: ' . $age;}
$html = "<html>
           $prnt_name<br>
           $prnt_age
       </html>";

You cannot embed PHP code in string literals. Build your $html string in a conventional way:

<?php
   include('mPDF/mpdf.php');

   $name = $_POST["name"];
   $age = $_POST["age"];

   $html = '<html>' . (!empty($name) ? 'Name: ' . $name : '') . '<br>'
                    . (!empty($age) ? 'Age: ' . $age : '') .
            '</html>';
   $mpdf = new mPDF();
   $mpdf->WriteHTML($html);
   $mpdf->Output();
   ?>