0

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 class="post-preview"><a href="news-details.php?nid=<?php echo htmlentities($row1['pid'])?>">
                        <img class="post-title" 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;" class="post-title"><?php echo htmlentities($row1['posttitle']);?></h2>
                        
                    </a>
                    <p class="post-meta"><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

tsundok
  • 9
  • 5
  • This answer may help you https://stackoverflow.com/questions/2398402/recursive-function-to-get-all-the-child-categories – Siddharth Rathod Dec 22 '21 at 06:32
  • Can you more explain your question? You want to show each category with 2 posts per row? – Farhod Nematov Dec 22 '21 at 06:32
  • @FarhodNematov so each row in table "tblcategory" is where the categories are. "tblposts" contains posts that goes in to different categories stored in tblcategory. as you can see in the code there are row and columns (row does not work in my code), i want to list one category per column, and two posts right under it per category. after 2 columns of categories are shown, next category should be next row – tsundok Dec 22 '21 at 06:37

1 Answers1

0

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
}
Farhod Nematov
  • 470
  • 2
  • 8
  • This is nearly what i wanted, everything is fine, only thing is, each 2 category should be in same row, next row should come after 2nd category from previous row – tsundok Dec 22 '21 at 06:48
  • @tsundok I have updated answer, check it please – Farhod Nematov Dec 22 '21 at 06:55
  • i have checked it and it is great so far, but only problem is, it every time opens 2 rows and for each row it is 2 columns but only for the same category side to side, i will upload an image to the post to show what i need – tsundok Dec 22 '21 at 07:03
  • I have added the image, but without the sidebar, i can figure it out later so I dont tire you too much – tsundok Dec 22 '21 at 07:08
  • Saw your comment in the code "row is single category" , each row should be 2 categories, only column will be 1 category – tsundok Dec 22 '21 at 07:20
  • You can't get by two item from sql. You have show posts inside single category and with html you will correct it. I have updated my answer see it – Farhod Nematov Dec 22 '21 at 07:29
  • row -> [col-md-5 -> col-12, col-12], [col-md-12 -> col-12, col-12] row -> [col-md-5 -> col-12, col-12], [col-md-12 -> col-12, col-12] row -> [col-md-5 -> col-12, col-12], [col-md-12 -> col-12, col-12] Your html structure to be like this – Farhod Nematov Dec 22 '21 at 07:30
  • now it shows 2 posts per category but 1 column per row, I will update post to how it is currently – tsundok Dec 22 '21 at 07:32
  • the drawing you did on my image is exactly what needed – tsundok Dec 22 '21 at 07:33
  • Did you check html structure with browser inspect element? Can you add html structure result? – Farhod Nematov Dec 22 '21 at 07:38
  • I have edited the post and added the result after first code – tsundok Dec 22 '21 at 07:42
  • The row closes after each category instead of each 2 category – tsundok Dec 22 '21 at 07:48
  • checked last update, row still closes after each category and for 2nd post col becomes col-12 – tsundok Dec 22 '21 at 08:10
  • @tsundok I have find my error. move $counter++ from else body to end of the first while loop. So while (...){ ... if ($counter % 2 == 0) // here is no else. .... //last line of while ` $counter++; ` } – Farhod Nematov Dec 22 '21 at 08:55
  • Wonderful everything is perfect now, at first I have realised the else mistake but could not think of the position... Thank you for your help... lastly do you know where I can replace a sidebar for 3rd column if you have time, if not it is totally fine i am satisfied – tsundok Dec 22 '21 at 09:31
  • I can't understand your question. If you want to show categories on right sidebar, simply collect categories to array and use it all places – Farhod Nematov Dec 22 '21 at 09:36
  • I mean, remember the picture i have in my post ? at the right side there is a sidebar which i connect from sidebar.php – tsundok Dec 22 '21 at 10:23
  • Sorry, I can't remember. If you need any help, you can make new question – Farhod Nematov Dec 22 '21 at 10:25