tags:

views:

258

answers:

7

I am trying to replace the HTML code with a heredoc statement. However, I am getting a parse error in the last line.I am sure that I have not left any leading space or indentation on the heredoc closing tag line.Following is a part of the code:

$table = <<<ENDHTML
    <div style="text-align:center;">
    <table border="0.5" cellpadding="1" cellspacing="1" style="width:50%; margin-left:auto; margin-right:auto;">
    <tr>
    <th>Show I.D</th>
    <th>Show Name</th>
    </tr>
    ENDHTML;
    while($row = mysql_fetch_assoc($result)){
         extract($row);
         $table .= <<<ENDHTML
         <tr>
          <td>$showid2 </td>
          <td>$showname2</td>
         </tr>
    ENDHTML;     
    }
    $table .= <<<ENDHTML
    </table>
    <p><$num_shows Shows</p>
    </div>
    ENDHTML; 
    echo $table;
    ?>

Where is the problem? I have a related question in addition to above. As a coding practice, is it better to use PHP code throughout or is it better to use a heredoc syntax. I mean, while in PHP mode, the script bounces back and forth between the HTML and PHP code. So, which is the preferred method?

+1  A: 

<p><$num_shows Shows</p>

Is that an extraneous '<' character before $num_shows?

Rob Knight
I just added here by mistake. Although it's not present in the original code.
Learner
And even if it was there, it couldn't generate a parse error, I think.
x3ro
+2  A: 

If this code is exactly what you are using, then there are a number of spaces down the left hand side with the exception of the first $table = <<<ENDHTML line. The ending heredoc needs to be exactly left-aligned with no spaces, tabs or other characters to the left of it.

Richy C.
Everything is perfectly on the left side in my code editor. It's only showing the left spaces here.God..I will tear my hairs apart :(
Learner
Can you reduce the code to a smaller test case? I.e. remove heredocs until you no longer receive the error?
Richy C.
Actually I have run the code without heredoc suntax i.e by using HTML with PHP code..that runs pretty well..I just wanted to try the heredoc syntax..and then came the problem :(
Learner
A: 

Wouldn't this pose a problem?

</tr>
ENDHTML;

You should probably:

ENDHTML;

Edit: Formatting failed ... and Richy C. said the same in the meantime :( I just meant to show that the first ENDHTML;has whitespace in front of it.

jensgram
A: 

Edit: changed to ob and back and now the loop works for me ...

while($row = mysql_fetch_assoc($result)){
    extract($row);
    $table .= <<< ENDHTML
    <tr>
     <td>$showid2</td>
     <td>$showname2</td>
    </tr>
ENDHTML;
}
OIS
Will the loop problem generate a parse error??
Learner
I just changed it from ob to heredoc and it works actually ...
OIS
+1  A: 

From the PHP manual about the Heredoc syntax:

The closing identifier must begin in the first column of the line.

And a little later in the nice red Warning box:

It is very important to note that the line with the closing identifier must contain no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.

So you need to write the code like this to comply with the syntax specification:

$table = <<<ENDHTML
    <div style="text-align:center;">
    <table border="0.5" cellpadding="1" cellspacing="1" style="width:50%; margin-left:auto; margin-right:auto;">
    <tr>
    <th>Show I.D</th>
    <th>Show Name</th>
    </tr>
ENDHTML;
    while($row = mysql_fetch_assoc($result)){
                extract($row);
                $table .= <<<ENDHTML
                <tr>
                        <td>$showid2 </td>
                        <td>$showname2</td>
                </tr>
ENDHTML;
    }
    $table .= <<<ENDHTML
    </table>
    <p><$num_shows Shows</p>
    </div>
ENDHTML;
    echo $table;

It’s up to you if you really want to use that.

Gumbo
I tried nearly everything but that parse code is just not bidding me goodbye. I think it's better if I bid goodbye to heredoc
Learner
A: 

The string delimiter needs to be by itself on a line, in the very first column. That is, you cannot add spacing or tabs around it.

adatapost
A: 

Guys, finally I succeeded in getting the parse error out of my way (phew!!). I just rewrote the code and it worked. Here is the code:

$table = <<<ABC
  <div style="text-align:center;">
  <table border="0.5" cellpadding="1" cellspacing="1" style="width:50%; margin-left:auto; margin-right:auto;">
  <tr>
  <th>Show I.D</th>
  <th>Show Name</th>
  <th>Show Genre</th>
  </tr>
ABC;
  while($row = mysql_fetch_assoc($result))
  {
      extract($row);
$table .= <<<ABC
      <tr>
      <td>$showid2 </td>
      <td>$showname2</td>
      <td>$showtype2_label</td>
      </tr>
ABC;
  }
$table .= <<<ABC
  </table>
  <p>$num_shows Shows</p>
  </div>
ABC;

echo $table;
Learner