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


How to write PHP inside HTML when the HTML is wrapped with PHP

我把整个HTML内容在一个PHP变量和里面的内容,我试图从数据库回一些数据抓取。但问题是我不能使数据库查询和回声的结果,因为整个事情是用PHP包装,然后HTML。(如果听起来令人困惑,请检查下面的代码)

是否有办法在PHP变量之外进行数据库查询并回显它。虽然我已经尝试在变量之外查询数据库,但事实证明,这次>> $row['examdate']正在创建问题。显示一个语法错误。

我正在做所有这些使用DOMPDF生成PDF。在获得变量之后,我将把它传递给我的控制器来生成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

您需要将变量的填充与PHP逻辑的执行分开。

在稍后阶段添加数据,而不是尝试一次分配所有数据。

下面是修改后的代码:

<?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;

?>