Conditions with PHP - 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.

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>

How Can I fillup bootstrap grid using foreach?

As I am trying to make bootstrap grid structure using foreach two column and inside it's one row and two rows structure. Attached a screen shot for better to better view.
Note: Not issue with CSS but first three items are goes in grid as picture.
Counter is not taking col-md-4 and not sure where to use Counter increment counter++? What if I want to show first three items using foreach?
Any other way to workaround on this layout? Tried so far as below
<?php $counter = 0;
foreach($view->result as $result) {
<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php if( $counter == 1 || $counter == 2 ) { ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter == 1){ ?>
<div class="post post-thumb">
// second element common body
</div>
</php if ( $counter == 2){
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php counter++; } } ?>
</div> //closing row
<?php } ?> //closing foreach
There are couple of syntax errors in your code, such as:
Opening and closing braces are not in sync i.e. foreach loop and if block(s) are not closed properly
counter++; should be $counter++;
Plus, there are few logical errors as well. Change your code in the following way,
<?php
$counter = 0;
foreach($view->result as $result) {
?>
<div id="hot-post" class="row hot-post">
<?php if( $counter == 0 || $counter % 3 == 0 ) { ?>
<div class="col-md-8 hot-post-left">
<div class="post post-thumb">
// first element common body
</div>
</div>
<?php } ?>
<?php if( $counter != 0 && $counter % 3 != 0 ) { ?>
<div class="col-md-4 hot-post-right">
<?php if( $counter % 3 == 1){ ?>
<div class="post post-thumb">
// second element common body
</div>
<?php } ?>
<?php if ( $counter % 3 == 2){ ?>
<div class="post post-thumb">
// third element common body
</div>
</div>
<?php } ?>
</div>
<?php
}
++$counter;
}
?>

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;

PHP foreach loop mark up

I have a PHP foreach loop which outputs a series of items.
What I want to achieve is some code (a div) wrapped around items 1 & 2 and also wrapped around 3 & the very last item.
Here is the PHP:
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo JHTML::_('content.prepare', $extraField->value); ?></span>
<div class="clearfix"></div>
</div>
<?php $counter++; endforeach; ?>
Any ideas on how I could achieve this?
Many thanks in advance.
Maybe something like this, forgive me my PHP is a bit rusty.
<?php foreach ($this->item->extra_fields as $key=>$extraField): ?>
<?php
$len = count($this->item->extra_fields);
if ($key == 1 || $key == 2 || $key == 3 || $key == len) {
echo "<div>";
}
?>
<div class="<?php echo ($key%2) ? "odd" : "even"; ?> type<?php echo ucfirst($extraField->type); ?> group<?php echo $extraField->group; ?>">
<span class="itemExtraFieldsValue"><?php echo JHTML::_('content.prepare', $extraField->value); ?></span>
<div class="clearfix"></div>
</div>
<?php
if ($key == 1 || $key == 2 || $key == 3 || $key == len) {
echo "</div>" ;
}
?>
<?php $counter++; endforeach; ?>
Might have to account for the length of the list, so your last check might be len + 1, and your first 3 checks might be (0, 1, and 2), it depends on what the $key value is.
It's a bit confusing what the $counter++ part of your code does in a foreach, perhaps you should be using that for your modulus and in place of the key.
Regardless it's difficult to code up a solution as I believe your code snippet is incomplete, and it would also be helpful to know what the data is that we are looping through and the intended output of just having the first 3 and the last item wrapped in a div.

php array output data using a loop

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.

Categories