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">«</div>
<div class="chevron right">»</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>