duagfgfn1981 2018-05-18 05: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:

  1. <div class="section projects">
  2. <span class="heading">Blog</span>
  3. <p>
  4. Here i have compiled a list of articles that i have written. I will continue to update this list.
  5. </p>
  6. <div class="project-list">
  7. <?php
  8. if ($post_results) {
  9. $panel_no = 1;
  10. /* fetch associative array */
  11. while ($row = mysqli_fetch_row($post_results)) {
  12. $result = glob ("posts/".$row[0]."/article-image.*");
  13. echo "<div class='project-list-item'>";
  14. echo "<img src='$result[0]' class='project-list-image' alt='Blog Item Image'>";
  15. echo "<div class='project-heading'>".$row[1]."</div>";
  16. echo "<div class='project-subheading'>".$row[2]."</div>";
  17. echo "</div>";
  18. }
  19. ?>
  20. </div>
  21. <div class="carousel">
  22. <div class="chevron left">&laquo</div>
  23. <div class="chevron right">&raquo</div>
  24. <?php
  25. while ($row = mysqli_fetch_row($post_results)){
  26. echo "<div id='panel-". $panel_no ."' class='panel";
  27. if($panel_no == 1){
  28. echo " active";
  29. }
  30. echo "'>";
  31. echo"<img src='".$result[0]."' id='panel-image". $panel_no ."' class='panel-image'>";
  32. echo "<div class='panel-description'>";
  33. echo "<div class='carousel-heading'>".$row[1]."</div>";
  34. echo "<div class='carousel-caption'>".$row[2]."</div>";
  35. echo "</div>";
  36. echo "</div>";
  37. $panel_no = $panel_no + 1;
  38. }
  39. ?>
  40. </div>
  41. <?php
  42. /* free result set */
  43. mysqli_free_result($post_results);
  44. }
  45. ?>
  46. </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.

  1. <div class="section projects">
  2. <span class="heading">Blog</span>
  3. <p>
  4. Here i have compiled a list of articles that i have written. I will continue to update this list.
  5. </p>
  6. <div class="project-list">
  7. <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>
  8. <div class="carousel">
  9. <div class="chevron left">«</div>
  10. <div class="chevron right">»</div>
  11. </div>
  12. </div>

展开全部

  • 写回答

1条回答 默认 最新

  • dongxunhua2054 2018-05-18 05: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:

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

    The second loop should look something like this:

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

    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.

    展开全部

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部