多次使用MySQLi查询结果数组

时间:2018-05-18 13:42:34

标签: javascript php html css mysqli

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

当我尝试重复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控制旋转木马按钮等,它只是将旋转木马面板设置为类&#34;活动&#34;分配给它为display: block;,而任何没有活动类的内容都有display: hidden;

第一个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>

1 个答案:

答案 0 :(得分:1)

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

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

第一循环:

reg [15:0]  LUT_DATA; 
reg [6:0]       LUT_INDEX;
parameter       LUT_SIZE = 31;
...
always
begin
case(LUT_INDEX) 
//  Video Config Data
0   :   LUT_DATA    <=  16'h9803;  //Must be set to 0x03 for proper operation
1   :   LUT_DATA    <=  16'h0100;  //Set 'N' value at 6144
2   :   LUT_DATA    <=  16'h0218;  //Set 'N' value at 6144
3   :   LUT_DATA    <=  16'h0300;  //Set 'N' value at 6144
4   :   LUT_DATA    <=  16'h1470;  // Set Ch count in the channel status to 8.
5   :   LUT_DATA    <=  16'h1520;  //Input 444 (RGB or YCrCb) with Separate Syncs, 48kHz fs
6   :   LUT_DATA    <=  16'h1630;  //Output format 444, 24-bit input
7   :   LUT_DATA    <=  16'h1846;  //Disable CSC
8   :   LUT_DATA    <=  16'h4080;  //General control packet enable
9   :   LUT_DATA    <=  16'h4110;  //Power down control
10  :   LUT_DATA    <=  16'h49A8;  //Set dither mode - 12-to-10 bit
11  :   LUT_DATA    <=  16'h5510;  //Set RGB in AVI infoframe
12  :   LUT_DATA    <=  16'h5608;  //Set active format aspect
13  :   LUT_DATA    <=  16'h96F6;  //Set interrup
14  :   LUT_DATA    <=  16'h7307;  //Info frame Ch count to 8
15  :   LUT_DATA    <=  16'h761f;  //Set speaker allocation for 8 channels
16  :   LUT_DATA    <=  16'h9803;  //Must be set to 0x03 for proper operation
17  :   LUT_DATA    <=  16'h9902;  //Must be set to Default Value
18  :   LUT_DATA    <=  16'h9ae0;  //Must be set to 0b1110000
19  :   LUT_DATA    <=  16'h9c30;  //PLL filter R1 value
20  :   LUT_DATA    <=  16'h9d61;  //Set clock divide
21  :   LUT_DATA    <=  16'ha2a4;  //Must be set to 0xA4 for proper operation
22  :   LUT_DATA    <=  16'ha3a4;  //Must be set to 0xA4 for proper operation
23  :   LUT_DATA    <=  16'ha504;  //Must be set to Default Value
24  :   LUT_DATA    <=  16'hab40;  //Must be set to Default Value
25  :   LUT_DATA    <=  16'haf16;  //Select HDMI mode
26  :   LUT_DATA    <=  16'hba60;  //No clock delay
27  :   LUT_DATA    <=  16'hd1ff;  //Must be set to Default Value
28  :   LUT_DATA    <=  16'hde10;  //Must be set to Default for proper operation
29  :   LUT_DATA    <=  16'he460;  //Must be set to Default Value
30  :   LUT_DATA    <=  16'hfa7d;  //Nbr of times to look for good phase

default:        LUT_DATA    <=  16'h9803;
endcase

第二个循环看起来像这样:

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

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