Echoing a while loop within an echo

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Eiolon
Forum Newbie
Posts: 17
Joined: Tue Feb 14, 2006 1:42 pm

Echoing a while loop within an echo

Post by Eiolon »

I have a table that is using while to show repeating results. The table is shown inside an echo, and I am having trouble getting it to work without error. I get:

Parse error: syntax error, unexpected '}' in C:\wamp\www\polaris\index.php on line 144

I am not sure why the } is unexpected.

Code: Select all

<?php if (isset($_POST['submit_search_polaris'])) {

echo '

<table class="data">
  <tr>
    <th><div align="left">Name</div></th>
    <th><div align="left">Birthdate</div></th>
    <th><div align="left">Gender</div></th>
    <th><div align="left">Phone Number</div></th>
    <th><div align="left">Barcode</div></th>
  </tr>
  '; do { '
  <tr onmouseover="this.bgColor=#EBFAFF;" onmouseout="this.bgColor=#FFFFFF;">
    <td>$PatronFullName</td>
    <td>$Birthdate</td>
    <td>$Gender</td>
    <td>$PhoneVoice1</td>
	<td>$Barcode</td>
  </tr>
  ' } while ($row = mssql_fetch_assoc($result)); '
</table>

';

}

?>
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Echoing a while loop within an echo

Post by JakeJ »

It would help if we knew which line was 144.

I strongly suspect that it's the } near the bottom of the page though. Perhaps your opening and closing brackets aren't matching up. Check your pairs.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Echoing a while loop within an echo

Post by liljester »

you need to put 'echo' inside the loop. also dont forget to close the echo statement inside the loop. then echo the closing of the table as well.
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: Echoing a while loop within an echo

Post by liljester »

something like this:

Code: Select all

<?php

if (isset($_POST['submit_search_polaris'])) {

echo '

<table class="data">
  <tr>
    <th><div align="left">Name</div></th>
    <th><div align="left">Birthdate</div></th>
    <th><div align="left">Gender</div></th>
    <th><div align="left">Phone Number</div></th>
    <th><div align="left">Barcode</div></th>
  </tr>
  '; do { echo '
  <tr onmouseover="this.bgColor=#EBFAFF;" onmouseout="this.bgColor=#FFFFFF;">
    <td>$PatronFullName</td>
    <td>$Birthdate</td>
    <td>$Gender</td>
    <td>$PhoneVoice1</td>
        <td>$Barcode</td>
  </tr>
  '; } while ($row = mssql_fetch_assoc($result)); 
  echo' </table>
';

}

?>
Eiolon
Forum Newbie
Posts: 17
Joined: Tue Feb 14, 2006 1:42 pm

Re: Echoing a while loop within an echo

Post by Eiolon »

Thanks!
Post Reply