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
Switch to threaded view of this topic Create a new topic Submit Reply
php+html mixup in displaying multidimensional array in html tables [message #176840] Sat, 28 January 2012 11:24 Go to next message
John is currently offline  John
Messages: 18
Registered: September 2010
Karma: 0
Junior Member
Hello,

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 ?
Re: php+html mixup in displaying multidimensional array in html tables [message #176843 is a reply to message #176840] Sat, 28 January 2012 12:03 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 28-01-2012 12:24, John wrote:
> Hello,
>
> 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.

you don't need continuous shifting .......

<?php $x=1; ?>Some <strong>html</strong> here<?php $x=$x+1 ?>

Will have the same effect as:
<?php
$x=1;
echo "Some <stong>html</strong> here";
$x=$x+1;
?>


>
> Is there a method which is 'quick an easy' to do the job ?
>

This will be a nice challenge to get to know PHP..... ;)

--
Luuk
Re: php+html mixup in displaying multidimensional array in html tables [message #176844 is a reply to message #176840] Sat, 28 January 2012 12:07 Go to previous messageGo to next message
crankypuss is currently offline  crankypuss
Messages: 147
Registered: March 2011
Karma: 0
Senior Member
On 01/28/2012 04:24 AM, John wrote:
> Hello,
>
> 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 ?

Why would you ever shift between <?php and <html>? Once you're in PHP,
just stay there and do the job. Generate the whole thing. I don't see
the issue, but maybe I don't understand.
Re: php+html mixup in displaying multidimensional array in html tables [message #176846 is a reply to message #176840] Sat, 28 January 2012 19:28 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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 a very crude solution, makes several assumptions, and has no
testing of or handling for unexpected conditions. Depending on the data
you feed it, it may crash or generate invalid markup, but under some
conditions it might instead generate a valid table.

The wonders of usenet will probably screw the formatting too. I can't do
much about that.

Rgds

Denis McMahon
Re: php+html mixup in displaying multidimensional array in html tables [message #176847 is a reply to message #176846] Sat, 28 January 2012 20:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/28/2012 2:28 PM, 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 a very crude solution, makes several assumptions, and has no
> testing of or handling for unexpected conditions. Depending on the data
> you feed it, it may crash or generate invalid markup, but under some
> conditions it might instead generate a valid table.
>
> The wonders of usenet will probably screw the formatting too. I can't do
> much about that.
>
> Rgds
>
> Denis McMahon

What is crude about it? What assumptions (other than the data does not
need to be run though htmlentities() or htmlspecialchars(), which is
quite easy to do)?

And what would crash?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176849 is a reply to message #176840] Sat, 28 January 2012 20:43 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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.

You mean `<?php' and `?>'; there can be only one `<html>' tag in a (Valid)
HTML document.

Contrary to common misconception, several `<?php's and `?>'s in a file
parsed by PHP are healthy and appropriate, as it is the most efficient
method (stdin --> stdout) and the easiest to maintain (no quoting necessary;
parsing, that includes syntax highlighting and client-side validation, just
works both for HTML and PHP).

Only make sure that you do not mix too much program logic with the output
(loops are OK). Use the template approach (not necessarily a full-blown
template system like Smarty). And, as always, indent your code so that you
can tell sections apart.

RTFM: <http://www.php.net/manual/en/tutorial.firstpage.php>

> Is there a method which is 'quick an easy' to do the job ?

Yes. You have found it.


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm> (404-comp.)
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 next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
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
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 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
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
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176852 is a reply to message #176850] Sat, 28 January 2012 23:02 Go to previous messageGo to next message
Michael Fesser is currently offline  Michael Fesser
Messages: 215
Registered: September 2010
Karma: 0
Senior Member
.oO(Thomas 'PointedEars' Lahn)

> 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>

[...]

You call _that_ maintainable and readable? IBTD.

You jump in and out from PHP and even use two different indentation
styles. That's just plain ugly and error-prone, since you don't really
see which parts belong together and where a block starts and ends.

If you don't want to use many 'echo' or 'print' statements (even though
it would make perfect sense in situations like this), use some template
mechanism.

Micha

--
http://mfesser.de/blickwinkel
Re: php+html mixup in displaying multidimensional array in html tables [message #176853 is a reply to message #176852] Sat, 28 January 2012 23:08 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Michael Fesser wrote:
> .oO(Thomas 'PointedEars' Lahn)
>
>> 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>
>
> [...]
>
> You call _that_ maintainable and readable? IBTD.
>

I assume he was demonstrating the exact opposite actually.

"This is how you would write virtually unmaintainable, error-prone, slow
code that very likely also produces invalid HTML after a few changes."
Re: php+html mixup in displaying multidimensional array in html tables [message #176855 is a reply to message #176840] Sat, 28 January 2012 23:50 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
Am 28.01.2012 12:24, schrieb John:
> Hello,
>
> 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 ?
>

Quick and easy? Easy is what you know.

I am at times still shocked to see how popular CMSs written in PHP mix up HTML and PHP.

This is generally unmaintainable, un-designable.

For quick, go with the echo style proposed.

For the heavier lifting use a template system, once you know it it will be very easy.
This will help you separate code / application logic, and design.

Template systems produce in the end scripts with a mix of PHP and HTML, but this is
not the level you work on. And you can give your templates to a designer.

I am quite happy with Smarty 3.

/Str.
Re: php+html mixup in displaying multidimensional array in html tables [message #176857 is a reply to message #176855] Sun, 29 January 2012 11:55 Go to previous messageGo to next message
r.mariotti is currently offline  r.mariotti
Messages: 17
Registered: December 2011
Karma: 0
Junior Member
After developing numerus commercial sites over the past decade+ all
the php I've created is by far mostly procedural. I personally never
leave php and output my pages with a "print <<<EOD" and the entire
page follows, variaables embededded and all.
Easy to create, read, follow, maintain, etc.

Just over a year ago I inherited a large Drupal site with over 10K
pages. Talk about dropping in/out of code... this site will do so
4,6,8 times per LINE. Its not only hard to follow the logic but even
harder to find the html one might be looking for. And to me the
killer is when they use things like this: <?php "}"?> to end a logic
block. Try and find those on a large page.

Do the drop in/out techniques work? Of course. But so does poorly
structured english written sentences. But they're almost impossible
to read.

Just my $.02 1/2 cents on this topic.
Re: php+html mixup in displaying multidimensional array in html tables [message #176858 is a reply to message #176857] Sun, 29 January 2012 14:43 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
r(dot)mariotti(at)fdcx(dot)net wrote:

> After developing numerus commercial sites over the past decade+ all
> the php I've created is by far mostly procedural. I personally never
> leave php and output my pages with a "print <<<EOD" and the entire
> page follows, variaables embededded and all.
> Easy to create, read, follow, maintain, etc.

Except when you want to indent or client-side validate your code. As for
the former, the EOD (or whatever delimiter you use) must be at the
*beginning of the line* and must at most followed by `;'. As for the
latter, a client-side HTML validator will not see the HTML that you put out
because to it it is PHP.

In addition, you still would have string escaping issues and difficulties
with function calls, and it is very inefficient to have PHP string-parse
large chunks of code that does not need parsing in the first place.

< http://php.net/manual/en/language.types.string.php#language.types.string.sy ntax.heredoc>

BTDT. Here-doc is tempting as a solution to this problem, but in the long
run it is more trouble than it is worth.


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
Re: php+html mixup in displaying multidimensional array in html tables [message #176859 is a reply to message #176858] Sun, 29 January 2012 15:44 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/29/2012 9:43 AM, Thomas 'PointedEars' Lahn wrote:
> r(dot)mariotti(at)fdcx(dot)net wrote:
>
>> After developing numerus commercial sites over the past decade+ all
>> the php I've created is by far mostly procedural. I personally never
>> leave php and output my pages with a "print<<<EOD" and the entire
>> page follows, variaables embededded and all.
>> Easy to create, read, follow, maintain, etc.
>
> Except when you want to indent or client-side validate your code. As for
> the former, the EOD (or whatever delimiter you use) must be at the
> *beginning of the line* and must at most followed by `;'. As for the
> latter, a client-side HTML validator will not see the HTML that you put out
> because to it it is PHP.
>

What does indenting have to do with it? And an HTML validator will
definitely see ALL the HTML you put out - because it is sent to the
client.

> In addition, you still would have string escaping issues and difficulties
> with function calls, and it is very inefficient to have PHP string-parse
> large chunks of code that does not need parsing in the first place.
>

What parsing is being done, other than the entire file, which the PHP
interpreter has to do anyway? All PHP is doing is outputting the
strings.


> < http://php.net/manual/en/language.types.string.php#language.types.string.sy ntax.heredoc>
>
> BTDT. Here-doc is tempting as a solution to this problem, but in the long
> run it is more trouble than it is worth.
>

That's your opinion. I've found it quite useful in many instances. It
clarifies the code, making it much easier to understand and maintain.

>
> PointedEars

But then these are typical comments from you.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176860 is a reply to message #176859] Sun, 29 January 2012 16:15 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Jerry Stuckle wrote:

> On 1/29/2012 9:43 AM, Thomas 'PointedEars' Lahn wrote:
>> r(dot)mariotti(at)fdcx(dot)net wrote:
>>> After developing numerus commercial sites over the past decade+ all
>>> the php I've created is by far mostly procedural. I personally never
>>> leave php and output my pages with a "print<<<EOD" and the entire
>>> page follows, variaables embededded and all.
>>> Easy to create, read, follow, maintain, etc.
>>
>> Except when you want to indent or client-side validate your code. As for
>> the former, the EOD (or whatever delimiter you use) must be at the
>> *beginning of the line* and must at most followed by `;'. As for the
>> latter, a client-side HTML validator will not see the HTML that you put
>> out because to it it is PHP.
>
> What does indenting have to do with it? And an HTML validator will
> definitely see ALL the HTML you put out - because it is sent to the
> client.

You can't read, can you?


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom(at)94(dot)75(dot)214(dot)39>
Re: php+html mixup in displaying multidimensional array in html tables [message #176861 is a reply to message #176860] Sun, 29 January 2012 16:45 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/29/2012 11:15 AM, Thomas 'PointedEars' Lahn wrote:
> Jerry Stuckle wrote:
>
>> On 1/29/2012 9:43 AM, Thomas 'PointedEars' Lahn wrote:
>>> r(dot)mariotti(at)fdcx(dot)net wrote:
>>>> After developing numerus commercial sites over the past decade+ all
>>>> the php I've created is by far mostly procedural. I personally never
>>>> leave php and output my pages with a "print<<<EOD" and the entire
>>>> page follows, variaables embededded and all.
>>>> Easy to create, read, follow, maintain, etc.
>>>
>>> Except when you want to indent or client-side validate your code. As for
>>> the former, the EOD (or whatever delimiter you use) must be at the
>>> *beginning of the line* and must at most followed by `;'. As for the
>>> latter, a client-side HTML validator will not see the HTML that you put
>>> out because to it it is PHP.
>>
>> What does indenting have to do with it? And an HTML validator will
>> definitely see ALL the HTML you put out - because it is sent to the
>> client.
>
> You can't read, can you?
>
>
> PointedEars

A whole lot better than you, obviously. But then you one again have
shown you have no idea what you're talking about.

But then you ignore valid arguments and just take pot shots at anyone
who points out you are wrong. You are well-known for this in multiple
newsgroups.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176862 is a reply to message #176861] Sun, 29 January 2012 17:41 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Jerry Stuckle wrote:

> On 1/29/2012 11:15 AM, Thomas 'PointedEars' Lahn wrote:
>> Jerry Stuckle wrote:
>>> On 1/29/2012 9:43 AM, Thomas 'PointedEars' Lahn wrote:
>>>> r(dot)mariotti(at)fdcx(dot)net wrote:
>>>> > After developing numerus commercial sites over the past decade+ all
>>>> > the php I've created is by far mostly procedural. I personally never
>>>> > leave php and output my pages with a "print<<<EOD" and the entire
>>>> > page follows, variaables embededded and all.
>>>> > Easy to create, read, follow, maintain, etc.
>>>>
>>>> Except when you want to indent or client-side validate your code. As
^^^^^^^^^^^^^^^^^^^^^^^ ^^
>>>> for the former, the EOD (or whatever delimiter you use) must be at the
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> *beginning of the line* and must at most followed by `;'. As for the
^^^^^^^^^^^^^^^^^^^^^^^
>>>> latter, a client-side HTML validator will not see the HTML that you put
>>>> out because to it it is PHP.
>>> What does indenting have to do with it? And an HTML validator will
>>> definitely see ALL the HTML you put out - because it is sent to the
>>> client.
>> You can't read, can you?
>
> A whole lot better than you, obviously. But then you one again have
> shown you have no idea what you're talking about.

No, evidently you can't read. For if you could, you would have noticed that
indenting heredoc inevitably shifts the delimiter right from the first
column, which creates a *syntax error*; so with heredoc you have to take
extra care that you do not indent that line, which makes maintenance harder.
You also have to take extra care then that the character following the
delimiter is at most one `;'; for example, concatenation is only possible
with the dot at the *next* line. All of that jumping through hoops for
gaining program *inefficiency* is unnecessary if one uses the natural,
efficient way of PHP coding with multiple PHP sections in the first place
(with the alternative syntax if you prefer that).

And if you could also think clearly, you would have interpreted "client-side
validate" as a validation *before* the PHP source code is deployed on the
production server, in the source code editor, and not misconstrued it as
validation in the browser (which is not even possible in all browsers).

Nuff said.


PointedEars
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom(at)94(dot)75(dot)214(dot)39>
Re: php+html mixup in displaying multidimensional array in html tables [message #176863 is a reply to message #176862] Sun, 29 January 2012 18:49 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/29/2012 12:41 PM, Thomas 'PointedEars' Lahn wrote:
> Jerry Stuckle wrote:
>
>> On 1/29/2012 11:15 AM, Thomas 'PointedEars' Lahn wrote:
>>> Jerry Stuckle wrote:
>>>> On 1/29/2012 9:43 AM, Thomas 'PointedEars' Lahn wrote:
>>>> > r(dot)mariotti(at)fdcx(dot)net wrote:
>>>> >> After developing numerus commercial sites over the past decade+ all
>>>> >> the php I've created is by far mostly procedural. I personally never
>>>> >> leave php and output my pages with a "print<<<EOD" and the entire
>>>> >> page follows, variaables embededded and all.
>>>> >> Easy to create, read, follow, maintain, etc.
>>>> >
>>>> > Except when you want to indent or client-side validate your code. As
> ^^^^^^^^^^^^^^^^^^^^^^^ ^^
>>>> > for the former, the EOD (or whatever delimiter you use) must be at the
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> > *beginning of the line* and must at most followed by `;'. As for the
> ^^^^^^^^^^^^^^^^^^^^^^^
>>>> > latter, a client-side HTML validator will not see the HTML that you put
>>>> > out because to it it is PHP.
>>>> What does indenting have to do with it? And an HTML validator will
>>>> definitely see ALL the HTML you put out - because it is sent to the
>>>> client.
>>> You can't read, can you?
>>
>> A whole lot better than you, obviously. But then you one again have
>> shown you have no idea what you're talking about.
>
> No, evidently you can't read. For if you could, you would have noticed that
> indenting heredoc inevitably shifts the delimiter right from the first
> column, which creates a *syntax error*; so with heredoc you have to take
> extra care that you do not indent that line, which makes maintenance harder.
> You also have to take extra care then that the character following the
> delimiter is at most one `;'; for example, concatenation is only possible
> with the dot at the *next* line. All of that jumping through hoops for
> gaining program *inefficiency* is unnecessary if one uses the natural,
> efficient way of PHP coding with multiple PHP sections in the first place
> (with the alternative syntax if you prefer that).
>

Sure, and you have to have the test for an 'if' statement enclosed in
parentheses, and variable names must start with a '$' - and all kinds of
things. That's the syntax. But it is not less efficient - in fact, it
is much MORE efficient than your way of going in and out of PHP. The
text in the HEREDOC is parsed ONCE - when the entire script is parsed
(which must be done anyway). But you're in PHP and you stay in PHP.
Multiple sections are not "natural" nor efficient.

> And if you could also think clearly, you would have interpreted "client-side
> validate" as a validation *before* the PHP source code is deployed on the
> production server, in the source code editor, and not misconstrued it as
> validation in the browser (which is not even possible in all browsers).
>

You can't client-side validate before the PHP source code is deployed on
the server. Validating in a context-sensitive editor is much different
- but then you obviously don't understand that difference, either.

> Nuff said.
>
>
> PointedEars

I would suggest that is a good idea. You just continue show your
stoopidity and complete lack of understanding of basic PHP programming.

But you continue to do that in other newsgroups, also. So that's
nothing new.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176865 is a reply to message #176847] Sun, 29 January 2012 19:58 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sat, 28 Jan 2012 15:14:51 -0500, Jerry Stuckle wrote:

> On 1/28/2012 2:28 PM, 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 a very crude solution, makes several assumptions, and has no
>> testing of or handling for unexpected conditions. Depending on the data
>> you feed it, it may crash or generate invalid markup, but under some
>> conditions it might instead generate a valid table.
>>
>> The wonders of usenet will probably screw the formatting too. I can't
>> do much about that.

> What is crude about it? What assumptions (other than the data does not
> need to be run though htmlentities() or htmlspecialchars(), which is
> quite easy to do)?

1) If the sub arrays don't all contain the same number of elements, you
could get a table with different numbers of cells in different rows.
Ideally you ought to catch that somehow. One of the principles I was
taught was about being liberal in what you accept as input and specific
in what you generate as output.

2) What if an array member is a binary object or a class?

Ultimately my solution is probably only suited to 2d arrays containing
the same number of elements in each sub-array, and where each element in
a sub-array is either an html safe string or a numeric value.

> And what would crash?

I have no idea what it will do if you feed it a binary object or a class
as a sub-array element, but I suspect it may crash. Whatever it does do,
from the point of view that the desired behaviour is that of "generating
meaningful html", I doubt it would work as desired if eg

$arr[3][2] = imagecreatetruecolor(640,480);
$arr[1][3] = fopen("/path/file.type","r");

etc etc

Rgds

Denis McMahon
Re: php+html mixup in displaying multidimensional array in html tables [message #176866 is a reply to message #176865] Sun, 29 January 2012 20:09 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/29/2012 2:58 PM, Denis McMahon wrote:
> On Sat, 28 Jan 2012 15:14:51 -0500, Jerry Stuckle wrote:
>
>> On 1/28/2012 2:28 PM, 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 a very crude solution, makes several assumptions, and has no
>>> testing of or handling for unexpected conditions. Depending on the data
>>> you feed it, it may crash or generate invalid markup, but under some
>>> conditions it might instead generate a valid table.
>>>
>>> The wonders of usenet will probably screw the formatting too. I can't
>>> do much about that.
>
>> What is crude about it? What assumptions (other than the data does not
>> need to be run though htmlentities() or htmlspecialchars(), which is
>> quite easy to do)?
>
> 1) If the sub arrays don't all contain the same number of elements, you
> could get a table with different numbers of cells in different rows.
> Ideally you ought to catch that somehow. One of the principles I was
> taught was about being liberal in what you accept as input and specific
> in what you generate as output.
>

This is from a MySQL query - multiple rows, all with the same number of
columns.

> 2) What if an array member is a binary object or a class?
>

See above.

> Ultimately my solution is probably only suited to 2d arrays containing
> the same number of elements in each sub-array, and where each element in
> a sub-array is either an html safe string or a numeric value.
>

See above.

>> And what would crash?
>
> I have no idea what it will do if you feed it a binary object or a class
> as a sub-array element, but I suspect it may crash. Whatever it does do,
> from the point of view that the desired behaviour is that of "generating
> meaningful html", I doubt it would work as desired if eg
>

See above.

> $arr[3][2] = imagecreatetruecolor(640,480);
> $arr[1][3] = fopen("/path/file.type","r");
>
> etc etc
>
> Rgds
>
> Denis McMahon

Read the entire post. None of your arguments are valid.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176867 is a reply to message #176866] Sun, 29 January 2012 22:10 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 29 Jan 2012 15:09:41 -0500, Jerry Stuckle wrote:

> This is from a MySQL query - multiple rows, all with the same number of
> columns.

I must have missed where the OP said that.

>> 2) What if an array member is a binary object or a class?

> See above.

> Read the entire post. None of your arguments are valid.

I checked the OP's post - it says nothing about the array being populated
from a MySql query.

In addition, even if it was populated from a MySql query, I don't know if
any of the columns in the table(s) concerned are BLOBs.

I did wonder if the "John Drako <jbravo556(at)gmail(dot)removethis(dot)com>" in the
"Fast/Easy way to extract a column from multi-dimensional array?" thread
was also the John "<John(at)agdp(dot)de>" in this thread, but wasn't actually
assuming that he was.

Rgds

Denis McMahon
Re: php+html mixup in displaying multidimensional array in html tables [message #176868 is a reply to message #176867] Sun, 29 January 2012 22:41 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 1/29/2012 5:10 PM, Denis McMahon wrote:
> On Sun, 29 Jan 2012 15:09:41 -0500, Jerry Stuckle wrote:
>
>> This is from a MySQL query - multiple rows, all with the same number of
>> columns.
>
> I must have missed where the OP said that.
>
>>> 2) What if an array member is a binary object or a class?
>
>> See above.
>
>> Read the entire post. None of your arguments are valid.
>
> I checked the OP's post - it says nothing about the array being populated
> from a MySql query.
>
> In addition, even if it was populated from a MySql query, I don't know if
> any of the columns in the table(s) concerned are BLOBs.
>
> I did wonder if the "John Drako<jbravo556(at)gmail(dot)removethis(dot)com>" in the
> "Fast/Easy way to extract a column from multi-dimensional array?" thread
> was also the John "<John(at)agdp(dot)de>" in this thread, but wasn't actually
> assuming that he was.
>
> Rgds
>
> Denis McMahon

If any of the data were BLOBs, he would know it. He's also responsible
for putting data into the database. Completely immaterial in this case.

As for the array - he should also know the contents of the array and the
number of elements in each array.

Your comments are only valid if he had nothing to do with building the
array or its contents. Which he also didn't say.

The time to validate stuff is when it's being put into the array, and
validating the array as it is built. Then you don't have to worry about
complicated and hard-to-maintain code like you are suggesting.

The bottom line is - start with good data and coding is easy. Build
crap data and the code becomes much harder to understand and maintain
(needless to say adding a lot more bugs to the scripts).



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: php+html mixup in displaying multidimensional array in html tables [message #176870 is a reply to message #176858] Mon, 30 January 2012 13:51 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 1/29/2012 6:43 AM, Thomas 'PointedEars' Lahn wrote:
> r(dot)mariotti(at)fdcx(dot)net wrote:
>
>> After developing numerus commercial sites over the past decade+ all
>> the php I've created is by far mostly procedural. I personally never
>> leave php and output my pages with a "print<<<EOD" and the entire
>> page follows, variaables embededded and all.
>> Easy to create, read, follow, maintain, etc.
>
> Except when you want to indent or client-side validate your code. As for
> the former, the EOD (or whatever delimiter you use) must be at the
> *beginning of the line* and must at most followed by `;'. As for the
> latter, a client-side HTML validator will not see the HTML that you put out
> because to it it is PHP.
>

I use heredocs for all my forms (particularly admin) and have no issues
with client side functions on the form even ajax work flawlessly.
The browser has no idea how the info sent to it was created, only that
it is sent in proper context.

> In addition, you still would have string escaping issues and difficulties
> with function calls, and it is very inefficient to have PHP string-parse
> large chunks of code that does not need parsing in the first place.
>

I actually find using strings in heredocs is very easy. Instead of
having to escape with my typical " . $var . " now all I need is {$var}
which seems to me as more of an easier and 'template' type of design to
read.

There are times when I need to call a method inside of the heredoc in
which case I process that var in a call above the heredoc definition.

> < http://php.net/manual/en/language.types.string.php#language.types.string.sy ntax.heredoc>
>
> BTDT. Here-doc is tempting as a solution to this problem, but in the long
> run it is more trouble than it is worth.
>

I have the opposite opinion once I started to use it more often.
It is simple and easy to read part of the coding for me.

>
> PointedEars
Re: php+html mixup in displaying multidimensional array in html tables [message #176871 is a reply to message #176862] Mon, 30 January 2012 14:04 Go to previous messageGo to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 1/29/2012 9:41 AM, Thomas 'PointedEars' Lahn wrote:
> Jerry Stuckle wrote:
>
>> On 1/29/2012 11:15 AM, Thomas 'PointedEars' Lahn wrote:
>>> Jerry Stuckle wrote:
>>>> On 1/29/2012 9:43 AM, Thomas 'PointedEars' Lahn wrote:
>>>> > r(dot)mariotti(at)fdcx(dot)net wrote:
>>>> >> After developing numerus commercial sites over the past decade+ all
>>>> >> the php I've created is by far mostly procedural. I personally never
>>>> >> leave php and output my pages with a "print<<<EOD" and the entire
>>>> >> page follows, variaables embededded and all.
>>>> >> Easy to create, read, follow, maintain, etc.
>>>> >
>>>> > Except when you want to indent or client-side validate your code. As
> ^^^^^^^^^^^^^^^^^^^^^^^ ^^
>>>> > for the former, the EOD (or whatever delimiter you use) must be at the
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> > *beginning of the line* and must at most followed by `;'. As for the
> ^^^^^^^^^^^^^^^^^^^^^^^
>>>> > latter, a client-side HTML validator will not see the HTML that you put
>>>> > out because to it it is PHP.
>>>> What does indenting have to do with it? And an HTML validator will
>>>> definitely see ALL the HTML you put out - because it is sent to the
>>>> client.
>>> You can't read, can you?
>>
>> A whole lot better than you, obviously. But then you one again have
>> shown you have no idea what you're talking about.
>
> No, evidently you can't read. For if you could, you would have noticed that
> indenting heredoc inevitably shifts the delimiter right from the first
> column, which creates a *syntax error*; so with heredoc you have to take
> extra care that you do not indent that line, which makes maintenance harder.
> You also have to take extra care then that the character following the
> delimiter is at most one `;'; for example, concatenation is only possible
> with the dot at the *next* line. All of that jumping through hoops for
> gaining program *inefficiency* is unnecessary if one uses the natural,
> efficient way of PHP coding with multiple PHP sections in the first place
> (with the alternative syntax if you prefer that).
>
> And if you could also think clearly, you would have interpreted "client-side
> validate" as a validation *before* the PHP source code is deployed on the
> production server, in the source code editor, and not misconstrued it as
> validation in the browser (which is not even possible in all browsers).
>

OK maybe I am not thinking clearly here but trying to wrap my head
around what you mean by "validation before the php source code is
deployed on the production server". The only thing I can guess is that
you are meaning using you editor to validate your code structure?

Please enlighten.

> Nuff said.

Nina Simone? Is that you?

>
>
> PointedEars
Re: php+html mixup in displaying multidimensional array in html tables [message #176872 is a reply to message #176871] Mon, 30 January 2012 15:03 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Scott Johnson wrote:

> […] trying to wrap my head around what you mean by "validation before the
> php source code is deployed on the production server". The only thing I
> can guess is that you are meaning using you editor to validate your code
> structure?

That is correct. For example, Eclipse PHP Development Tools (PDT) [1] allow
you to perform validation both on the PHP code *and* the content that is
being generated with PHP if the latter is generated outside of PHP sections.
It is also properly highlighted then, and the structure is displayed in the
Outline View (where you can switch between the PHP parse tree and the
markup/whatever parse tree).

This is very helpful, especially if you need to generate content with a
complex structure (e. g. in templates). It appears to work with any
generated content, as long as the filename suffix is .php or you have added
the Content-Type to the list of PHP types and opened the file with the PHP
Editor. (This also works with temporary files downloaded from the
production server for a quick fix to be backported later.)

Apparently that client-side validation is not as common a technique among
PHP developers as I thought. However, this would account for the amount of
markup ranging from being slightly invalid to completely borken, working
only by chance because of browser's error correction, that is generated by
many PHP-driven Web applications.


PointedEars
___________
[1] <http://www.eclipse.org/projects/project.php?id=tools.pdt>
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
Re: php+html mixup in displaying multidimensional array in html tables [message #176873 is a reply to message #176870] Mon, 30 January 2012 15:27 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Scott Johnson wrote:

> On 1/29/2012 6:43 AM, Thomas 'PointedEars' Lahn wrote:
>> r(dot)mariotti(at)fdcx(dot)net wrote:
>>> After developing numerus commercial sites over the past decade+ all
>>> the php I've created is by far mostly procedural. I personally never
>>> leave php and output my pages with a "print<<<EOD" and the entire
>>> page follows, variaables embededded and all.
>>> Easy to create, read, follow, maintain, etc.
>>
>> Except when you want to indent or client-side validate your code. As for
>> the former, the EOD (or whatever delimiter you use) must be at the
>> *beginning of the line* and must at most followed by `;'. As for the
>> latter, a client-side HTML validator will not see the HTML that you put
>> out because to it it is PHP.
>
> I use heredocs for all my forms (particularly admin) and have no issues
> with client side functions on the form even ajax work flawlessly.

I did not mean that.

> The browser has no idea how the info sent to it was created, only that
> it is sent in proper context.

Of course.

>> In addition, you still would have string escaping issues and difficulties
>> with function calls, and it is very inefficient to have PHP string-parse
>> large chunks of code that does not need parsing in the first place.
>
> I actually find using strings in heredocs is very easy. Instead of
> having to escape with my typical " . $var . " now all I need is {$var}
> which seems to me as more of an easier and 'template' type of design to
> read.
>
> There are times when I need to call a method inside of the heredoc in
> which case I process that var in a call above the heredoc definition.

ACK. The drawback of that approach is that you have a variable definition
that is detached from the context in which the variable is used; the larger
the heredoc, the more detached. This counts as "jumping through hoops" in
my book, as in "lacking flexibility".

>>
< http://php.net/manual/en/language.types.string.php#language.types.string.sy ntax.heredoc>
>>
>> BTDT. Here-doc is tempting as a solution to this problem, but in the
>> long run it is more trouble than it is worth.
>
> I have the opposite opinion once I started to use it more often.
> It is simple and easy to read part of the coding for me.

For example, I have a CSS that is being generated by PHP because I am using
PHP variables in it to ease maintenance. I would not think of using one
large `echo' statement with heredoc to generate that stylesheet instead.
Instead, I have several few *small* PHP sections in it which define the
variable value and print it (with `echo'):

<http://PointedEars.de/media/video/series/style.css.phps>

[symlinked for you; the original has still suffix .css because of

$ cat .htaccess
<IfModule mod_php5.c>
<FilesMatch "\.(ph(p3?|tml)|css)$">
SetHandler application/x-httpd-php
</FilesMatch>
</IfModule>

]

So I let PHP only do what PHP must do, and can keep my variable definitions
close to where I use them (but I do not have to). If I open the foo.css
file with the CSS Editor, I can still use the Outline View to jump to the
ruleset that concerns me and make the necessary modifications really fast,
without having to think about escaping the whole stylesheet and related
problems.

This approach is very flexible and very efficient in several ways.


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
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Sun Nov 10 07:08:57 GMT 2024

Total time taken to generate the page: 0.03083 seconds