duagfgfn1981 2018-05-18 13:42
浏览 68
已采纳

不止一次使用MySQLi查询结果数组

I am making a blog listings section on my website. I have decided that when the window is desktop size, i will have a carousel that scrolls through the different blog posts, and when it is mobile size, it will just be one list with all of the blog posts on.

When i try to repeat a while loop to loop through all of the posts in a database, twice, the second while loop doesn't seem to work, as if when i reassign the $row variable to the mysqli_fetch_row() function containing my results, it can't find my results.

Here is the code:

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

There is some JS in the background controlling the carousel buttons etc. and it just sets the carousel panel with the class "active" assigned to it to be display: block;, and anything that doesn't have the active class has display: hidden;.

The first while loop works, the second one doesn't. The SQL definitely works, since if i move the screen to be mobile size, it displays the full list exactly how i want it.

There is something i cannot see that is making the panel not appear, any help is appreciated.

Edit: This is the HTML that is outputted currently when this code is ran (ignore the weird formatting of the mobile view list items). This is when the page is in mobile device view.

<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>
  • 写回答

1条回答 默认 最新

  • dongxunhua2054 2018-05-18 13:50
    关注

    The problem here is that mysqli_fetch_row() moves the internal pointer of the mysqli to the next result set. So when you try to access it again there is no data for it to access.

    One solution would be to create an empty PHP array before the first array and store those rows in it, and than in the second loop you go trough the new array instead of fetch_array().

    First loop:

       $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>";
            }
        }
    

    The second loop should look something like this:

    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>";
    }
    

    The other solution would be to run the same query again (WHICH IS A BAD PRACTICE) unless there is no other way of doing it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)