Page 1 of 1

Echoing a while loop within an echo

Posted: Tue Jul 20, 2010 4:34 pm
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>

';

}

?>

Re: Echoing a while loop within an echo

Posted: Tue Jul 20, 2010 4:57 pm
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.

Re: Echoing a while loop within an echo

Posted: Tue Jul 20, 2010 4:58 pm
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.

Re: Echoing a while loop within an echo

Posted: Tue Jul 20, 2010 5:01 pm
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>
';

}

?>

Re: Echoing a while loop within an echo

Posted: Tue Jul 20, 2010 5:07 pm
by Eiolon
Thanks!