Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4663

Re: php+html mixup in displaying multidimensional array in html tables

Message-ID <5865552.bgypaU67uL@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-01-28 22:01 +0100
Subject Re: php+html mixup in displaying multidimensional array in html tables
Newsgroups comp.lang.php
References <jg0lpp$29b$1@nnrp.linuxfan.it> <4f244c53$0$5013$a8266bb1@newsreader.readnews.com>
Followup-To comp.lang.php

Followups directed to: comp.lang.php

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

php+html mixup in displaying multidimensional array in html tables John <John@agdp.de> - 2012-01-28 12:24 +0100
  Re: php+html mixup in displaying multidimensional array in html tables Luuk <Luuk@invalid.lan> - 2012-01-28 13:03 +0100
  Re: php+html mixup in displaying multidimensional array in html tables crankypuss <no@email.thanks> - 2012-01-28 05:07 -0700
  Re: php+html mixup in displaying multidimensional array in html tables Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-28 19:28 +0000
    Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-28 15:14 -0500
      Re: php+html mixup in displaying multidimensional array in html tables Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-29 19:58 +0000
        Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-29 15:09 -0500
          Re: php+html mixup in displaying multidimensional array in html tables Denis McMahon <denismfmcmahon@gmail.com> - 2012-01-29 22:10 +0000
            Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-29 17:41 -0500
    Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-28 22:01 +0100
      Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-28 17:37 -0500
      Re: php+html mixup in displaying multidimensional array in html tables Michael Fesser <netizen@gmx.de> - 2012-01-29 00:02 +0100
        Re: php+html mixup in displaying multidimensional array in html tables The Natural Philosopher <tnp@invalid.invalid> - 2012-01-28 23:08 +0000
  Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-28 21:43 +0100
  Re: php+html mixup in displaying multidimensional array in html tables "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-01-29 00:50 +0100
    Re: php+html mixup in displaying multidimensional array in html tables r.mariotti@fdcx.net - 2012-01-29 06:55 -0500
      Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-29 15:43 +0100
        Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-29 10:44 -0500
          Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-29 17:15 +0100
            Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-29 11:45 -0500
              Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-29 18:41 +0100
                Re: php+html mixup in displaying multidimensional array in html tables Jerry Stuckle <jstucklex@attglobal.net> - 2012-01-29 13:49 -0500
                Re: php+html mixup in displaying multidimensional array in html tables Scott Johnson <noonehome@chalupasworld.com> - 2012-01-30 06:04 -0800
                Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-30 16:03 +0100
        Re: php+html mixup in displaying multidimensional array in html tables Scott Johnson <noonehome@chalupasworld.com> - 2012-01-30 05:51 -0800
          Re: php+html mixup in displaying multidimensional array in html tables Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-01-30 16:27 +0100

csiph-web