-4

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
black_belt
  • 6,601
  • 36
  • 121
  • 185
  • Curious how did you get so huge amount of not working code? Why, as a newbie, don't you check that it works after each line?!?!?!?! In that case you would have only one line that you have problem with, but now you have 20 – zerkms Dec 31 '11 at 07:03
  • Use the [HEREDOC syntax](http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) for large string variables. – mario Dec 31 '11 at 07:07
  • possible duplicate of [Is there a reason to use Heredoc in PHP?](http://stackoverflow.com/questions/5673269/is-there-a-reason-to-use-heredoc-in-php) – mario Dec 31 '11 at 07:09
  • @mario: terrible examples in the checked answer though. In both cases it is easier to use single quotes – zerkms Dec 31 '11 at 07:14

1 Answers1

3

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;

?>

 

favo
  • 5,426
  • 9
  • 42
  • 61