Re: php+html mixup in displaying multidimensional array in html tables [message #176851 is a reply to message #176850] |
Sat, 28 January 2012 22:37 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 1/28/2012 4:01 PM, Thomas 'PointedEars' Lahn wrote:
> Denis McMahon wrote:
>
>> On Sat, 28 Jan 2012 12:24:14 +0100, John wrote:
>>> I am new to this problem and shall be very grateful for any hint on how
>>> to a´void an unreadable code-salad, when trying to display a
>>> bidimensional array (thus with two indexes) on a html table. I refer to
>>> the continuous shifting between<?php and<html> every two lines.
>>>
>>> Is there a method which is 'quick an easy' to do the job ?
>>
>> You mean something like:
>>
>> echo "<table>\n";
>> foreach ($arr as $line) {
>> echo "<tr>\n";
>> foreach ($line as $cell) {
>> echo "<td>$cell</td>\n";
>> }
>> echo "</tr>\n";
>> }
>> echo "</table>\n";
>
> This is how you would write virtually unmaintainable, error-prone, slow code
> that very likely also produces invalid HTML after a few changes.
>
> Please don't.
>
> <table>
> <?php
> foreach ($arr as $line)
> {
> ?>
> <tr>
> <?php
> foreach ($line as $cell)
> {
> ?>
> <td><?php echo htmlspecialchars($cell); ?></td>
> <?php
> }
> ?>
> </tr>
> <?php
> }
> ?>
> </table>
>
> is just fine (see how the braces and tags align at different columns?).
> There is also a more verbose alternative syntax, which you could write as
> follows:
>
> <table>
> <?php foreach ($arr as $line): ?>
> <tr>
> <?php foreach ($line as $cell): ?>
> <td><?php echo htmlspecialchars($cell); ?></td>
> <?php endforeach; ?>
> </tr>
> <?php endforeach; ?>
> </table>
>
> But currently that does not work well with Eclipse PDT:
>
> <https://bugs.eclipse.org/bugs/show_bug.cgi?id=359473>
>
>
> PointedEars
This is a perfect example of the original op's question. No way would I
keep going in and out of PHP like that. Other than the missing call to
htmlspecialchars(), I find Denis's code much more readable and maintainable.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|