2011-12-31 117 views
-4

我将整个HTML内容放入PHP变量中,并在该内容中尝试回显从数据库获取的某些数据。但问题是我无法进行数据库查询和回显reuslt,因为整个事情都是用PHP和HTML封装的(如果听起来令人困惑,请查看下面的代码。)如何在使用PHP封装HTML时在HTML内写入PHP

有什么办法可以让数据库在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

好奇你是怎么不工作的代码,这样巨量?为什么作为一个新手,你不检查它是否在每行之后都有效?!?!?!?!在这种情况下,你只有一行你有问题,但现在你有20 – zerkms 2011-12-31 07:03:56

+0

使用[HEREDOC语法](http://www.php.net/manual/en/language.types.string.php# language.types.string.syntax.heredoc)用于大型字符串变量。 – mario 2011-12-31 07:07:42

+0

可能的重复[是否有理由在PHP中使用Heredoc?](http://stackoverflow.com/questions/5673269/is-there-a-reason-to-use-heredoc-in-php) – mario 2011-12-31 07:09:28

回答

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; 

>

+0

感谢favo。您的解决方案正是我一直在寻找的。它完美的作品。再次感谢Favo。 :) – 2011-12-31 15:40:04

+0

第三个支架在这里是关键点... :) – 2011-12-31 15:41:38