当用PHP包装HTML时,如何在HTML中编写PHP

I am putting a whole HTML content inside a PHP variable and inside that content I am trying to echo some data fetching from database. But the problem is I cannot make the database query and echo the reuslt as the whole thing is wrapped with PHP and then HTML.(if it sounds confusing please check the code below.)

Is there any way to make the database query outside the PHP variable and echo it. Although I have already tried querying the database outside the variable but as it turned out this time >> $row['examdate'] is creating the problem. It shows an syntax error.

I am doing all these to generate PDF using DOMPDF. After getting the variable I will pass it to my controller to generate the pdf.

 <?php $variable= " I need your help, I want to echo this <br>

<table id=\"table-6\" width=\"100%\">
<tr>
    <th>Exam Date</th>
    <th>Exam Type</th>
    <th>Subject</th>

</tr>

$this->db->select('*');
$this->db->from('marks');
$this->db->where('studentid', $studentid);
$this->db->where('examdate >=', $daterange1);
$this->db->where('examdate <=', $daterange2);
$this->db->order_by('examdate','DESC'); 
$query = $this->db->get(''); 


            if ($query->num_rows() > 0)
            {
               $row = $query->row_array();

    <tr>
        <td> echo $row['examdate']</td> ///****this line***** 

        <td>12</td>
        <td>12</td>
        <td>100</td>
    </tr>
    </table>
   "; ?>-<< variable ends here

You need to separate the filling of your variable from the execution of your PHP logic.

Append the data at a later stage instead of trying assign everything in one single step.

Here is a modified code:

<?php
$variable = " I need your help, I want to echo this <br>

<table id=\"table-6\" width=\"100%\">
    <tr>
        <th>Exam Date</th>
        <th>Exam Type</th>
        <th>Subject</th>

    </tr>
";

// now execute the database logic
$this->db->select('*');
$this->db->from('marks');
$this->db->where('studentid', $studentid);
$this->db->where('examdate >=', $daterange1);
$this->db->where('examdate <=', $daterange2);
$this->db->order_by('examdate','DESC'); 
$query = $this->db->get(''); 

if ($query->num_rows() > 0)
{
    $row = $query->row_array();

    // append the data to the existing variable using ".="
    // and include the examdate
    $variable .= "
        <tr>
            <td>{$row['examdate']}</td> ///****this line***** 

            <td>12</td>
            <td>12</td>
            <td>100</td>
        </tr>
    ";
}

// append the closing table tag to the variable using ".=" again
$variable .= "</table>";

// output $variable
echo $variable;

?>