Determining logic to break down cards horizontally in bootsrap - php

I have the following piece of code which essentially creates bootstrap 4 cards dynamically after obtaining some values from a db. The problem is however each new entry keeps getting stacked horizontally and I am struggling to write a piece of logic to tell the html to break a new line to stack cards once its more than 3 cards.
Here is what I have so far, really appreciate some guidance on it
<div class="row row-container">
<?php foreach ($data as $row) : ?>
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">
<?php echo $row->title?></h5>
<p class="card-text"><?php echo $row->ID?></p>
View
</div>
</div>
</div>
<?php endforeach; ?>
</div>
My initial code that works fine other than the fact that it keeps stacking new elements horizontally.Essentailly to break a new row I need a new line of <div class="row row-container"> which is what I cant think of at the moment on how to get.
I tried adding the logic as below which would check using a count variable whether the number of items is divisible by 3 or not (it should break a new row if it is) but I dont know how to conttine
<div class="row row-container">
<?php $count=0; ?>
<?php foreach ($data as $row) : ?>
<?php
$count++
?>
<?php
if (($count % 3) ==0 ){
echo "<div class="row row-container">";
}
echo "$count" ?>
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title"><?php echo $row->title ?></h5>
<p class="card-text"><?php echo $row->description ?></p>
View
</div>
</div>
</div>
<?php endforeach; ?>
</div>

There are different ways of doing this, but if the sole purpose is to ensure that every 4th column goes to a new row, I would use array_chunk to achieve this:
<?php foreach (array_chunk($data, 3) as $row) : ?>
<div class="row row-container">
<?php foreach ($row as $col) : ?>
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">
<?php echo $col->title?></h5>
<p class="card-text"><?php echo $col->ID?></p>
View
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
Here's a live demo for your quick reference.
The array_chunk function breaks an array into smaller chuncks. More details are available at php.net.

Related

How to properly display this php block

