javascript - 在 php while 循环中循环 HTML 代码

标签 javascript php jquery html css

我希望 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/

相关文章:

javascript - 乘法 2 数组 javascript/jquery

javascript - 从元素底部计算像素

php - 在 Android 中使用 PHP 将数据作为 JsonArray 发送到 MySQL 的教程

php - 将 .slideToggle 与动态生成的 div 一起使用

javascript - IE8开发者工具调试器给出错误的异常信息

jquery - 单击 jQuery UI 对话框中的按钮之一

javascript - 如何将文件从 Axios 上传到 Django?

javascript - 在加载 jQuery 模板后初始化 SLICK Carousel

javascript - PHP AJAX 和 MySQL

php - 如何将 yyyy mm dd 转换为 Laravel 日期并插入 mysql date() 列