html - Encapsulate PHP repeating section/array to a string -
i have repeating section mysql look-up - code (including html) this:
<br> <?php { ?> <?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 output of string (for example, $mydatastring = *the output above*)
whenever try this, error:
parse error: syntax error, unexpected t_encapsed_and_whitespace, expecting t_string or t_variable or t_num_string (edited include surrounding html)
you need remove php start of second line. code runs fine. advocate making code more readable:
<?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)); ?> i removed unnecessary ; after elses.
it's question of personal preference replace your
?> | <?php with
echo " | "; to avoid opening , closing tags.
edit
if want build string in loop, 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));
Comments
Post a Comment