I have an array of items and want to loop through them but instead of grouping them inside of a div I would like to group them by css Class.
I dont want to do div grouping because of mobile views there might be 4 or more in line and the breaks css class will change as the screen size changes or will be moved to absolute positioning removing the break all together and forming a grid.
I can create the breaks every 3 items but the class assign im struggling with.
Any help would be appreciated.
I would like to create the below layout in a PHP foreach loop.
ex.
<div class="listing">
<div class="group_1">item1</div>
<div class="group_1">item2</div>
<div class="group_1">item3</div>
<div class="break_1"></div>
<div class="group_2">item4</div>
<div class="group_2">item5</div>
<div class="group_2">item6</div>
<div class="break_2"></div>
<div class="group_3">item7</div>
<div class="group_3">item8</div>
<div class="group_3">item9</div>
<div class="break_3"></div>
<div class="group_4">item10</div>
<div class="group_4">item11</div>
<div class="group_4">item12</div>
<div class="break_4"></div>
</div>
Not very sure if it is what you are looking, anyway, given a basic array like this:
$arr=array(1,2,3,4,5,6,7,8,9,10,11,12);
you can loop it and have the code you expect this way:
$group_num=1;
echo "<div class='listing'>";
foreach ($arr as $key=>$item) {
echo "<div class='group_$group_num'>$item</div>";
if ($key%3==0) {
echo "<div class='break'></div>";
$group_num++;
}
}
echo "</div>";
Results:
<div class='listing'>
<div class='group_1'>1</div>
<div class='group_1'>2</div>
<div class='group_1'>3</div>
<div class='break'></div>
<div class='group_2'>4</div>
<div class='group_2'>5</div>
<div class='group_2'>6</div>
<div class='break'></div>
<div class='group_3'>7</div>
<div class='group_3'>8</div>
<div class='group_3'>9</div>
<div class='break'></div>
<div class='group_4'>10</div>
<div class='group_4'>11</div>
<div class='group_4'>12</div>
<div class='break'></div>
if we have array of items as :
$items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8'];
$result = '';
foreach($items as $index => $item) {
$groupNumber = intval(floor($index/3)) + 1;
$result .= "<div class='group_$groupNumber'>$item</div>";
}
echo $result;
and this is a sample output
Thanks to the help of Giacomo Pittalis and Hassan Ali Salem I have managed to solve this in a bit of a crude way.
<?php
$items = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9', 'item10'];
$i = 0;
$len = count($items);
foreach ($items as $item) {
$mod = intval(floor($i / 3) + 1);
echo '<div class="' . 'group_' . $mod . '">' . $items[$i] . '</div>';
if (($i % 3 == 2)) { ?>
<div class="content_holder hidden" id="group_<?php echo $mod; ?>"></div>
<?php }
if ($i == $len - 1) { ?>
<div class="content_holder hidden" id="group_<?php echo $mod; ?>"></div>
<?php }
$i++;
}
?>
Related
I have an array with items: $array = ['item 1','item 2','item 3','item 4','item 5','item 6','item 7']
I am looking for the following HTML:
<div class="big">Item 1</div>
<div>
<div class="small">Item 2</div>
<div class="small">Item 3</div>
</div>
<div class="big">Item 4</div>
<div>
<div class="small">Item 5</div>
<div class="small">Item 6</div>
</div>
<div class="big">Item 7</div>
In text: Every 3rd item (including the first) have to have their own div, while the 2nd and 3rd items get a wrapping div.
I've come up with the following loop, but I'm pretty sure it's not going to work in the long run, since it'll probably end the loop with an opening div, since I keep on opening a new one after each BIG ITEM.
$i = 0;
foreach($array as $item) {
<?php
if($i % 3 == 0) {
if($i == 0) {?>
<div class="big">
Item <?= $i; ?>
</div>
<div>
<?php } else { ?>
</div>
<div class="big">
Item <?= $i; ?>
</div>
<div>
<?php } ?>
<?php } else { ?>
<div class="small">
small item
<?= $i; ?>
</div>
<?php } ?>
<?php
$i++;
}
I feel like I'm close, but it also feels wrong to start and close divs in $is where it doesn't belong. Maybe the only thing I'm missing is checking if $i equals the length of the array, and then don't open a new <div> after a big item?
You can use simple if-elseif-else construct to print the divs.
If the index of the current element % 3 is 0, it is a big div, else it is a small div.
If the index of the current element % 3 is greater than 0, it is a small div.
If it is 1, prepend an opening div tag.
If it is 2, append a closing div tag.
Snippet:
<?php
foreach ($array as $idx => $set) {
$mod = $idx % 3;
if($mod === 0){
echo "<div class='big'>$set</div>";
}elseif($mod === 1){
echo "<div><div class='small'>$set</div>";
}else{// if $mod is 2
echo "<div class='small'>$set</div></div>";
}
}
Online Demo
Alternatively, use array_chunk then pick out the first item with array_shift, then join the remaining.
<?php
$array = array_chunk(['item 1','item 2','item 3','item 4','item 5','item 6','item 7'], 3);
foreach ($array as $set) {
echo '<div class="big">'.array_shift($set).'</div>';
if (count($set)) echo '<div><div class="small">'.implode('</div><div class="small">', $set).'</div></div>';
}
?>
Result
<div class="big">item 1</div>
<div>
<div class="small">item 2</div>
<div class="small">item 3</div>
</div>
<div class="big">item 4</div>
<div>
<div class="small">item 5</div>
<div class="small">item 6</div>
</div>
<div class="big">item 7</div>
Example: https://3v4l.org/URB3D
Here you are my simple answer: I think you don't need to use additional counter in case that interpreter adds indexes to array:
$array = ['item 1','item 2','item 3','item 4','item 5','item 6','item 7'];
foreach($array as $key=>$da){
$a = $key % 3;
if($a === 0){
echo "<div class=\"big\">" . $da . "</div>\n";
}
else{
if($a === 1)
echo "<div>\n";
echo " <div class=\"small\">" . $da . "</div>\n";
if($a === 2)
echo "<div>\n";
}
}
I have an ACF Repeater field i'd like to output as an accordion grid, like so:
<div class="intro row">
<div class="item item-1">name 1</div>
<div class="item item-2">name 2</div>
<div class="item item-3">name 3</div>
<div class="item item-4">name 4</div>
</div>
<div class="expanded row">
<div class="expand" id="item-1">expanded info 1</div>
<div class="expand" id="item-2">expanded info 2</div>
<div class="expand" id="item-3">expanded info 3</div>
<div class="expand" id="item-4">expanded info 4</div>
</div>
<div class="intro row">
<div class="item item-5">name 5</div>
<div class="item item-6">name 6</div>
<div class="item item-7">name 7</div>
<div class="item item-8">name 8</div>
</div>
<div class="expanded row">
<div class="expand" id="item-5">expanded info 5</div>
<div class="expand" id="item-6">expanded info 6</div>
<div class="expand" id="item-7">expanded info 7</div>
<div class="expand" id="item-8">expanded info 8</div>
</div>
I can group the initial row fine, it's just the second "expanded" row i'm having trouble with. How can I repeat and group the second row of 4 correctly in the same loop? My current PHP:
<?php // check if the repeater field has rows of data
if( have_rows('features') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
while ( have_rows('features') ) : the_row();
$name = get_sub_field('feature_name');
$expandedInfo = get_sub_field('feature_info');
if ($count % 4 == 0) {
$group++;
?>
<div class="intro row">
<?php
}
?>
<div class="item item-<?php echo $count; ?>">
<?php echo $name ?>
</div><!-- item-->
<?php
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<?php
}
$count++;
endwhile;
else :
// no rows found
endif;
?>
The second 'expanded' row can be done so that you store each count (item-1,item-2) in an array or just traverse through all the count when you close the intro row.
<?php
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<div class="expanded row">
<?php
$start = $count-3;
// if $count is 4, $start will be 1, and the $i will go to 4
// if $count is 8, $start will be 5
for($i=$start;$i<=$count;$i++){
echo '<div class="expand" id="item-' . $i . '"></div>';
} ?>
</div>
<?php
}
This is just an example. I would suggest you to store each $count in an array and then use the count($array) to get the number of them. After you have traversed the array, reset it.
The Array Approach
<?php // check if the repeater field has rows of data
if( have_rows('features') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
// Content Array
$content_array = array();
while ( have_rows('features') ) : the_row();
$name = get_sub_field('feature_name');
$expandedInfo = get_sub_field('feature_info');
// Adding the Expanded Info
$content_array[ 'item-' . $count ] = $expandedInfo;
if ($count % 4 == 0) {
$group++;
?>
<div class="intro row">
<?php
}
?>
<div class="item item-<?php echo $count; ?>">
<?php echo $name ?>
</div><!-- item-->
<?php
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<div class="expanded row">
<?php
foreach( $content_array as $item_id => $expanded_info ) {
echo '<div class="expanded" id="' . $item_id . '">';
echo $expanded_info;
echo '</div>';
} ?>
</div>
<?php
// Resetting the Array
$content_array = array();
}
$count++;
endwhile;
else :
// no rows found
endif;
?>
okey, lets see ,
using variables to store your templates may helps a lot in this context ,
as follow :
$intro = '';
$expanded = '';
while (have_rows('features')) :
the_row();
if ($count % 4 == 0) {
$group++;
$intro .= '<div class="intro row">';
$expanded .= '<div class="expanded row">';
}
$intro .= '<div class="item item-' . $count . '"></div><!-- item-->';
$expanded .= '<div class="expand" id="item-' . $count . '"></div>';
if ($count % 4 == 3) {
$intro = '</div><!-- intro-->';
$expanded = '</div><!-- intro-->';
}
$count++;
endwhile;
I've made a quick example to explain to show you how using variables may fix your issue : https://3v4l.org/cKPP4
Can't test it right now, but I think something like that should work.
<?php // check if the repeater field has rows of data
if( have_rows('features') ):
// loop through the rows of data
// add a counter
$count = 0;
$group = 0;
$array = array();
while ( have_rows('features') ) : the_row();
$name = get_sub_field('feature_name');
$expandedInfo = get_sub_field('feature_info');
if ($count % 4 == 0) {
$group++;
?>
<div class="intro row">
<?php
}
?>
<div class="item item-<?php echo $count; ?>">
<?php echo $name ?>
</div><!-- item-->
<?php
array_push($array, $expandedInfo);
if ($count % 4 == 3) {
?>
</div><!-- intro-->
<div class="expanded row">
<?php
for ($i=0; $i < count($array); $i++) {
echo '<div class="expand" id="item-'.$i + 1.'">'.$array[$i].'</div>';
}
echo '</div>';
}
$count++;
endwhile;
else :
// no rows found
endif;
?>
I need a little help with a php loop for OpenCart.
I need to do is wrap a div around the output of the data every 4 loops.
I have the following
<?php foreach ($categories as $category) { ?>
<div class="col-lg-3 col-md-3">.....</div>
<?php } ?>
I get this
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
But what I'm hoping to get is
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
<div class="col-lg-3 col-md-3">.....</div>
</div>
Any help would be greatly appreciated :)
try this
<?php
$i=0;
$wrap_count = 4; // you can change this no of divs to wrap
foreach ($categories as $category)
{
$i+=1;
if($i%$wrap_count==1)
{
echo '<div class="row">';
}
?>
<div class="col-lg-3 col-md-3">.....</div>
<?php
if($i%$wrap_count==0)
{
echo '</div>';
}
}
if($i%$wrap_count!=0)
{
echo '</div>';
}
?>
<?php
$i = 0;
foreach ($categories as $category) {
if (($i % 4) === 0) {
echo "<div class='row'>";
}
echo '<div class="col-lg-3 col-md-3"></div>';
if (($i % 4) === 3) {
echo "</div>";
}
$i++;
}
?>
Ps, If $categories is a non keyed array, ie array('a', 'b', 'c') then you can get rid of the i = 0; and i++; and change the foreach to foreach ($categories as $i => $category) {
And as zerkms mentioned in a comment, the magic here is coming from % (Modulo operation), which is essentially like a clock- the numbers will increment from 0 upwards until they hit 12, at which point it is reset to 0 again. So the clock is said to be modulo twelve. In this instance we're using modulo four to cyclically print our our opening and closing row elements.
Modulo four would be the sequence 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, etc
Try this :-
<div class="row">
<?php
$i = 1;
foreach ($categories as $category) { ?>
<div class="col-lg-3 col-md-3">.....</div>
<?php if ($i % 4 == 0){ ?>
</div><div class="row">
<?php } ?>
<?php $i++; ?>
<?php } ?>
You may use a counter variable. Initially set it to equal 0. Then for each loop check its value. If it is equal to 0 then output <div class="row"> and add 1 to the variable's value. If its value is > 0 and < 5 then output <div class="col-lg-3 col-md-3">.....</div> and add 1 to it's value. If it is equal to 5 output </div> and reset the counter variable to 0.
Something like (code tested):
<?php
$counter = 0;
foreach ($categories as $category) {
If ($counter == 0) {
echo '<div class="row">
<div class="col-lg-3 col-md-3">.....</div>
';
$counter++;
} elseif ($counter > 0 && $counter < 3) {
echo '<div class="col-lg-3 col-md-3">.....</div>
';
$counter++;
} elseif ($counter == 3) {
echo '<div class="col-lg-3 col-md-3">.....</div>
</div>
';
$counter = 0;
}
}
if ($counter > 0) {
echo '</div>
';
}
?>
The Modulo operator will be equal to zero when used in a comparison with the number of items you would like in a row. So you can output your starting <div> there.
To add a closing div you will need to check the modulo of 4 is equal to 3 or if we have reached the total number of items, to prevent a missing close </div>.
<?php
// set a counter
$rowCount = 0;
// store the total number of categories minus 1 as we will be
// counting from 0 with the counter
$categoryTotal = count($categories) - 1;
foreach($categories as $c){
if($rowCount % 4 == 0){
echo '<div class="row">';
}?>
<div class="col-lg-3 col-md-3""></div>
<?php
// add your closing div if it is the end of a row or if there are no more items
if($rowCount % 4 == 3 || $rowCount == $categoryTotal){
echo '</div>';
}
// increment your counter
$rowCount++;
} ?>
I have an alternative option that might work depending on your array.
$new_array = array_chunk($your_array, 4);
foreach($new_array as $group_of_$four){
echo '<div class="row">';
foreach($group_of_four as $one){
// Single Item
}
echo '</div>';
}
Well, add this to your code:
//before foreach
$i = 0;
// inside the foreach before its closing }
$i++;
}//this closes the foreach
And then before adding any div (probably in the beginning of the foreach:
if($i % 4 == 0)
{
echo "<div class='each-four-iterations'>";
}
And before the $i++ statement, add this :
if($i % 4 == 0)
{
echo "</div>";
}
<?php
$wrap_count = 2; // you can change this no of divs to wrap
foreach ($categories as $category) :
$i+=1;
if($i%$wrap_count==1):
echo '<div class="row">';
endif;?>
<div class="col-lg-3 col-md-3">.....</div>
if($i%$wrap_count==0) : echo '</div>'; endif; endforeach;
?>
Works Fine For me
Try this one. It works for me.
<?php $count = 1; ?>
<?php foreach ($model->userProfile->portfolio as $item): ?>
<?php if ($count % 4 == 1): ?>
<div class="row" style="margin-bottom: 30px;">
<?php endif ?>
<div class="col-md-3">
</div>
<?php if ($count % 4 == 0): ?>
</div>
<?php endif ?>
<?php $count++ ?>
<?php endforeach ?>
Hi I am using Bootstrap 2 with a PHP content management system.
I am rendering 12 items from the database, each 3 items needs to be wrapped in a row. However I am unable to achieve this my last attempt is below (with simplified markup):
$i = 1;
echo "<div class='row-fluid'>";
foreach($posts as $p) {
if ($i % 3 == 0) {
echo "</div>";
}
if ($i % 4 == 0) {
echo "<div class='row-fluid'>";
}
echo "<div class='span4'><h5>$p->title</h5></div>";
$i++;
}
In affect what I am looking for is something like this:
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
<div class="row">
<div class="item></item>
<div class="item"</item>
<div class="item"></item>
</div>
I have tried everything I can think of any help would be great thanks.
Try if there are 12 rows :
echo "<div class='row-fluid'>";
foreach($posts as $p) {
echo "<div class='span4'><h5>$p->title</h5></div>";
if ($i % 3 == 0) {
echo "</div>";
echo ( $i< 12 )? "<div class='row-fluid'>" : "";
}
$i++;
}
I think this will work
$i = 0;
echo "<div class='row-fluid'>";
foreach($posts as $p) {
echo "<div class='span4'><h5>$p->title</h5></div>";
if ($i % 3 == 0) {
echo "</div><div class='row-fluid'>";
}
$i++;
}
echo "</div>";
I want to create a row if columns greater than 3 using PHP loop because i am using wordpress
My code is here
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
If columns are greater than 3, then it should create a new row like this
<div class="row">
<div class="column">column1</div>
<div class="column">column2</div>
<div class="column">column3</div>
</div>
<div class="row">
<div class="column">column1</div>
</div>
Any help will be highly appreciated.
Thanks in advance
Sure - just use modulus:
<?php
$elements = array('foo', 'bar', 'rab', 'oof');
echo '<div class="row">';
foreach ($elements as $i => $element) {
if ($i > 0 && $i % 3 == 0) {
echo '</div><div class="row">';
}
echo '<div class="column">' . $element . '</div>';
}
echo '</div>';
?>
DEMO
Output:
<div class="row">
<div class="column">foo</div>
<div class="column">bar</div>
<div class="column">rab</div>
</div>
<div class="row">
<div class="column">oof</div>
</div>
You need something like this:
<?
echo '<div class="row">';
for ($i=0; $i<15;$i++){
if ($i%3 == 0 && $i != 0){
echo '</div><div class="row">';
}
echo '<div class="column">column '.($i+1).'</div>';
}
echo '</div>';
?>
WORKING CODE
There is alternative solution with function.
<?php
// Managing the Code -- Functions: Handling a Variable Number of Parameters
// building a row of 10 <td> columns with a variable number of items
function preferencesRow()
{
// initialize $output
$output = '';
// use "func_get_args()" to collect all parameters into an array
$params = func_get_args();
// used to make sure 10 columns are filled
$maxCols = 10;
// loop through the parameters
foreach ($params as $item) {
$output .= "<td width='80px' align='center'>$item</td>\n";
$maxCols--;
}
// fill in the rest of the row with empty columns
for ($x = 0; $x < $maxCols; $x++) {
$output .= "<td width='80px'> </td>\n";
}
return $output;
}
// NOTE: you can use "." or "," with echo
echo '<h1>Preferences</h1><hr />' . PHP_EOL;
echo '<table border=1>' . PHP_EOL;
echo '<tr><th>Tarzan</th>';
echo preferencesRow('Africa', 'jungles', 'tantor', 'mangani', 'cabin in the woods',
'hunting', 'swinging in trees', 'Jane');
echo '</tr>';
echo '<tr><th>Jane</th>';
echo preferencesRow('London', 'parties', 'dancing', 'social events', 'lavish estates');
echo '</tr>';
echo '</table>' . PHP_EOL;