php array output data using a loop - php

I'd like to set the following up as a loop (so that the pattern is repeated every 5 elements). I've tried to read up on it and I believe I need to set it up as an array, but I have no idea where to start.
Here's my code:
<div class="ccm-page-list">
<?php
$i= 0;i;
foreach ($pages as $page):
$i++;
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$img = $page->getAttribute('thumbnail');
$thumb = $ih->getThumbnail($img, 550, 550, true);
?>
<a href="<?php echo $url ?>" target="<?php echo $target ?>"> <div class="col-sm-4 grid-item">
<div class="item-img-blog" >
<img src="<?php echo $thumb->src ?>" width="auto" height="100%" alt="" />
<div <?php
if($i == 1) {
?>class="item-class-1" <?php }
if($i == 3) {
?>class="item-class-2" <?php }
if($i == 2) {
?>class="item-class-3" <?php }
if($i == 4) {
?>class="item-class-3" <?php }
if($i == 5) {
?>class="item-class-1" <?php }
?>>
<div class="text-container">
<h1><?php echo $description ?></h1>
<p><?php echo $date = $page->getCollectionDatePublic('d/m/Y'); ?></p>
</div>
</div>
</div>
</div></a>
<?php endforeach; ?>
</div>
I would really appreciate any help! Thanks in advance!

$i= 0;
$classes = array(1 => 1, 2 => 3, 3 => 2, 4 => 3, 5 => 1);
foreach ($pages as $page) {
$i++;
.....
echo 'class="item-class-'.$classes[$i % (count($classes) + 1)];
.....
}

