从类外部但在html标记内部回显php变量
我的问题是: 请有人给我一个提示,因为我不想回显PHP中的html,但PHP中的html从类外部但在html标记内部回显php变量,php,jquery,html,Php,Jquery,Html,我的问题是: 请有人给我一个提示,因为我不想回显PHP中的html,但PHP中的html <div class='comment-text'> <?php echo $commento; ?> </div> class Posts{ public function __construct(){ $this->connP = new
<div class='comment-text'>
<?php
echo $commento;
?>
</div>
class Posts{
public function __construct(){
$this->connP = new connection2();
$this->connP = $this->connP->dbconnection();
}
public function DETAIL_p(){
$sql_cmmnt = "SELECT * FROM tbl_main_comments WHERE POST_ID = ?";
$sql_cmmnt_statement =$this->connP->prepare($sql_cmmnt);
$sql_cmmnt_statement->bindParam(1,$posta);
$sql_cmmnt_statement->execute();
while($row2=$sql_cmmnt_statement->fetch()){
$commento = $row2['comments'];
$name_f = $row2['fname'];
$name_s = $row2['sname'];
$commentID = $row2['comment_id'];
$date_comment_posted = $row2['date_time_commented'];
$time = date("d M Y", strtotime($date_comment_posted));
}
}
}else{echo("error");}
//END OF SQL
}else{
echo("oops! something went wrong try again later");
}
}
}
$objectP=new Posts();
$objectP->DETAIL_p();
班级职务{
公共函数构造(){
$this->connP=newconnection2();
$this->connP=$this->connP->dbconnection();
}
公共功能详细信息{
$sql\u cmmnt=“从发布ID=?”的tbl\u主注释中选择*;
$sql\u cmmnt\u语句=$this->connP->prepare($sql\u cmmnt);
$sql\u cmmnt\u语句->bindParam(1$posta);
$sql\u cmmnt\u语句->执行();
而($row2=$sql\u cmmnt\u语句->fetch()){
$commento=$row2['comments'];
$name_f=$row2['fname'];
$name_s=$row2['sname'];
$commentID=$row2['comment_id'];
$date_comment_posted=$row2['date_time_comment'];
$time=date(“d M Y”,strottime($date_comment_posted));
}
}
}else{echo(“error”);}
//SQL结束
}否则{
echo(“哎呀!出了问题,请稍后再试”);
}
}
}
$objectP=新职位();
$objectP->DETAIL_p();
您应该在Posts类中存储$commento变量。然后创建一个函数来返回它,并调用该函数
class Posts {
private $commento;
public function getComment (){ echo $commento }
public function DETAIL_p() {
//other function code
}
之后,您可以在HTML中调用它
<?php
$object->getComment();
?>
班级帖子{
是一个彻底的错误;在这里打开php标记。是的,我的代码安排不好,我只是这样做来解释我的问题,我如何从html内部和类外部回显类变量。或者更好的方法是从类外部调用类函数,返回需要回显的类变量。然后使用它在脚本标记内的html中显示。
<?php
$object->getComment();
?>