-1

I have a series of echo statements that are going to be a pain as I need to add more formatting. I found an example of HEREDOC here that looked pretty good.

I want to replace these series of echo statements but it is not working.

Existing code:

while($row = mysqli_fetch_array($result))
  {

  echo "<tr>";
  echo "<td>" . $row['EmpFirstName'] . "</td>";
  echo "<td>" . $row['EmpLastName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

New attempt (not working)

  while($row = mysqli_fetch_array($result))
      {

      echo <<<EOL 
       <tr>
         <td>" . $row['EmpFirstName'] . "</td>
         <td>" . $row['EmpLastName'] . "</td>
      </tr>
      }
    </table>
      EOL;
Community
  • 1
  • 1
Rocco The Taco
  • 3,695
  • 13
  • 46
  • 79
  • 1
    Sidenote: Your posted code contains spaces before `EOL;` there must not be any before it. (Most probably the (or a) cause, if that's the actual code you're using). – Funk Forty Niner Feb 26 '14 at 21:29

1 Answers1

1

You're opening the HEREDOC inside the while loop, but not closing it until after the loop. And you don't need to open and close quotes to include the variables. You need to do it this way:

while($row = mysqli_fetch_array($result))
{
    echo <<<EOL 
       <tr>
           <td>{$row['EmpFirstName']}</td>
           <td>{$row['EmpLastName']}</td>
       </tr>
EOL;
}
echo "</table>";

Note the lack of spaces before EOL, as noted by Fred and FreshPrince.

Mark Parnell
  • 9,175
  • 9
  • 31
  • 36
  • I'm getting a syntax error starting on this line still? echo << – Rocco The Taco Feb 26 '14 at 22:37
  • It's not that line that's the problem, see my updated answer. You need the curly braces (`{}`) around the array variables (or you can remove the single quotes around the keys, but I prefer to use the braces). – Mark Parnell Feb 26 '14 at 22:43
  • I'm still getting the syntax error...screenshot here: https://www.dropbox.com/s/j30qw7g53ebta1i/syntax.jpg – Rocco The Taco Feb 27 '14 at 11:55
  • I can't see anything wrong with that code and it works fine when I try it - do you get an error when actually viewing the page, or just in your editor? – Mark Parnell Feb 27 '14 at 21:32