我正在构建自己的小型博客平台,作为 PHP 和 MySQL 的练习/乐趣/练习。我目前正在使用以下代码来输出正确的格式(效果完美):
$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){
会更容易编码,因为数据是按名称存储的,而不是按数组编号存储的(如果字段数量超过几个,那么记住起来会很麻烦),
我尝试用之前的 while 循环更新代码,但发现当我去拉取数据时。按名称从数组获取数据,它不再在
$data['title']
中正常运行。有没有
办法在 <<_END 标签中完成此操作,或者我应该对每一行使用 print 函数?在另一个注释中,这是否是正确的编码技术?只是业余爱好者。)
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 another note, is this even proper coding technique? (I'm only an amateur.)
发布评论
评论(1)
更好的是直接写HTML。这使得维护 HTML 变得更加容易,并且您可以使用 IDE 中的功能,例如语法突出显示或代码完成。
示例:
我正在使用控制结构的替代语法。它提高了处理 HTML 时的可读性,特别是如果您有嵌套的控制结构(嵌入在 HTML 中时括号更难以识别)。
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:
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).