Click here to Skip to main content
13,803,579 members
Rate this:
Please Sign up or sign in to vote.
See more:
First is for slideshow,another one is for posts.Second while executed after the endwhile. But when I delete first while then I was able to get posts otherwise only 1st while is working

What I have tried:


  include "libs/db.php";
  include "libs/config.php";
  include "libs/function.php";

  $db = new database();
  $query = "SELECT * from posts order by id DESC";
$posts = $db->select($query);
$query = "SELECT * from categories";
$cats = $db->select($query);


<!--First while -->
<?php while($row1 = $posts->fetch_array()) :?>

        <div class="welcome-single-slide">
            <!-- Post Thumb -->
                        <img src="images/<?php echo $row1['image'];?>" alt="">

            <div class="project_title">

                <div class="post-date-commnents d-flex">
                    <a href="#">May 19, 2017</a>
                    <a href="#">5 Comment</a>

                <a href="single.php?idd=<?php echo $row1['id'];?>">
                    <h5>"<?php echo $row1['title'];?>"</h5>



            <!-- Overlay Text -->

    <?php endwhile;?>


    <!-- ****** Welcome Area End ****** -->

    <!-- ****** Categories Area Start ****** -->
    <section class="categories_area clearfix" id="about">
        <div class="container">
            <div class="row">

<?php while ($row = $posts->fetch_array()) :?>
                  <!-- Single Post -->
                  <div class="col-12">
                      <div class="list-blog single-post d-sm-flex wow fadeInUpBig" data-wow-delay=".8s">
                          <!-- Post Thumb -->

                          <div class="post-thumb">
                              <img src="images/<?php echo $row['image'];?>" alt="">
                          <!-- Post Content -->
                          <div class="post-content">
                              <div class="post-meta d-flex">
                                  <div class="post-author-date-area d-flex">
                                      <!-- Post Author -->
                                      <div class="post-author">
                                          <a href="#"><?php echo $row['author'];?></a>
                                      <!-- Post Date -->
                                      <div class="post-date">
                                          <a href="#"><?php echo $row ['date'];?></a>
                                  <!-- Post Comment & Share Area -->
                                  <div class="post-comment-share-area d-flex">
                                      <!-- Post Favourite -->
                                      <div class="post-favourite">
                                          <a href="#"> 10</a>
                                      <!-- Post Comments -->
                                      <div class="post-comments">
                                          <a href="#"> 12</a>
                                      <!-- Post Share -->
                                      <div class="post-share">
                                          <a href="#"></a>
                              <a href="#">
                                  <h4 class="post-headline"><?php echo $row ['title'];?></h4>
                              <p><?php echo substr($row ['content'],0,300);?></p>
                              <a id="continuereading" href="single.php?idd=<?php echo $row ['id'];?>" class="read-more">Continue Reading..</a>

             <!--This is the second while for the same query-->
                <?php endwhile ;?>
Posted 7-Dec-17 0:14am
Updated 7-Dec-17 2:39am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Looking through your code, I'm a bit surprised you get anything useful at all.

You have php that is not surrounded by <?php> and <?>
You while loop is broken by interspersed HTML - so it's broken.

You must remember something very important: php is all executed on the server before the page is sent to the client for rendering by the browser.

If, for example, you wish to have HTML mixed with your php and repeated in a loop, it must be part of the php output.

   foreach($row as $r)
    ECHO "<tr><td>{$r[1]}</td><td>{$r[2]}</td><tr>"

Notice how the table may be declared outside the php loop but the rows are generated, in full, within it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181215.1 | Last Updated 7 Dec 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100