输出html的echo语句的最佳实践


best practices for echo statement that outputs html?

大家好,我有一个应用程序,允许用户在空的div上发布内容。我需要一些关于使其可扩展的建议,因为我必须确保如果用户发布了大量内容,浏览器不会减慢或崩溃。我是一个初学者,所以请容忍我。所以在下面的代码中,我有一个echo语句,可以打印出html内容。我想知道从长远来看如何使该功能可扩展?我应该将html设置为变量,然后打印它吗?我应该用JSON编码吗?请给我这样的建议。谢谢

PHP代码

<?php
include_once("connection.php");
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
//You can use Inner Join to display the matched records in between the two tables
$query = "SELECT * FROM `qpost` A INNER JOIN `user` B ON A.id=B.id ";
$result = $conn->query($query);
 if($result) {
 	
 } else {
 	echo "bitch: " . $conn->error;
 }
while($row = $result->fetch_assoc()) {
$time = time();
if( $time < $row['logged_time'] + 30) {
echo " <div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
	<div class='panel-heading'>
		 <h3 class='panel-title'><span class='glyphicon glyphicon-user'> <b>{$row['username']}</b></span> &#8195&#8195 &#8195&#8195 <span class='glyphicon glyphicon-time'></span> Posted_on: {$row['time']}      </h3> 
	</div>
	<div class='panel-body' style='word-break:break-all'>
       
		<h4>{$row['question']} </h4>
        <p> {$row['description']}</p>
       
    </div>
    <div class='panel-footer'>
    	<button class='btn btn-primary'>Request Connection</button>
        <button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
    </div>
</div>
</div>
</div> ";
  }//end if
}//end while
?>

您是否尝试分离数据逻辑和数据显示?如果您使用phpMVC框架,这将在您的案例中更加方便。下面是一个简单的例子。

//demo.html code
<?php
 include_once("connection.php");
 $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
 //You can use Inner Join to display the matched records in between the two tables
$query = "SELECT * FROM `qpost` A INNER JOIN `user` B ON A.id=B.id ";
$result = $conn->query($query);
if ($result) {
} else {
   echo "bitch: " . $conn->error;
}
$time = time();
$data_result = array(); // focus on your data logic process
while ($row = $result->fetch_assoc()) {
  if ($time < $row['logged_time'] + 30) {
    $data_result[] = $row;
  }
}
?>
<?php foreach($data_result as $index => $row) { ?>
<div class='row'>
    <div class='col-md-8'>
        <div id='thePost' class='panel panel-default'>
            <div class='panel-heading'>
                <h3 class='panel-title'><span class='glyphicon glyphicon-user'> <b><?php echo $row['username']?></b></span>
                    &#8195&#8195 &#8195&#8195 <span class='glyphicon glyphicon-time'></span> Posted_on: <?php echo $row['time']?>
                </h3>
            </div>
            <div class='panel-body' style='word-break:break-all'>
                <h4><?php echo $row['question']?></h4>
                <p> <?php echo $row['description']?></p>
            </div>
            <div class='panel-footer'>
                <button class='btn btn-primary'>Request Connection</button>
                <button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
            </div>
        </div>
    </div>
</div>
 <?php } ?>

你所做的一切都很好。然而,正如Eric所说,您可以退出并重新输入您的PHP

您可以将html存储在一个文件中,使用file_get_contents获取它,然后使用sprintf操作它

在一个名为"row.html"的文件中

<div class='row'>
<div class='col-md-8'>
<div id='thePost' class='panel panel-default'>
    <div class='panel-heading'>
         <h3 class='panel-title'>
             <span class='glyphicon glyphicon-user'>  
                <b>%s</b> <!-- username -->
             </span> &#8195&#8195 &#8195&#8195 
             <span class='glyphicon glyphicon-time'>
                 Posted_on: %s  <!-- time -->   
             </span> 
          </h3> 
    </div>
    <div class='panel-body' style='word-break:break-all'>
        <h4>%s</h4> <!-- question -->
        <p> %s</p>  <!-- description -->
    </div>
    <div class='panel-footer'>
        <button class='btn btn-primary'>Request Connection</button>
        <button class='btn btn-success'>Chat <span class='glyphicon glyphicon-comment'></button>
    </div>
</div>
</div>
</div> 

然后在你的PHP中。。。。

<?php
while($row = $result->fetch_assoc()) {
     $time = time();
     if( $time < $row['logged_time'] + 30) {
          $rowHtml = file_get_contents('row.html');
          echo sprintf($rowHtml, 
                       $row['username'], 
                       $row['time'], 
                       $row['question'], 
                       $row['description']);
     }
}
?>

我个人喜欢我的语言之间尽可能多的抽象:)

祝你好运!

用PHP回显HTML的一种方法是使用Heredoc syntax(http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

<?php
$name = 'MyName';
echo <<<MYHTML
<p>
My name is <b>"$name"</b>.
</p>
MYHTML;
?>