【问题标题】:Working with multiple MySQL rows and For Loop - not working使用多个 MySQL 行和 For 循环 - 不工作
【发布时间】:2015-10-25 06:01:11
【问题描述】:

我试图从 MySQL 打印前 3 个最近的帖子(行)。我正在使用 for loop 计算 3 次,为每个帖子(行)打印 3 个 DIVS。 然后我使用for each 循环打印出MySQL 行。问题是我在一个 DIV 中打印出所有三行。

代码如下:

 <!-- Content Body Container -->
<div class="content-body" class="container-fluid">
  <div id="content-container">

    <?php
    $result = doSql("SELECT `gamereviews`.`reviewContent` FROM `gamereviews` ORDER BY `gamereviews`.`dateOfReview` DESC LIMIT 3");
    ?>

    <?php
      for($i = 1; $i<=3; $i++) {
    ?>
      <div id="outer-box">
        <div id="postImage-wrapper">
          image
        </div>
        <div id="postContent-wrapper">
          <h3> This is an Example post </h3>
          <span id="postInfo-wrapper">Author | 22/10/2015 | Xbox | 3 Comments</span><br />
          <?php
          if($result && mysqli_fetch_assoc($result)>0) {
            foreach($result as $row) {
              echo '<div onclick="alert(\''.$row['reviewContent'].'\');">'.substr($row['reviewContent'], 0, 300).'</div>';
            //  echo $row['reviewContent']. '<br /><br />';
            }
          }
          ?>
        </div>

        <div id="postRating-wrapper">
          7.7
        </div>
      </div>


  <?php
    }
    ?>
  </div>
</div>

如何将每个 MySQL 行打印到 for 循环创建的每个 DIV 中?有什么想法吗?

更新:

结果如下: https://gyazo.com/5a91ed6a3837c0293c7504525ef86681

如您所见,所有行都打印在第一个 DIV 中。也就是说,第一次运行 for 循环时,它会打印出其中的所有行。但是第二次 for 循环运行,它不会打印出第二个 div 内的任何内容。它应该在 SECOND DIV 中打印出 mysql 的 SECOND 行,并在 FIRST DIV 中打印出 mysql 的 FIRST 行。以及第三格中的第三行mysql

【问题讨论】:

  • 你想重复哪个 div??
  • 无。 div 正在工作,打印出 3 个 div。 for each 语句也在工作,但它在创建的第一个 div 内打印出 3 行。我希望将 3 行中的每一行分别打印在三个 div 中。所以每个post div都有一行(文章),总共三个post div。有什么想法吗?
  • 你想重复你的第一个 div 3 次?如果是,那是哪个 div??
  • 我添加了页面当前外观的屏幕截图。如您所见,所有行都打印在第一个 DIV 中。也就是说,第一次运行 for 循环时,它会打印出其中的所有行。但是第二次 for 循环运行,它不会打印出第二个 div 内的任何内容。它应该在 SECOND DIV 中打印出 mysql 的 SECOND 行,并在 FIRST DIV 中打印出 mysql 的 FIRST 行。和第三格中的第三行mysql。

标签: php mysql for-loop


【解决方案1】:

如果我的问题是正确的,那么您一开始就不应该使用 for 打印三个 div,其中包含一个结果:

    <?php
$counter = 0;
    if($result && mysqli_fetch_assoc($result)>0)
            foreach($result as $row) :
        if($counter >= 3)
            break;
        ?>
        <div id="outer-box">
            <div id="postImage-wrapper">
                image
            </div>
            <div id="postContent-wrapper">
                <h3> This is an Example post </h3>
                <span id="postInfo-wrapper">Author | 22/10/2015 | Xbox | 3 Comments</span><br />
                <?php
                echo '<div onclick="alert(\''.$row['reviewContent'].'\');">'.substr($row['reviewContent'], 0, 300).'</div>';
                //  echo $row['reviewContent']. '<br /><br />';
                ?>
            </div>

            <div id="postRating-wrapper">
                7.7
            </div>
        </div>


        <?php
    $counter++;
    endforeach;
    ?>

【讨论】:

  • 谢谢,它成功了。但我不明白你为什么没有为你的 for 和 for each 循环使用大括号?
  • 我不喜欢大括号,因为有时你不知道什么语句在哪里结束。结束;清楚地告诉你 foreach 在哪里结束:)。
  • 以下语句也是如此: foreach($result as $row) : if($counter >= 3) break;冒号 ':' 在这里是什么意思?
  • 冒号就像左大括号'{',现在脚本正在寻找右大括号'}'。当你在 foreach 示例中使用冒号 php 搜索 end{{statement_here}} 时,它的 endforeach;
【解决方案2】:

像这样更改您的代码。

  <?php
      if($result && mysqli_fetch_assoc($result)>0) {
           $i = 1;
        foreach($result as $row) { ?>
         <div id="outer-box">
             <div id="postImage-wrapper">
                 image
             </div>
           <div id="postContent-wrapper">
              <h3> This is an Example post </h3>
              <span id="postInfo-wrapper">Author | 22/10/2015 | Xbox | 3 Comments</span><br />

              <?php  echo '<div onclick="alert(\''.$row['reviewContent'].'\');">'.substr($row['reviewContent'], 0, 300).'</div>';
       ?>
           </div>

        </div>

        <div id="postRating-wrapper">
            7.7
        </div>

     <?php

      if($i == 3)
            break;
      $i++;
      }// end of foreach
      }// end of if()
      ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-02
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多