I have been googling like crazy to find an answer to what I thought would have been a very common question to no avail...
I am using " " to break a table into a more readable structure when viewing source. Is there any way to get it to respect current indentation levels? For example:
Turn this:
<table>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
Into this:
<table>
<tbody>
<tr>
<td>Data</td>
<td>Data</td>
<td>Data</td>
<td>Data</td>
</tr>
</tbody>
</table>
I am printing the tbody
rows and cells out with the following code:
<?php
$first = true;
foreach ($data->rows as $row) {
if ($first) {
print "<tr class='highlight'>";
$first = false;
} else {
print "<tr>";
}
foreach ($row as $cell) {
print "<td>";
printf('%s', $cell);
print "</td>
";
}
print "</tr>
";
}
?>
<?php
$first = true;
foreach ($data->rows as $row) {
if ($first) {
echo "\t\t<tr class='highlight'>";
$first = false;
} else {
print "\t\t<tr>";
}
foreach ($row as $cell) {
print "\t\t\t<td>";
printf("\t\t\t\t%s", $cell);
print "</td>
";
}
print "\t\t</tr>
";
}
?>
I recommend you to write an identation counter, if you would like to do it dynamically. And remember to use double quotes for escape sequences, single quotes won´t work here (because it´s treated like a variable).
This is what google gave me.
You should concatinate the entire table in one php string, parse it through the function that can be found here:
http://www.daveperrett.com/articles/2007/04/05/format-xml-with-php/
and then simply echo $xml;
I just found it, I give no guarantee that this works as itended but it looks reliable.
The final HTML source have not to respect an indentation... Nobody see it (or you but don't get crazy with this).
Ok, sometimes this is also an obsession for me (one among many others ?), so you can do:
echo '
<tr>';
//Loop starts
echo '
<td></td>';
// Loop ends
echo '
</tr>';
Here you get a newline and your HTML indentation is independant from the PHP one. Is it what you would ?