[FX.php List] PHP Question
Gjermund Gusland Thorsen
ggt667 at gmail.com
Fri Jul 21 02:47:37 MDT 2006
Above HTML tag
---
$i=0;
foreach( $r['data'] as $value ) {
if( $i % 5 == 0 ) {
$linebreak = '</tr><tr>';
} else {
$linebreak = '';
}
$line[] = $linjeskift . '<td>'.$i.'</td>';
// or perhaps something like this:
// $line[] = $linjeskift . '<td>'.$value['some fieldname'][0].'</td>';
$i++;
}
This is what to put where you want the output in the HTML
---
echo '<table><tr>' . implode( "\n\t", $line ) . '</tr></table>';
On 7/21/06, Kevin Futter <kfutter at sbc.melb.catholic.edu.au> wrote:
>
> On 21/7/06 9:08 AM, "Steve Hannah" <shannah at sfu.ca> wrote:
>
>
>
> On 20-Jul-06, at 3:51 PM, Kevin Futter wrote:
>
>
> On 21/7/06 3:25 AM, "Bob Patin" <bob at patin.com> wrote:
>
>
>
> I don't know why I never thought to use ECHO rather than to move in and out
> of PHP code... !
>
> This is much more elegant; I'll definitely store this away for future
> use...
>
>
>
> That's an interesting comment Bob. I tend to go the other way – I try as
> hard as I can to separate data processing logic from both structural and
> presentational layers.
> I agree with you on this approach, as you are describing the MVC
> (Model-View-Controller) pattern. Unfortunately PHP, on its own, doesn't
> cater all that well to MVC. It works well for page layouts where you only
> have to drop a view variables into a largely HTML page:
>
> e.g. <title><?=$mytitle?></title>
> ...
>
> But as soon as you add control logic with "if" statements and loops you end
> up with a mess like:
> <? if ($condition){ ?>
> <p>Some information:</p>
> <ul>
> <? foreach ($myvars as $key=>$value){ ?>
> <li><?=$key?> is <?= $value?></li>
> <? } ?>
> </ul>
> <? } ?>
>
> You can see how it would be easy to lose one of the closing braces in the
> midst of a bunch of html code. The above example becomes:
> if ( $condition){
> echo "<p>Some information</p>
> <ul>";
> foreach ($myvars as $key=>$value){
> echo "<li>$key is $value</li>";
> }
> echo "</ul>";
> }
>
> A little bit more sightly and much easier to keep track of the opening and
> closing braces. Of course if we start putting too much html in there then
> it could become difficult to keep track of the html tags and we have the
> same problem in the other direction.
>
> The best way to do MVC in PHP is to use some sort of templating engine like
> smarty so that this is done in a way that is easy to follow and debug in
> both HTML and PHP terms
>
> e.g.
> {if $condition}
> <p>Some information:</p>
> <ul>
> {foreach from=$myvars key=key item=value}
> <li>{$key} is {$value}</li>
> {/foreach}
> </ul>
> {/if}
>
> Readable in both PHP and HTML terms.
>
> However there are further considerations when it comes to PHP. Sometimes
> you just want to display a small bit of text and this bit will be displayed
> several times - maybe as part of a loop. You have to be careful that you
> don't reload (re-include) the template page every time the snippet is
> displayed or your performance tanks. In this case I would tend to use ECHO
> statements over dropping out of PHP (e.g. ?> <p>my test</p> <? ) because
> when there is just a small amount of HTML code inside your PHP function, the
> readability of the PHP is more important than the readability of the HTML.
>
> Anyways, that is just my take on the issue after years of trying it every
> way under the sun.
>
> Best regards
>
> Steve
>
>
> I understand your point Steve, and I'm not a zealot about it. With your
> example I would indeed do it the way you suggest – it's horses for courses.
> The one point I would make however is about spotting code blocks: I've
> recently changed using from trailing braces [ if(blah){ ] to hanging braces:
>
> if (blah)
> {
> ...
> }
>
> I've noticed a monumental increase in code readability with this one change
> alone, most especially with spotting code blocks. It helps enormously to
> mitigate some of the concerns you raised about the mess of intermingled PHP
> and HTML. I can thank a colleague of mine (who is a trained programmer) for
> pointing me in this direction, and highly recommend it as a superior code
> formatting style.
>
> Kev (an old dog learning new tricks)
>
>
> --
> Kevin Futter
> Webmaster, St. Bernard's College
> http://www.sbc.melb.catholic.edu.au/
>
>
> ------------------------------------------
> This e-mail and any attachments may be confidential. You must not disclose
> or use the information in this e-mail if you are not the intended recipient.
> If you have received this e-mail in error, please notify us immediately and
> delete the e-mail and all copies. The College does not guarantee that this
> e-mail is virus or error free. The attached files are provided and may only
> be used on the basis that the user assumes all responsibility for any loss,
> damage or consequence resulting directly or indirectly from the use of the
> attached files, whether caused by the negligence of the sender or not. The
> content and opinions in this e-mail are not necessarily those of the
> College.
>
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>
>
>
More information about the FX.php_List
mailing list