[FX.php List] Embed php into HTML or vice versa?

Kevin Futter kfutter at sbc.melb.catholic.edu.au
Thu Aug 31 19:45:47 MDT 2006


On 1/9/06 11:08 AM, "Jonathan Schwartz" <jonathan at eschwartz.com> wrote:

> Hmmm... so how would you do that for the foreach statement?...
> 
> 
> <?php foreach($searchResult['data'] as $key => $value)
> {
> echo "<tr class=\"ver12\"><td><ahref=\"famdetail.php?recid=";
> $recordDetails=explode('.',$key);
> $currentRecord=$recordDetails[0];
> echo $currentRecord."\">".$value['FamilyName'][0];
> echo "</a></td>";
> }
> ?>
> 
> Jonathan

One option would be like so:

<?php
foreach($searchResult['data'] as $key => $value)
{
    $recordDetails=explode('.',$key);
    $currentRecord=$recordDetails[0];
?>
<tr class="ver12"><td><a href="famdetail.php?recid=<?php echo $currentRecord
. "\">" . $value['FamilyName'][0]; ?></a></td>
<?php
}
?>

That's not necessarily the only way to do it - remember, I'm not advocating
absolutes here. Being true to my approach, I would ordinarily drop out of
PHP to close the opening <a> tag, but I've taken a pragmatic approach here
in printing it out with PHP as part of the existing echo statement. I find
the clear delineation between HTML (or whatever markup language we're
talking about) and PHP much easier to read and debug. As Steve pointed out
earlier, it's also closer to the MVC model that most web apps try to pursue.

The only other tip I'd give you is that when you do concat HTML and PHP, put
spaces before and after your concat character (.); they're too easily lost
or mistaken for full stops (periods) otherwise.


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




More information about the FX.php_List mailing list