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

当HTML用PHP包装时,如何在HTML中编写PHP,php,html,codeigniter-2,Php,Html,Codeigniter 2,我将整个HTML内容放在一个PHP变量中,并在该内容中尝试回显从数据库获取的一些数据。但问题是我无法进行数据库查询并回显reuslt,因为整个过程都是用PHP和HTML包装的(如果听起来很混乱,请检查下面的代码) 有没有办法在PHP变量之外进行数据库查询并对其进行回显。虽然我已经尝试在变量外部查询数据库,但正如这次所证明的那样>>$row['examdate']正在产生问题。它显示了一个语法错误 我正在使用DOMPDF生成PDF。得到变量后,我将把它传递给我的控制器以生成pdf <?ph

我将整个HTML内容放在一个PHP变量中,并在该内容中尝试回显从数据库获取的一些数据。但问题是我无法进行数据库查询并回显reuslt,因为整个过程都是用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;

奇怪的是,你怎么会得到这么多不工作的代码?为什么,作为一个新手,你不在每一行之后检查它是否工作吗?!?!?!?!在这种情况下,您可能只有一行有问题,但现在您有20行用于大字符串变量。但在选中的答案中可能重复@mario:可怕的示例。在这两种情况下,使用单引号Hanks favo更容易。你的解决方案正是我想要的。它工作得很好。再次感谢(法沃)