【问题标题】:Use MySQLi query results array more than once多次使用 MySQLi 查询结果数组
【发布时间】:2018-05-18 13:42:34
【问题描述】:

我正在我的网站上制作博客列表部分。我已经决定,当窗口为桌面大小时,我将有一个轮播来滚动浏览不同的博客文章,而当它是移动大小时,它将只是一个包含所有博客文章的列表。

当我尝试重复 while 循环以循环遍历数据库中的所有帖子时,第二次 while 循环似乎不起作用,就好像我将 $row 变量重新分配给 mysqli_fetch_row() 函数一样包含我的结果,它找不到我的结果。

代码如下:

<div class="section projects">
        <span class="heading">Blog</span>
        <p>
            Here i have compiled a list of articles that i have written. I will continue to update this list.
        </p>
        <div class="project-list">
            <?php

                if ($post_results) {
                    $panel_no = 1;  
                    /* fetch associative array */
                    while ($row = mysqli_fetch_row($post_results)) {
                        $result = glob ("posts/".$row[0]."/article-image.*");
                        echo "<div class='project-list-item'>";
                        echo "<img src='$result[0]' class='project-list-image' alt='Blog Item Image'>";
                        echo "<div class='project-heading'>".$row[1]."</div>";
                        echo "<div class='project-subheading'>".$row[2]."</div>";
                        echo "</div>";
                    }
                    ?>
        </div>
        <div class="carousel">
            <div class="chevron left">&laquo</div>
            <div class="chevron right">&raquo</div>
            <?php
                    while ($row = mysqli_fetch_row($post_results)){
                        echo "<div id='panel-". $panel_no ."' class='panel";
                            if($panel_no == 1){
                                echo " active";
                            }
                            echo "'>";
                            echo"<img src='".$result[0]."' id='panel-image". $panel_no ."' class='panel-image'>";
                            echo "<div class='panel-description'>";
                                echo "<div class='carousel-heading'>".$row[1]."</div>";
                                echo "<div class='carousel-caption'>".$row[2]."</div>";
                            echo "</div>";
                        echo "</div>";
                        $panel_no = $panel_no + 1;
                    }
                ?>
        </div>







        <?php
                /* free result set */
                mysqli_free_result($post_results);
            }
            ?>
    </div>

后台有一些 JS 控制轮播按钮等,它只是将分配给它的类“活动”的轮播面板设置为display: block;,而任何没有活动类的东西都有@ 987654324@.

第一个 while 循环有效,第二个无效。 SQL 确实有效,因为如果我将屏幕移动到移动大小,它会完全按照我想要的方式显示完整列表。

有一些我看不到的东西使面板不出现,感谢任何帮助。

编辑:这是运行此代码时当前输出的 HTML(忽略移动视图列表项的奇怪格式)。这是页面在移动设备视图中的时间。

<div class="section projects">
        <span class="heading">Blog</span>
        <p>
            Here i have compiled a list of articles that i have written. I will continue to update this list.
        </p>
        <div class="project-list">
            <div class="project-list-item"><img src="posts/28/article-image.jpg" class="project-list-image" alt="Blog Item Image"><div class="project-heading">Cars in a City</div><div class="project-subheading">This is an article about cars in a city.</div></div><div class="project-list-item"><img src="posts/29/article-image.jpg" class="project-list-image" alt="Blog Item Image"><div class="project-heading">Whitehaven Marina</div><div class="project-subheading">dsahidhsai</div></div><div class="project-list-item"><img src="posts/30/article-image.jpg" class="project-list-image" alt="Blog Item Image"><div class="project-heading">fcdsfds</div><div class="project-subheading">sadasdsa</div></div><div class="project-list-item"><img src="posts/31/article-image.jpg" class="project-list-image" alt="Blog Item Image"><div class="project-heading">fcdsfds</div><div class="project-subheading">sadasdsa</div></div><div class="project-list-item"><img src="posts/32/article-image.png" class="project-list-image" alt="Blog Item Image"><div class="project-heading">fvsdvsdc</div><div class="project-subheading">fcdcdsccdscds</div></div>            </div>
        <div class="carousel">
            <div class="chevron left">«</div>
            <div class="chevron right">»</div>
                        </div>







                </div>

【问题讨论】:

标签: javascript php html css mysqli


【解决方案1】:

这里的问题是 mysqli_fetch_row() 将 mysqli 的内部指针移动到下一个结果集。因此,当您再次尝试访问它时,没有数据可供访问。

一个解决方案是在第一个数组之前创建一个空的 PHP 数组并将这些行存储在其中,然后在第二个循环中您将通过新数组而不是 fetch_array()。

第一个循环:

   $posts = array();
   if ($post_results) {
        $panel_no = 1;  
        /* fetch associative array */
        while ($row = mysqli_fetch_row($post_results)) {
            $posts[] = $row;
            $result = glob ("posts/".$row[0]."/article-image.*");
            echo "<div class='project-list-item'>";
                echo "<img src='$result[0]' class='project-list-image' alt='Blog Item Image'>";
                echo "<div class='project-heading'>".$row[1]."</div>";
                echo "<div class='project-subheading'>".$row[2]."</div>";
            echo "</div>";
        }
    }

第二个循环应该是这样的:

foreach($posts as $row){
    $result = glob ("posts/".$row[0]."/article-image.*");
    echo "<div class='project-list-item'>";
        echo "<img src='$result[0]' class='project-list-image' alt='Blog Item Image'>";
        echo "<div class='project-heading'>".$row[1]."</div>";
        echo "<div class='project-subheading'>".$row[2]."</div>";
    echo "</div>";
}

除非没有其他方法,否则另一种解决方案是再次运行相同的查询(这是一种不好的做法)。

【讨论】:

  • 你需要从第二个循环中删除$posts[] = $row;,否则它永远不会结束。
  • 谢谢,不小心错过了:)
猜你喜欢
  • 1970-01-01
  • 2013-08-09
  • 2011-08-21
  • 1970-01-01
  • 2017-07-24
  • 2018-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多