I'm using data from MySQL database and display looks like this:
I want 3 articles in one line. The pictures below are made with HTML and CSS.
This is my code:
while($row=mysqli_fetch_array($result))
{?>
<div class="container">
<div class="row padding">
<div class="col-md-4 col-xs-12">
<div class="card">
<img class="card-img-top" src="<?php echo UPLPATH . $row['slika']; ?>">
<div class="card-body">
<h6 class="card-title">« <?php echo $row['naslov']; ?> »</h4>
</div>
<p class="ispod"><?php echo $row['datum'];?> </p>
</div>
</div>
</div>
</div>
<?php }?>
This seems to be a nice point in your project to implement a template engine. There a couple out there. I really liked working with typo3 fluid, but back to your question...
For your existing code, you can end your php code, use html and just echo the php variables you need.
<?php
// ... some php stuff
?>
<div class="container">
/* some html stuff * /
<img class="malaSlika" src="<?php echo UPLPATH . $row['slika']; ?>">
/* some more html stuff */
/* <?= is short for <?php echo
<img class="malaSlika" src="<?= UPLPATH . $row['slika']; ?>">
</div>
<?php
// ... some more php stuff
As said, maybe a template engine would help this even further.
Edit: To add div class="row" it could be something like this.
<?php ... ?>
<div class="container">
<?php
$i = 0;
while($row=mysqli_fetch_array($result)) :
if($i % 3 == 0) : ?>
<div class="row padding">
<?php endif; ?>
<div class="col-md-4 col-xs-12">
<div class="card">
<img class="card-img-top" src="<?php echo UPLPATH . $row['slika']; ?>">
<div class="card-body">
<h6 class="card-title">« <?php echo $row['naslov']; ?> »</h4>
</div>
<p class="ispod"><?php echo $row['datum'];?> </p>
</div>
</div>
<?php if($i % 3 == 0) : ?>
</div>
<?php endif; ?>
<?php
$i++;
endwhile;
?>
</div>
It will always get ugly at some point I think. This is a quick solution. the modulo (%) devides by the number and returns the rest value. so $i modulo 3 return 0 at 0, 3, 6, 9, 12 etc. Meaning it will always add the "row padding" div.

bootstrap 4 cards with php foreach loop

Iam trying to get my web page to display all of my shop items in bootstrap 4 cards. 3 wide by however many deep ( i may add pagination another day )
I've got a php foreach loop which populates bootstrap4 cards perfectly. The trouble is they display vertically ( one on top of the other ) . ive tried class= columns which works on dummy divs but not when i integrate with my for each loop.
I ve tried everything regarding bootstrap docs but cant get the cards to display 3 wide and however many deep ( the foreach and the items control this. )
Should i be even using 'cards' or use something else. thx for your time
<div class="container">
<!-- $result = my php code using x-path to get results from xml query goes here. -->
<?php
foreach ( $result as $elements){
?>
<div class="row-fluid ">
<div class="col-sm-4 ">
<div class="card-columns-fluid">
<div class="card bg-light" style = "width: 22rem; " >
<img class="card-img-top" src=" <?php echo $elements->pictures->picture[2]->filename ; ?> " alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><b><?php echo $elements->advert_heading ?></b></h5>
<p class="card-text"><b><?php echo $elements->price_text ?></b></p>
<p class="card-text"><?php echo $elements->bullet1 ?></p>
<p class="card-text"><?php echo $elements->bullet2 ?></p>
Full Details
</div></div></div></div>
<?php
}
}
?>
</div>
</div> <!--container close div -->
As stated, the .row-fluid should be outside of the loop :
<div class="container">
<div class="row-fluid ">
<!-- my php code which uses x-path to get results from xml query. -->
<?php foreach ( $result as $elements) : ?>
<div class="col-sm-4 ">
<div class="card-columns-fluid">
<div class="card bg-light" style = "width: 22rem; " >
<img class="card-img-top" src=" <?php echo $elements->pictures->picture[2]->filename ; ?> " alt="Card image cap">
<div class="card-body">
<h5 class="card-title"><b><?php echo $elements->advert_heading ?></b></h5>
<p class="card-text"><b><?php echo $elements->price_text ?></b></p>
<p class="card-text"><?php echo $elements->bullet1 ?></p>
<p class="card-text"><?php echo $elements->bullet2 ?></p>
Full Details
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div> <!--container div -->
You need to move your <div class="row-fluid "> class outside of the foreach loop, otherwise it will create a new row for each class.
Also, as a comment has mentioned, you need to close all your divs correctly.

Fill all divs, even when the array is too short

I'm developing a WordPress site using Bootstrap. I want to get the first three posts in a category, then loop through them and put all three in a single row.
The trouble is that when a category has only one or two posts the layout is broken, because no markup for the div is written to the page.
<div class="row">
<?php
foreach(array_slice($posts_array, 0, 3) as $post):
?>
<div class="col col-12 col-lg-4">
<?php echo get_the_post_thumbnail(); ?>
<?php echo $post->post_title; ?>
</div>
<?php
endforeach;
?>
</div>
I need this to generate
<div class="row">
<div class="col col-12 col-lg-4">
<img src=image.jpg">
Title
</div>
<div class="col col-12 col-lg-4">
<img src=image2.jpg">
Second Title
</div>
<div class="col col-12 col-lg-4">
<img src=image3.jpg">
Third Title
</div>
</div>
But if a category only has two posts then I get
<div class="row">
<div class="col col-12 col-lg-4">
<img src=image.jpg">
Title
</div>
<div class="col col-12 col-lg-4">
<img src=image2.jpg">
Second Title
</div>
</div>
I'm sure this can't be too hard, but my brain doesn't seem to be working. Please help!
Count the array, try and see if $count % 3 == 0, if not, add fake array elements onto the end, and in your code, just check for something in the fake array that shows it is a real row or not, then you can display a blank box or whatever for the remaining elements.
The other way is starting $x = 1, and incremementing within the loop.
Try This:
<div class="row">
<?php
$val = 3;
$i = 1;
foreach(array_slice($posts_array, 0, $val) as $post):
?>
<div class="col col-12 col-lg-4">
<?php echo get_the_post_thumbnail(); ?>
<?php echo $post->post_title; ?>
</div>
<?php
$i++; endforeach;
?>
<?php for ($j = 0; $j < ($val - $i); $j++) { ?>
// Your Extra Divs.
<?php } ?>

Not able to align horizontal slider in a row

I have a horizontal carousel that is working fine, here is the code for it
Now i wish to fetch the data from database and display it that corousel. Following is the code that i used
<div class='row'>
<div class='col-xs-12'>
<div class="carousel slide media-carousel" id="media">
<div class="carousel-inner">
<div class="item active">
<div class="row">
<?php foreach($student as $student): ?>
<div class="col-md-3">
<a class="thumbnail" href="#"><?php echo $student->fullname;?></a>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
<a data-slide="prev" href="#media" class="left carousel-control">‹</a>
<a data-slide="next" href="#media" class="right carousel-control">›</a>
</div>
</div>
</div>
but the view is distorted and instead of all the slides sliding in one row, the view is coming like this
That's because unless the row is active, the other .item row gets the property display:none; If you were to remove the display:none; it should work as you want.
Create the class="row" inside the loop. Also keep in mind that the screen consists of 12 units only, so if you create a row of more than 12 units it will go to next line automatically. As you have created the loop that creates "col-md-3" for more than 4 times which sums more than 12, so it moves to next line.
For reference view this
As you have not given the php code, i am giving u sample of it
The array that you are getting divide it into a group of 4 (or as many as you want) like given below
$query = $this->db->get('student');
$r = $query->result();
$s = (array_chunk($r, 4));
return $s;
And then on the code you have given, change into following
<div class="carousel-inner">
<?php foreach($s as $key => $per_student): ?>
<?php if($key=='0'): ?>
<div class="item active">
<?php else: ?>
<div class="item ">
<?php endif; ?>
<div class="row">
<?php foreach($per_student as $student): ?>
<div class="col-md-3">
<a class="thumbnail" href="#"><?php echo $student->fullname;?></a>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>

3 element per row in bootstrap/Wordpress

I have the following problem: i need to create really simple layout, on each line i would like to have 3 box all of the same size, and if i understood right, in order to achieve this i need to construct a structure like the following:
<div class="row">
<div class=" news col-md-3 col-centered">
</div>
<div class=" news col-md-3 col-centered">
</div>
<div class=" news col-md-3 col-centered">
</div>
</div>
This is my php script in the index.php:
<?php while(have_posts()) : the_post();?>
<div class="row">
<div class=" news col-md-3 col-centered">
<h4><?php the_title();?></h4>
<p><?php the_excerpt(); ?> </p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
With this code, each box get a <div class="row"> element like the following:
<div class="row">
<div class=" news col-md-3 col-centered">
</div>
</div>
...
How can i fix this?
This is what i get now: if a box as more height than another, it leave the space under it without any element.
the height of the box depends on the content. what i would like to have is something like this:
Just move the row outside of The Loop, so that it's not repeated:
<div class="row">
<?php while(have_posts()) : the_post(); ?>
<div class="news col-md-4 col-centered">
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?> </p>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
Also, you need to change your column width to col-md-4, since Bootstrap uses a 12-column grid, and you want 3 columns per row.
If you need to actually close out each row after 3 columns are shown, you need to use a counter:
<div class="row">
<?php $i = 1; while(have_posts()) : the_post();?>
<div class="news col-md-3 col-centered">
<h4><?php the_title();?></h4>
<p><?php the_excerpt(); ?> </p>
</div>
<?php if ( $i % 3 === 0 ) { echo '</div><div class="row">'; } ?>
<?php $i++; endwhile; wp_reset_query(); ?>
</div>
This last example will ensure that all floats are cleared, and that each row of elements are on their own lines.

Categories