douxiao0400 2017-03-12 21:34
浏览 21

从类外部回显php变量但在html标签内

My problem is:

How can access I variable from outside class like so below?

Please someone give me a hint because I don't want to echo html inside PHP but PHP inside the HTML.

<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();              
  • 写回答

1条回答 默认 最新

  • dongzhang0243 2017-03-12 22:19
    关注

    You should store the $commento variable in your Posts class. Then create a function to return it, and call that function

    class Posts {
    private $commento;
    public function getComment (){ echo $commento }
    
    public function DETAIL_p() {
    //other function code
    }
    

    Then later you can call it in your HTML.

    <?php
    $object->getComment();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号