我将整个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
答案 0 :(得分:3)
您需要将变量的填充与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;
&GT;