在php循环中循环HTML代码

时间:2017-12-26 15:05:51

标签: javascript php jquery html css

我想在每次执行while()后重复HTML代码。但它并没有重复。

实际上,我希望它在每一行都能看到一个方框,用注释和底部的按钮组成。因此,我将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; ?>

请帮助我该怎么办?

1 个答案:

答案 0 :(得分:0)

一个简单的原因是在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>';
      }
  ?>