我希望 HTML 代码在每次执行 while()
后重复。但它没有重复。
实际上,我希望在它读取的每一行中,底部都有一个带有评论和点赞按钮的框。因此,我将 HTML 代码保留在 while
循环中,但在每个执行行都写在同一个框中后运行代码。
引用代码如下:
<?php
$name = $_POST['name'];
$query = $_POST["query"];
$po = "$name-$query";
$myfile = fopen("posts.txt", "a+");
fwrite($myfile,$po);
fclose($myfile);
$mypost=fopen("posts.txt","r");
while(!feof($mypost)): ?> <!-- with this while loop i want this div class to repeat -->
<div class="w3-container w3-card w3-white w3-round w3-margin"><br>
<img src="/w3images/avatar2.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px">
<span class="w3-right w3-opacity">1 min</span>
<h4>fdg</h4><br>
<hr class="w3-clear">
<p> <?php
echo fgets($mypost);
?></p>
<button type="button" class="w3-button w3-theme-d1 w3-margin-bottom"><i class="fa fa-thumbs-up"></i> Like</button>
<button type="button" class="w3-button w3-theme-d2 w3-margin-bottom"><i class="fa fa-comment"></i> Comment</button>
</div>
<?php endwhile; ?>
请帮忙我能做什么?
最佳答案
一个简单的原因是在 php 代码块中对 html 代码使用相关字符串的回显{}
<?php
$name = $_POST['name'];
$query = $_POST["query"];
$po = "$name-$query";
$myfile = fopen("posts.txt", "a+");
fwrite($myfile,$po);
fclose($myfile);
$mypost = fopen("posts.txt","r");
while(!feof($mypost)) {
echo '<div class="w3-container w3-card w3-white w3-round w3-margin"><br>
<img src="/w3images/avatar2.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px">
<span class="w3-right w3-opacity">1 min</span>
<h4>fdg</h4><br>
<hr class="w3-clear">
<p>'. fgets($mypost)
. '</p>
<button type="button" class="w3-button w3-theme-d1 w3-margin-bottom"><i class="fa fa-thumbs-up"></i> Like</button>
<button type="button" class="w3-button w3-theme-d2 w3-margin-bottom"><i class="fa fa-comment"></i> Comment</button>
</div>';
}
?>
关于javascript - 在 php while 循环中循环 HTML 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47980369/