开发者

How can I use an associative array inside an <<<_END html tag?

I'm building my own little blogging platform as a practice/fun/exercise in PHP and MySQL. I'm currently using the following code to output the proper formatting (which works perfectly):

$rows=mysql_num_rows($postsresult);
for ($j=0 ; $j < $rows ; ++$j){
    $row=mysql_fetch_row($postsresult);

    echo <<<_END
    <div class="titlebox"> $row[3] </div>
    <div class="maincontent"> $row[2]
    <div class="postclosercontainer">
    <div class="postcloser">Sincerely, <br />
    Samuel'<span>Explosion Festival</span>' Shenaniganfest </div>
    </div></div>
_END;
}

However, I found that the while($info=mysql_fetch_array($postsresult){ would be easier to code for, as the data is stored by name instead of by array number (which, with any more than a few fields, becomes aggravating to remember).

I tried to update the code with the prior while loop, but found that when I went to pull the data from the array by name, it no longer functioned properly within the <<<_END tags.

For example: <div class="titlebox"> $data['title'] generates an error.

Is there any way to accomplish this within the <<<_END tags, or should I just use the print function for each line? On a anot开发者_开发知识库her note, is this even proper coding technique? (I'm only an amateur.)


Better is to directly write HTML. This makes it easier to maintain your HTML and you might be able to use features from your IDE such as syntax highlighting or code completion.

Example:

<?php
// your other code    
?>

<?php while(($info=mysql_fetch_array($postsresult))): ?>
    <div class="titlebox"><?php echo $info['title']; ?> </div>
    <div class="maincontent"> 
         <?php echo $info['content']; ?>
         <div class="postclosercontainer">
              <div class="postcloser">Sincerely, <br />
                   Samuel'<span>Explosion Festival</span>' Shenaniganfest
              </div>
         </div>
    </div>
<?php endwhile; ?>

I'm using the alternative syntax for control structures. It increases readability when dealing with HTML, especially if you have nested control structures (brackets are much more difficult to spot when embedded in HTML).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