Is there a way to have the card appear on the side when using a foreach loop in PHP?

The card design displayed here needs to be aligned to the side

I am attempting to showcase data from my database using PHP Codeigniter. The data is displaying correctly, but the issue I'm facing is that the card layout is positioned vertically instead of horizontally. You can refer to the image provided above for reference.

My goal is to have the cards align horizontally when looping through the data using a foreach loop, and then stack them vertically after every 4 or 5 cards.

I have attempted to adjust the position of the foreach loop code, but it hasn't resolved the issue. I am utilizing Bootstrap 4 for this project.

Below is the code snippet:

<div class="row">
        <div class="container">
            <div class="card-body">
                <?php
                    foreach ($user as $u) {?>
                <div class="col-md-2 mt-4">
                    <div class="card mt-3" style="width:15rem;">
                        <img src="<?=base_url() . 'assets/img/' . $u->gambar_game;?>"
                            class="card-img-top img-responsive" alt="...">
                        <h5 class="card-title font-weight-bold  "><?php echo $u->nama_game; ?></h5>
                        <p class="card-text"><?=$u->deskripsi_game;?></p>
                        <a href="<?=base_url() . 'assets/games/' . $u->link_game;?>"
                            class="btn btn-primary btn-block">Play Game</a>
                    </div>
                </div>
            </div>
            <?php }?>
        </div>
    </div>

Answer №1

Your current issue is that you are repeating the entire container inside the row, but you actually want to repeat the columns inside the container.

Here's a suggestion (loop over the col-md-2):

<div class="row">

        <div class="container">
            <div class="card-body">
     <?php
foreach ($user as $u) {?>
                <div class="col-md-2 mt-4">
                    <div class="card mt-3" style="width:15rem;">
                        <img src="<?=base_url() . 'assets/img/' . $u->gambar_game;?>"
                            class="card-img-top img-responsive" alt="...">
                        <h5 class="card-title font-weight-bold  "><?php echo $u->nama_game; ?></h5>
                        <p class="card-text"><?=$u->deskripsi_game;?></p>
                        <a href="<?=base_url() . 'assets/games/' . $u->link_game;?>"
                            class="btn btn-primary btn-block">Play Game</a>

                    </div>

                </div>
            </div>
 <?php }?>
        </div>

    </div>

    </div>

NOTE: Your HTML structure was a bit confused. Try this. Use col-md-3 to have 4 columns inside your row, or col-md-2 for 6 columns inside your row. Remember, the maximum total width for a row in Bootstrap grid is 12 units. Refer to Bootstrap Grid Documentation.

<div class="container">
  <div class="row">

  <?php foreach ($user as $u) {?>
  <!-- col-md-2  -->
    <div class="col-md-4 mt-4">
      <div class="card">
        <div class="card-body">
           <img src="<?=base_url() . 'assets/img/' . $u->gambar_game;?>"class="card-img-top img-responsive" alt="...">
           <h5 class="card-title font-weight-bold  "><?php echo $u->nama_game; ?></h5>
           <p class="card-text"><?=$u->deskripsi_game;?></p>
           <a href="<?=base_url() . 'assets/games/' . $u->link_game;?>" class="btn btn-primary btn-block">Play Game</a>
        </div>
      </div>
    </div>
    <!-- col-md-2 end -->
    <?php }?>

  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to automatically blur the input field and toggle it upon clicking the checkbox using Angular

<input (click)="check()" class="checkbox" type="checkbox"> <input [(ngModel)]="second_month" value="2019-09" type="month" [class.blurred]="isBlurred">> I need the second input(type=month) field to be initially blurred, and only unblur ...

Guide on transferring a 200MB database to an HTML5 web page executed locally

In the process of creating a search tool for internal use within my organization, I have established a deployment strategy that involves: Storing an HTML5 web page on the file server. Keeping a 200MB JSON or JavaScript file in another location. Currentl ...

PHP Unable to Transmit POST Information

After successfully getting the first page of my registration form to work, I've encountered a new issue. The user must choose one of three options before proceeding to the next page, but it seems that the data from the first page is not being sent to ...

What is the best way to create a dot animation using CSS or JavaScript?

