I want to group the data as 3 or 2 in the foreach loop while listing the data with PHP. Is it possible to do this?
There are 30 tables in total.
I want to group by 3 by 3 and I want to give "first" class to the first data per group.
Like the picture I want to list.
Sample:
<div class="container">
<?php foreach ($query as $row): ?>
<div class="row">
<div class="post-1 first"></div>
<div class="post-2"></div>
<div class="post-3"></div>
</div>
<?php endforeach; ?>
</div>
First, you pass the data you want to group into array_chunk() and determine the limit, and then we say $key==0 in the first element of each group.
<div class="container">
<?php foreach (array_chunk($query, 3) as $chunk): ?>
<div class="row">
<?php foreach ($chunk as $key => $row): ?>
<div class="post <?php if ($key == 0): ?>first<?php endif; ?>"></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
Related
This question already has answers here:
How can I echo two items per one time by using PHP Foreach?
(2 answers)
How to display two table columns per row in php loop
(4 answers)
Closed 3 years ago.
I have a testimonial carousel. The carousel loop through two item each time. Now I want to get two items each time in a foreach loop. How can I get it?
Code:
<?php foreach ($kiyra_section_meta['testimonials-group'] as $single_testimonial): ?>
<div>
<div class="row">
<div class="col-md-6">
<div class="single-review">
<div class="media">
<div class="media-body">
<h3 class="mt-0"><?php echo esc_html($single_testimonial['client-name']); ?></h3>
<h5><?php echo esc_html($single_testimonial['client-position']); ?></h5>
<i class="fas fa-quote-right fa-5x"></i>
</div>
</div>
<?php echo esc_html($single_testimonial['client-testimonial']); ?>
</div> <!-- /.single-review -->
</div>
</div>
</div>
<?php endforeach; ?>
You can use array_chunk to split the array to group the two items, and then use foreach again:
foreach (array_chunk($input_array, 2) as $group) {
// Start Group
foreach ($group as $item) {
// Item
}
// End group
}
Update with HTML
<div class="owl-carousel">
<?php foreach (array_chunk($input_array, 2) as $group) : ?>
<div class="owl-item">
<?php foreach ($group as $item) : ?>
<div class="item">
<!-- Code of item -->
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
I have 2 tables in database first one is $slide_images and second is $why_us and I am using owl carousel for images.
The $slide_images contains 10 rows.
The $why_us contains 3 rows.
I need each image take one row of $why_us that's mean the first 3 images will have a text from $why_us table.
I tried many ways to do that but it's not given me what I want and I can't edit tables to do that and the id column is not nested to join them with SQL.
Can i solve them with nested foreach?
<div class="home_area">
<!-- start Carousel -->
<div class="owl-carousel block_1">
<?php
if(is_array($slide_image)){
foreach($slide_image as $src){
?>
<div class="overlay-text">
<div class="carousel_item" style="background-image:url('<?php echo base_url(); echo $src->url; ?>');"></div>
<div class="text-layer">
<!--============ start we us ================ -->
<?php if($why_us != ''){ ?>
<div class="we">
<div class="container">
<div class="row">
<?php foreach($why_us as $we){ ?>
<div class="box">
<h6><?php echo $we->name; ?></h6>
<div class="text"><?php echo $we->desc; ?></div>
</div>
<?php }?>
</div>
</div>
</div>
<?php } ?>
<!--============= end we us ========== -->
</div>
</div>
<?php } }?>
</div>
<!-- end Carousel -->
</div>
I hope I described my question well.
You should not use nested loops. That creates a cross product between the arrays.
Instead, you should just have one loop that uses the corresponding elements of both arrays. Since one of the arrays is smaller, you should check that the index exists.
foreach ($slide_image as $index => $src) {
// code that uses `$src` here
if (isset($why_us[$index])) {
$we = $why_us[$index];
// code that uses $we here
}
}
i made this foreach loop but it just doesn't show me anything on the screen doesn anybody see what the problem is?
There're 2 items of the array $data['page']['children']
<?php foreach ($data['page']['childeren'] as $news): ?>
<div class="donerenNews">
<div class="doneren-image">
<img class="group list-group-image doneren-image" src="<?php echo $site_url ?>assets/img/placeholderSubPage.png" alt="">
</div>
<div class="caption doneren-tekst-left">
<h1>
<?php echo $news['title'] ?>
</h1>
<p>
<?php echo $news['description'] ?>
</p>
</div>
</div>
<?php endforeach; ?>
it looks like you put childreren instead of children in
foreach ($data['page']['childeren']
so change it to
foreach ($data['page']['children']
Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.
I would like to output some specific HTML on the third iteration of a loop in PHP. Here is my code:
<?php foreach ($imgArray as $row): ?>
<div class="img_grid"><?= $row ?></div>
<?php endforeach; ?>
On the third iteration of this loop, Instead of displaying:
<div class="img_grid"><?= $row ?></div>
I would like to display:
<div class="img_grid_3"><?= $row ?></div>
I would like to end up with this if my array looped 8 times:
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid_3">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid_3">[some html]</div>
<div class="img_grid">[some html]</div>
<div class="img_grid">[some html]</div>
Thanks
Assuming $imgArray is an array and not an associative array (i.e. it has numeric indices), this is what you want:
<?php foreach($imgArray as $idx => $row): ?>
<?php if($idx % 3 == 2): ?>
<div class="img_grid_3"><?php echo $row; ?></div>
<?php else: ?>
<div class="img_grid"><?php echo $row; ?></div>
<?php endif; ?>
<?php endforeach; ?>
You could tighten it up a bit like this:
<?php foreach($imgArray as $idx => $row):
if($idx % 3 == 2) {
$css_class = 'img_grid_3';
} else {
$css_class = 'img_grid';
}
?>
<div class="<?php echo $css_class; ?>"><?php echo $row; ?></div>
<?php endforeach; ?>
Or even more (some folks would just go with a ternary conditional inline in the HTML), but the law of diminishing returns kicks in eventually with regard to readability. Hopefully this gives you the right idea, though.