Div标签不会在php中关闭

时间:2013-08-30 18:44:52

标签: php function html echo

简单一个!我正在尝试编写一个廉价的论坛。来自C背景,我开始注意到PHP的一些奇怪之处。虽然有一个函数将DIV中的字符串(HTML)返回到位,但浏览器不会打印</DIV> - 即使它自己被回显。

PHP是否决定何时回显某些DOM元素或对HTML输出有限制?

    echo "Start<div id='Forum'>";
    echo "Forum";   
    GetFullList();
    echo "</div>";

其中,GetFullList()包含:

function GetFullList(){ 
        $sql="SELECT * FROM `Forum` WHERE `IsReply` =0";
        $result=mysql_query($sql);
        if (!$result){
            echo mysql_error();
        }
        if($result){
        while($ForumEntry = mysql_fetch_assoc($result)){
            $IsReply        =   $ForumEntry["IsReply"];
            $ParentPost =   $ForumEntry["ParentTopic"];
            $f_User     =   $ForumEntry["User"];
            $f_Replies      =   $ForumEntry["Replies"];
            $f_Views        =   $ForumEntry["Views"];
            $f_Time     =   $ForumEntry["Time"];
            $f_Post     =   $ForumEntry["Post"];
            $f_Topic        =   $ForumEntry["Topic"];
            $f_Index        =   $ForumEntry["Index"];
            echo DisplayPost($f_User, $f_Replies, $f_Views, $f_Time, $f_Post, $f_Topic, $f_Index);
            GetChildPostsOf($ParentPost);
                }
            }
        }

DisplayPost()构建如下:

 function DisplayPost($f_User, $f_Replies, $f_Views, $f_Time, $f_Post, $f_Topic, $f_Index){           
            $PostBlock = "<div id='Grp_Cell' style='width:930;background-color:#999999;text-align:left;'><div id='Grp_Cell' style='float:left;'><div id='Tbl_Cel'>User: ".$f_User."</div><div id='Tbl_Cel'>Replies: ". $f_Replies."</div><div id='Tbl_Cel'>Views: ".$f_Views."</div><div id='Tbl_Cel'style='background-color:777777;height:112;'>Post started on ".$f_Time.".&nbsp;</div></div><div id='Grp_Cell' style='float:right;width:600;'><div id='Tbl_Cel'>Subject: ".$f_Topic."</div><div id='Tbl_Cel' style='background-color:777777;height:150;'>". $f_Post."</div><a onClick='Reply(".$f_Index.");Filter();'><div id='Tbl_Cel' style='background-color:#888888; height:50; width:50; float:right; padding:2;border-color:black; border:2;'><br>Reply</div></a></div>";
            return $PostBlock;
          }

(显示DB结果的div脚手架:帖子。) 当我尝试回应“&lt; / div&gt;”时在GetFullList()之后,结果不会以HTML格式打印,而是将页面的其余部分包含在格式错误的div下。

1 个答案:

答案 0 :(得分:1)

$ PostBlock中有10个开场div和9个结束div。必要时应添加结束</div>。查看输出结果的简单方法是将其分解为以下行:

$PostBlock = "
<div id='Grp_Cell' style='width:930;background-color:#999999;text-align:left;'>         
    <div id='Grp_Cell' style='float:left;'>
        <div id='Tbl_Cel'>User: ".$f_User."</div>
        <div id='Tbl_Cel'>Replies: ". $f_Replies."</div>
        <div id='Tbl_Cel'>Views: ".$f_Views."</div>
        <div id='Tbl_Cel'style='background-color:777777;height:112;'>Post started on ".$f_Time.".&nbsp;</div>
    </div>
    <div id='Grp_Cell' style='float:right;width:600;'>
        <div id='Tbl_Cel'>Subject: ".$f_Topic."</div>
        <div id='Tbl_Cel' style='background-color:777777;height:150;'>". $f_Post."</div>
        <a onClick='Reply(".$f_Index.");Filter();'><div id='Tbl_Cel' style='background-color:#888888; height:50; width:50; float:right; padding:2;border-color:black; border:2;'><br>Reply</div></a>
    </div>  ";