https://i.sstatic.net/60Hym.gif This particular GIF is sourced from Dribbble. I attempted to create a demo using pure CSS. Below are snippets of my code: @keyframes circles{ 0%{ transform: scale(0) rotate(150deg); } 100%{ transform: scale( ...

What is the best method for sending the styled and checked option via email?

Just finished customizing the checkboxes on my contact form at cleaners.se/home. Here's my question: If I select "telefon," how can I ensure that this option is sent to my email? In Contact Form 7, I used to simply type a shortcode. Is there a simila ...

Issue with Jdenticon icons not displaying correctly in ng-repeat loop

Recently, I started using the Jdenticon JavaScript library for generating user icons. This library allows me to input a hash and then render it as either SVG or Canvas. Here is an example code snippet: <svg width="200" height="200" data-jdenticon-has ...

What could be causing the data-price values of my alternative select options to not add up correctly when running this function?

ISSUE I am facing a challenge in calculating the total values of select inputs based on the radio button selected initially. Each radio button corresponds to different select inputs with their own data-price values. Once a radio button is chosen, the div ...

Utilize JavaScript to iterate through a JSON object and retrieve the indices that meet the specified criteria

While I found a previous answer that somewhat addresses my issue, I am seeking guidance on how to return an array of the indexes where a specific value appears. For example, if **18A38** is the target value, it should return the positions [1,3]. The sampl ...

I'm wondering why the columns in my Bootstrap 4 grid don't have uniform height and width

Currently, I am utilizing Bootstrap 4 to create a grid view. However, I have encountered an issue where my columns do not have the same width and their heights are uneven, as evident from the borders. It is mentioned that in Bootstrap 4, columns should inh ...

Delete the HTML line break

I need some help figuring out how to eliminate the line break in HTML. Essentially, I want to display two "Hello World" lines next to each other instead of stacked on top of each other. <p>Hello World</p> <p>Hello World</p> ...

Encountering a server error - what triggers this message upon utilizing .htaccess?

When I include a .htaccess file within a directory named admin containing the code below: AuthName "Alertme" AuthType Basic AuthUserFile /mobs/.htpasswd require valid-user and a .htpasswd file with the following credentials: rajasekar:66Acdia7gE1to Upo ...

Unlimited horizontal slider control powered by HTML

In my quest to develop a slider control with infinite capabilities, I am looking for a way for users to slide left and right on a bar or panel, allowing them to navigate through past dates when sliding left and future dates when sliding right. While there ...

The height of the logo is not the same as the height of the menu

Seeking assistance with aligning a logo next to the navigation menu on my website. Despite my attempts to adjust margins and paddings, the logo and menu appear to be misaligned. As a beginner in CSS, any guidance or tips would be highly appreciated! https ...

What is the best way to position an element on the left side of the navbar without having it animated?

Could someone assist me in placing the Life's Good logo on the left side of the nav bar without applying the animation from the "btn" class? Below is a snippet of my code with an example where I want the logo highlighted in Yellow: * { padding:0; m ...

The ongoing calculation consistently yields a result of zero

This content belongs to the index.php file if(isset($_POST['submit'])) { echo header('Location:answer.php'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> ...

Preventing the entire HTML page from loading in the registration form on ASP.NET: Tips and tricks

I am currently developing an ASP.NET web application that includes a registration form. However, I am facing an issue where every time a user enters their Username or Email, the entire first_home page reloads. How can I prevent the entire page from reloadi ...

Place a div element on top of the others by using the onClick() event to "dock" it

Can someone help me with setting/docking a div element above another div? The div contains some other divs and form objects, serving as an inputbox dialog that should appear right above an element when clicked. I have set the onClick() event for a Click &l ...

How can PHP be used to replace the div html() function?

I am working with multiple product elements that are dynamically assigned their class and ID using PHP: $product1["codename"] = "product-1"; $product1["short"] = "Great Product 1"; $product2["codename"] = "product-2"; $product2["short"] = "Great Product ...

How to Retrieve the Width of a Div Element in Jquery with Overflow Hidden

Is there a way to determine the width of a div that is inside another div with "overflow: hidden;"? I attempted setting overflow to auto and then using $("#divselector").width(), but it seems to always provide the width of the parent div! For example: H ...