You don't need an extra loop, you can use the modulo operator (aka, the remainder of a division) and some basic math to cope with the fact that you want to cycle 1->5:
$i= 0; // note that I removed the "i;" at the end of this line!
foreach ($pages as $page):
$cyclici = ($i % 5) + 1;
// then, use $cyclici everywhere you want to see that 1-5 1-5 pattern
...
if($cyclici == 1) {
...
// and increment $i at the *end* of the loop instead of the start.
$i++
endforeach;
EDITed to clarify the relationship between $i and $cyclici - $i is still incremented for each iteration of the loop, $cyclici is derived from that incrementing value to obtain the desired 1 2 3 4 5 1 2 3 4 5 ... sequence.

Related

Check If bootstrap last column is even or odd

I have bootstrap column in while loop, what I want is if bootstrap last column is odd then I want column to be 12 (col-12), I found the way to check number even or odd but want to check last number so if last number (last column) is odd I want column to be 12 else remain col-6
I have tried:
<div class"<?php echo ($i == (2 || 4 || 6) )?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
<div class"<?php echo ($i % 2 == 0)?'col-md-6':'col-md-12'; ?>">
display content there in while loop
</div>
but could get the job done...
You make a mistake in writing HTML. But version with modulo must work:
<div class="row">
<?php for ($i = 0; $i < 100; $i++) { ?>
<div class="<?php echo ($i%2 === 0) ? 'col-md-6' : 'col-md-12'; ?>">
display content there in while loop
</div>
<?php } ?>
</div>
solution By m_hutley
Where $i = counter;
$total = acf repeater count;
<div class="<?php echo ($i == $total && $total % 2 !== 0 )?'col-md-12':'col-md-6'; ?>" id="<?php echo $i; ?>">
<h1><?php echo "contain"; ?></h1>
</div>

Conditions with PHP

I'm still pretty new to PHP and trying to figure out an issue I've found on our website at work. In the screenshot, the last div.image-group should be inside the div.image-row that only has two items in it — basically, I always want the div.image-group to be inside a div.image-row, which should hold at most three div.image-group's (but the first div.image-row might contain fewer).
Can anyone point me in the right direction to correct this?
Here's the code:
<?php $counter = 0; ?>
<?php foreach ($rows as $id => $row): ?>
<?php $counter++; ?>
<?php if ($counter % 3 == 1 || $counter === 1) { ?>
<div class="image-row small clearfix">
<?php } ?>
<div class="image-group"><?php print $row; ?></div>
<?php if (($counter != 1 && $counter % 2 == 1) || ($id == count($rows) -1 && $counter % 2 != 1)) { ?>
</div>
<?php } ?>
<?php endforeach; ?>
The code will be simpler if you divide the original array into groups of three at the beginning.
<?php foreach (array_chunk($rows, 3) as $image_row): ?>
<div class="image-row small clearfix">
<?php foreach ($image_row as $image_group): ?>
<div class="image-group"><?= $image_group ?></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The second condition should be:
<?php if ($counter % 3 == 0 || $counter == count($rows)) { ?>
$counter % 3 == 0 is true after every third row, and $counter == count($rows) is true after the last row.

PHP Wordpress Increment Loop

I have this code:
<?php if (types_render_field( "slider-imagine-1", array( 'raw' => true) ) !== "") : ?>
<div class="item">
<img src="<?php echo(types_render_field( 'slider-imagine-1', array( 'raw' => true) )); ?>" alt="">
</div>
<?php else :?> <?php endif; ?>
I want to repeat this code 10 times, and each time it repeats, the number from the variable "slider-imagine-1" increments by 1.
SOLVED:
<?php for($i = 1; $i < 11; $i++) {
// Increment variable by 1
$name = "slider-imagine-".$i;
// Extract incremented variable from WordPress
$variable = (types_render_field($name, array( "raw" => true) ));
// If variable is not empty, do this
if ($variable != "") {
echo '<div class="item">';
echo '<img src="'.$variable.'"></img>';
echo '</div>';
}
} ?>
Try something like that maybe
<?php
for($i = 0; $i < 10; $i++){
$name = "slider-imagine-".$i;
if (types_render_field($name, array( 'raw' => true) ) !== "") : ?>
<div class="item">
<img src="<?php echo(types_render_field($name, array( 'raw' => true) )); ?>" alt="">
...
You Miss to close php tag to rendre the HTML, you need something like that
// Php code in the for loop
if($name !='')
?>
<div>HTML <?php echo $name;?> Continue html<\div>
<?php
// Php code
// If you don't to close php tag
echo ' <div>'. $name .'<\div>';
That only an example on how to mix php and html, this will fit to your needs.

Manipulating a foreach loop

I have the following foreach loop:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php endforeach; ?>
The first 2 items are fine but the items 3-14 I need to wrap in a div so I can control the layout. I am wondering how I would do this? The problem is not all of the 3-14 items will be populated.
Any advice would be welcome ... thanks
You need a control variable to determine which iteration you are at, from there you can add divs or whatever you want to do from the third iteration onward. You could try something like this:
<?php $i = 0;
foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php $i++;
if ($i < 3) { ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php } else { ?>
Add divs, or whatever you want to do here.
<?php }
endforeach; ?>
So with your additions, since the original formula should work, don't mess with that part. Instead make an addition via a $style variable like so:
$i = 1;
foreach ($this->item->extra_fields as $key=>$extraField):
if($i == 1)
$style = 'id="largeImageWrap" class="pull-left"';
elseif($i == 2)
$style = 'id="sidePanelWrap"';
else
$style = 'class="row"';
// If less than or equal to 3, add <div>
if(($i <= 3))
$front = true;
// If greater than 14 <div>
elseif(($i > 14))
$front = true;
// Else no <div>
else
$front = false;
if($front == true) echo "<div $style>"; ?>
<span class="itemExtraFieldsValue"><?php echo $extraField->value; ?></span>
<?php
// If greater than 2 and less than 14 echo blank or </div>
echo (($i > 2) && ($i < 14))? "":"
</div>";
$i++;
endforeach;

Is there an easy way to do 4 at a time in a php foreach loop

I have this php foreach loop
<?php $all_news = $this->db->query("Select * from news"); ?>
<div class="inner">
foreach($all_news as $key=>$news){?>
<div class="news <?php echo ($key%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
But the problem is the $all_news may have 20 results or so but the design only allows me to put for 4 news blocks in each inner div...is there a way make this happen so i have only 4 news divs in each inner div
<?php
$all_news = $this->db->query("Select * from news");
echo '<div class="inner">';
$c = count($all_news);
for($i = 0; $i < $c; $i++){
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $news['title'] ?>
if($i % 4 == 3)
echo '</div><div class="inner">';
}
echo '</div>';
?>
Change your query to only return 4 rows:
SELECT * FROM news LIMIT 4
Alternatively you can change your for-loop.
for($i = 0; $i < min(4, count($all_news)); $i++)
{?>
<div class="news <?php echo ($i%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$i]['title'];
<?}
[edit]
See what you mean now. Create two loops:
<?
$index = 0;
while ($index < count($all_news))
{
$news = $all_news[$index];
?>Start outer div<?
for ($item = 0; $item < 5; $item++)
{
?>Inner div with news item <? echo $news['title'];
}
?>End outer div<?
$index++;
}
You could use two for loops:
<?php $all_news = $this->db->query("Select * from news"); ?>
<?php for($i = 0, $l = count($all_news); $i < $l; $i+=4): ?>
<div class="inner">
<?php for($j = $i; $j < $i+4; $j++): ?>
<div class="news <?php echo ($j%2==1)? 'odd': 'even'?>">
<div class="news_img">....<?php echo $all_news[$j]['title'] ?>
<?php endfor;?>
</div>
<?php endfor;?>
Another option would be array_chunk [docs].
The laziest way would be to just check whether or not you've already done four in the current div on the fly. If you have, close the current div and start a new one:
<div class="inner">
<?php
foreach ($all_news as $key => $news) {
if ($key % 2) {
$oddEven = 'odd';
} else {
$oddEven = 'even';
if ($key && $key % 4 === 0) {
echo '</div><div class="inner">';
}
}
echo "<div class=\"news $oddEven\">";
// ...
}
?>
</div>
Note that this assumes $all_news has an element at 0, so it makes sure that it doesn't close the first, empty div.

Categories