2011-02-04 89 views
0

我正在构建我自己的小博客平台,作为PHP和MySQL中的练习/趣味/练习。我目前使用下面的代码输出正确的格式(这完美的作品):如何在<<< _ END HTML标签中使用关联数组?

$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; 
} 

然而,我发现,while($info=mysql_fetch_array($postsresult){会更容易对代码,数据是通过名称而不是存储数组编号(其中,任何超过几个字段,变得加重记住)。

我试图用before while循环更新代码,但发现当我按名称从数组中拉出数据时,它不再在< < < _END标记中正常运行。

例如:<div class="titlebox"> $data['title']生成错误。

有没有什么办法可以在< < < _END标签中完成这项工作,还是应该为每一行使用打印功能?另一方面,这是否是合适的编码技术? (我只是一个业余爱好者。)

回答

2

更好的是直接编写HTML。这样可以更容易地维护您的HTML,并且您可以使用IDE中的功能,如语法突出显示或代码完成。

实施例:

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

我使用的alternative syntax for control structures。它在处理HTML时增加了可读性,尤其是如果您有嵌套的控制结构(嵌入HTML时更难以发现括号)。

+0

+1对于HTML,不通过PHP回显。 – alex 2011-02-04 03:59:24