dongshao6591 2015-10-25 06:01
浏览 24
已采纳

使用多个MySQL行和For循环 - 无法正常工作

I have trying to print the top 3 recent posts (rows) from MySQL. I am using a for loop that counts 3 times to prints out 3 DIVS for each post (row). Then I use for each loop to print out the MySQL row. The problem is I am printing out all three rows inside one DIV.

Here is the code:

 <!-- 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>

How can I print each MySQL row into each DIV created by for loop? Any ideas?

UPDATE:

Here are the results: https://gyazo.com/5a91ed6a3837c0293c7504525ef86681

As you can see, all the rows are printed in the first DIV. That is, the first time for loop is run, it printed out all the rows inside it. But the SECOND time for loop is run, it doesn't print out anything inside the second div. It SHOULD print out the SECOND row from mysql in the SECOND DIV and FIRST row of mysql in FIRST DIV. And THIRD row of mysql in THIRD DIV

  • 写回答

2条回答 默认 最新

  • dsgdf45654 2015-10-25 06:27
    关注

    If I get your question right you shouldn't be using for in the first place Print three divs with one result in it:

        <?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;
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?