I have a repeating section from a MySQL look-up - the code (including HTML) is this:
<br>
<?php do { ?>
<?php if (substr($row_mDetails['Lo'], 0, 1) === '-') {echo str_replace('.', '', number_format($row_mDetails['Lo'], 5));} else {echo '+'.str_replace('.', '', number_format($row_mDetails['Lo'], 5));}; ?> |
<?php if (substr($row_mDetails['La'], 0, 1) === '-') {echo str_replace('.', '', number_format($row_mDetails['La'], 5));} else {echo '+'.str_replace('.', '', number_format($row_mDetails['La'], 5));}; ?>
| <?php echo $row_mDetails['Name']; ?> | 0 |<br>
<?php } while ($row_mDetails = mysql_fetch_assoc($mDetails)); ?>
How do I get the output of this to a string (for example, $MyDataString = *the output from the above*
)
Whenever I try this, I get the error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
(Edited to include surrounding HTML)
You need to remove the php
from the start of the second line. Then the code runs fine. I would advocate making this code slightly more readable:
<?php
do {
if (substr($row_mDetails['Lo'], 0, 1) === '-') {
echo str_replace('.', '', number_format($row_mDetails['Lo'], 5));
} else {
echo '+' . str_replace('.', '', number_format($row_mDetails['Lo'], 5));
}
?> | <?php
if (substr($row_mDetails['La'], 0, 1) === '-') {
echo str_replace('.', '', number_format($row_mDetails['La'], 5));
} else {
echo '+' . str_replace('.', '', number_format($row_mDetails['La'], 5));
}
?> | <?php
echo $row_mDetails['Name'];
?> | 0 |<br> <?php
} while ($row_mDetails = mysql_fetch_assoc($mDetails));
?>
I removed the unnecessary ;
from after your else
s.
It's a question of personal preference but you could also replace your
?> | <?php
with
echo " | ";
to avoid opening and closing tags.
If you want to build your string in a loop, you could do something like this:
do {
if (substr($row_mDetails['Lo'], 0, 1) === '-') {
$str .= str_replace('.', '', number_format($row_mDetails['Lo'], 5));
} else {
$str .= '+' . str_replace('.', '', number_format($row_mDetails['Lo'], 5));
}
$str .= " | ";
if (substr($row_mDetails['La'], 0, 1) === '-') {
$str .= str_replace('.', '', number_format($row_mDetails['La'], 5));
} else {
$str .= '+' . str_replace('.', '', number_format($row_mDetails['La'], 5));
}
$str .= " | " . $row_mDetails['Name'] . " | 0 |<br> ";
} while ($row_mDetails = mysql_fetch_assoc($mDetails));
could you paste it with your html or if there isn't any why are you ending the php and starting it again and again?
PS: on second row you have " php if " yet you haven't closed the earlier with ?> and you forgot "