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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?