Home > Net >  How can I repeat loop for all categories
How can I repeat loop for all categories

Time:12-22

I have a loop in PHP that did not really end up like I wanted to since i am not experienced with PHP and could not figure out. I want to repeat a loop to show every post for each category which have tables called "tblcategories" and "tblposts". I want to do 2 categories per row with 2posts each but did not even manage to do more than a single category. Here is the UPDATED code:

 <?php   
       $no_of_records_per_page = 2;
       $selectcat = "SELECT * FROM tblcategory"; //get all categories
       $stmt = $con->prepare($selectcat); 
       $stmt->execute();
       $result = $stmt->get_result();
       $counter = 0;
       while ($row = mysqli_fetch_array($result)) { //loop all category. here row is single category
           $query="select tblposts.id as pid,tblposts.PostTitle as posttitle,tblposts.PostImage,tblcategory.CategoryName as category,tblcategory.id as cid,tblsubcategory.Subcategory as subcategory,tblposts.PostDetails as postdetails,tblposts.PostingDate as postingdate,tblposts.PostUrl as url from tblposts left join tblcategory on tblcategory.id=tblposts.CategoryId left join  tblsubcategory on  tblsubcategory.SubCategoryId=tblposts.SubCategoryId where tblposts.Is_Active=1 and tblposts.CategoryId=? order by tblposts.id desc  LIMIT ?";
           $stmt = $con->prepare($query); 
           $stmt->bind_param("ii", $row['id'], $no_of_records_per_page);
           $stmt->execute();
           $resultt = $stmt->get_result();
           
           if ($counter % 2 == 0) echo "<div class='row'>"; //here we open row for each even category, so 0, 2, 4,...
           else $counter  ;
       
           
           echo "<div class='col-md-5'>";
       
           while ($row1 = mysqli_fetch_array($resultt)) {
               echo "<div class='col-md-12'>"; ?>

<h2 style="text-align:center;" ><a href="category.php?catid=<?php echo htmlentities($row1['cid'])?>"><?php echo htmlentities($row1['category']);?></a></h2>
                <hr> 
                    
                <div ><a href="news-details.php?nid=<?php echo htmlentities($row1['pid'])?>">
                        <img  src="admin/postimages/<?php echo htmlentities($row1['PostImage']);?>" alt="<?php echo htmlentities($row1['posttitle']);?>" style="width:250px; height:auto; box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);">
                        <h2 style="font-size:20pt;" ><?php echo htmlentities($row1['posttitle']);?></h2>
                        
                    </a>
                    <p ><b>Category : </b> <a href="category.php?catid=<?php echo htmlentities($row1['cid'])?>"><?php echo htmlentities($row1['category']);?></a> <br><?php echo htmlentities($row1['postingdate']);?></a></p>
                </div>
                <hr>

       <?php
               // show here post from $row1
       
               echo "</div>";
           }
           echo "</div>";
           if ($counter % 2 == 1) echo "</div>"; //here we close row for category so, 1, 3, 5,...
       }
        ?>       

Something like this, without the sidebar, i will figure the sidebar out later

CodePudding user response:

I can't totally understand you, but I think you have to select all categories first, then with while loop you have to select its posts.

Your html structure should be like this

image

So like this:

$no_of_records_per_page = 2;
$selectcat = "SELECT * FROM tblcategory"; //get all categories
$stmt = $con->prepare($selectcat); 
$stmt->execute();
$result = $stmt->get_result();
$counter = 0;
while ($row = mysqli_fetch_array($result)) { //loop all category. here row is single category
    $query="select tblposts.id as pid,tblposts.PostTitle as posttitle,tblposts.PostImage,tblcategory.CategoryName as category,tblcategory.id as cid,tblsubcategory.Subcategory as subcategory,tblposts.PostDetails as postdetails,tblposts.PostingDate as postingdate,tblposts.PostUrl as url from tblposts left join tblcategory on tblcategory.id=tblposts.CategoryId left join  tblsubcategory on  tblsubcategory.SubCategoryId=tblposts.SubCategoryId where tblposts.Is_Active=1 and tblposts.CategoryId=? order by tblposts.id desc  LIMIT ?";
    $stmt = $con->prepare($query); 
    $stmt->bind_param("ii", $row['id'], $no_of_records_per_page);
    $stmt->execute();
    $resultt = $stmt->get_result();
    
    if ($counter % 2 == 0) echo "<div class='row'>"; //here we open row for each even category, so 0, 2, 4,...
    else $counter  ;

    
    echo "<div class='col-md-5'>"; //open col for row posts category title will be inside col-5

    while ($row1 = mysqli_fetch_array($resultt)) {
        echo "<div class='col-md-12'>";

        // show here post from $row1

        echo "</div>";
    }
    echo "</div>"; // close col-5
    if ($counter % 2 == 1) echo "</div>"; //here we close row for category so after two col-5 row will be close
}
  •  Tags:  
  • php
  • Related