FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » php+html mixup in displaying multidimensional array in html tables
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: php+html mixup in displaying multidimensional array in html tables [message #176850 is a reply to message #176846] Sat, 28 January 2012 21:01 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Fast/Easy way to extract a column from multi-dimensional array?
Next Topic: Freelance PHP Developer
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 23 07:02:33 GMT 2024

Total time taken to generate the page: 0.05031 seconds