PHP loop need to create new div every 5 elements - php

I've got a forech loop that displays 50 logos. But what I need is another loop that creates a new div(.autogrid_wrapper .cte .block) every 5 images.
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php foreach($this->entries as $entry): ?>
<figure class="image_container">
<img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
<?php endforeach; ?>
</div>
</div>
I hope you guys can help me.

A simple counter could help -
<div class="autogrid_wrapper cte block">
<div class="inner">
<?php
$i = $j = $k = 0;
foreach($this->entries as $entry):
$i++;
$class = '';
if($j === 0) {
$class = 'first';
}
$j++;
$html = '';
if($i % 5 === 0) {
$k++;
$j = ($i - (5 * $k));
$class = 'last';
$html = "</div></div>
<div class='autogrid_wrapper cte block'><div class='inner'>";
}
?>
<figure class="image_container <?php echo $class; ?>">
<img src="<?php echo $entry->field('logo')->generate(); ?>" title="<?php echo $entry->field('name')->value(); ?>" alt="<?php echo $entry->field('name')->value(); ?>" >
</figure>
<?php
echo $html;
endforeach;
?>
</div>
</div>

Related

I want to show more than one photo

<div class="col-lg-6">
<div class="cz-product-gallery">
<div class="cz-preview order-sm-2">
<?php $i = 0;
foreach ($prdGal as $galeri) {
$i++; ?>
<div class="cz-preview-item <?php if ($i == 1) {
echo "active";
} ?>" id="galeri_<?= $galeri->id ?>">
<img class="cz-image-zoom" src="<?= base_url(firstGalleryItem($product->galeri_uniq)) ?>" data-zoom="<?= base_url(firstGalleryItem($product->galeri_uniq)) ?>" alt="<?= $product->urun_ad ?>">
<div class="cz-image-zoom-pane"></div>
</div>
<?php } ?>
</div>
The above code is showing bigphotos
<div class="cz-thumblist order-sm-1">
<?php $i = 0;
foreach ($prdGal as $gal) {
$i++;
$activeClass = $i == 1 ? "active" : "";?>
<a class="cz-thumblist-item <?php if ($i == 1) {
echo "active";
} ?>" href="#galeri_<?= $gal->id ?>">
<img src="<?= base_url(firstGalleryItem($product->galeri_uniq)) ?>"
alt="<?= $product->urun_ad ?>">
</a>
<?php } ?>
</div>
This code shows the small version of the big photos.
The problem here is that even though I upload 2 different photos, I can only take 1 photo. How can I show 2 different photos?

How can I set my for - loop to print images in the HTML - page

I have two images in a folder and their record is in a database so I want to show the images on the page through a for loop. But the problem is that I have classes (left and right) for which I want to define a variable to show images separately, so how can I do. The only problem is with the $val variable.
<div class="col-md-7 wthree-services-bottom-grids">
<?php for ($i = 0; $i < 2; $i++){ $val = ''?>
<div class="wthree-services-<?php echo $val; ?>">
<img src="<?php echo base_url(); ?>uploads/frontend/about_us/<?php echo $about_images[$i]->image; ?>" alt="">
</div>
<?php $val = 'right'; }?>
<!-- <div class="wthree-services-right">
<img src="<?php echo base_url(); ?>uploads/frontend/about_us/<?php echo $about_images[1]->image; ?>" alt="">
</div>-->
<div class="clearfix"> </div>
</div>
I'm not sure if I understand correctly what you're trying to do but does this behave like what you're after?
<div class="col-md-7 wthree-services-bottom-grids">
<?php
for ($i = 0; $i < 2; $i++):
$val = ($i == 0 || $i % 2 == 0 ? 'left': 'right'); ?>
<div class="wthree-services-<?php echo $val; ?>">
<img src="<?php echo base_url(); ?>uploads/frontend/about_us/<?php echo $about_images[$i]->image; ?>" alt="">
</div>
<div class="clearfix"></div>
<?php endfor; ?>

foreach display only first 3 items

We use a foreach code and we want to display only the first 3 items.
But for some reason our code does not work, it currently still display all items.
What am I missing here?
CODE:
<?php $items = $_order->getAllItems(); $i = 0; foreach($items as $i): if($i < 3) {?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $i++; } endforeach;?>
You need to use different variable inside foreach():-
<?php
$items = $_order->getAllItems();
$i = 0;
foreach($items as $itm):
if($i >= 3) {break;}else{?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($itm->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $i++; } endforeach;?>
A much better solution using array_slice():-
<?php
$items = $_order->getAllItems();
$item = array_slice($items, 0, 3); // get first three only
foreach($item as $itm):
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($itm->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php endforeach;?>
Sorry, read the question wrong. Here's the updated answer.
Your foreach iterater was same as the count variable $i
<?php
$items = $_order->getAllItems();
$i = 0;
foreach($items as $item) {
?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /> </div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php
$i++;
if($i == 3) {
break; // because we don't want to continue the loop
}
}
?>
Use for seems like more pretty than foreach:
<?php $items = $_order->getAllItems();
for ($i = 0; $i < count($items) && $i < 3; $i++): ?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($items[$i]->getProductId())->getSmallImageUrl(); ?>"
border="0"/></div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($items[$i]->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php endfor; ?>
if you want to show only 3 items then you should break out of foreach:
if($counter >= 3) break;
else { //rest of the code ...
}
or simply use a for loop instead.
you are resetting your counter $i for every iteration in the loop, use another variable $counter
<?php $items = $_order->getAllItems(); $counter = 0; foreach($items as $i): if($counter < 3) {?>
<li class="order-row-item">
<div class="order-row-product">
<div class="order-row-product-image">
<img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /></div>
<div class="order-row-product-name">
<?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
</div>
</div>
</li>
<?php $counter++; } endforeach;?>
$count='1';
for ( $i = 0; $i <= 100; $i++ ) {
if ( $var[$i] == "value" ) {
print $i.'-'.$var[$i] . "<br>"; // print $i to display row number
if ( $count++ >= 3 ) {
break;
}else{
// have a cup of coffee ;)
}
}

PHP while loop, display in pairs of two

In Wordpress, I am creating a slider to slide through some content that the user specify. I am using ACF (advanced custom fields) if anyone is familiar with that, however what I am trying to accomplish is to show two content items at a time, while sliding through the jQuery slider.
Here is my loop:
<?php while(has_sub_field('popular_topic')): ?>
<li>
<div class="slide">
<img src="<?php the_sub_field('popular_topic_image'); ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
echo '<p>'.$newContent.'</p>';
?>
<a class="more-arrow" href="<?php the_sub_field('popular_topic_link'); ?>">Read More</a>
</div>
</div>
</li>
<?php endwhile; ?>
This is currently working, however it only shows one slide. I want it to show two slides and a time. Is there something I can do with a counter? Does this make sense?
my syntax might be a little off but maybe give this a shot?
<?php $i = 0; ?>
<?php while(has_sub_field('popular_topic')): ?>
<?php
$len = 60;
$popularTopicTitle = get_sub_field('popular_topic_title');
$newContent = substr($popularTopicTitle, 0, $len);
if(strlen($newContent) < strlen($popularTopicTitle)) {
$newContent = $newContent.'...';
}
$contentVar[$i] = array (
'img' => the_sub_field('popular_topic_image'),
'title' => $newContent,
'link' => the_sub_field('popular_topic_link')
);
$i++;
?>
<?php endwhile; ?>
<?php $j = 0; ?>
<?php while ($j < $i) : ?>
<li>
<div class="slide">
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php $j++; ?>
<?php if ($j <= $i) : ?>
<img src="<?php echo $contentVar[$j]['img']; ?>" alt="" />
<div class="img-wrapper"></div>
<div class="slider-content">
<p><?php echo $contentVar[$j]['title']; ?></p>
<a class="more-arrow" href="<?php echo $contentVar[$j]['link']; ?>">Read More</a>
</div>
<?php endif; ?>
</div>
</li>
<?php $j++; ?>
<?php endwhile; ?>

Output reverse count foreach loop php

I have the follow foreach loop:
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=1;
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count++; }; ?>
I need help getting the count to reverse. The output I am looking for is as follows:
<div id="slide4"></div>
<div id="slide3"></div>
<div id="slide2"></div>
<div id="slide1"></div>
Any help with this would be great.
Change $count=1; to $count = sizeof($arrayreversed);
and $count++; to $count--;
First of all get the amount of keys in the array and assign it as $count, like this:
$count = count($arrayreversed);
Then replace the $count++ with $count-- and you're all set.
How about using the size of the array as your initial count and then subtracting 1 each iteration?
<?php $arrayforward = get_field('background_slider');
$arrayreversed = array_reverse($arrayforward);
$count=count($arrayreversed);
foreach($arrayreversed as $subarray) {
$subfield1 = $subarray['background_image'];
$subfield2 = $subarray['background_image_alt'];
$subfield3 = $subarray['text_image'];
$subfield4 = $subarray['text_image_alt']; ?>
<div id="slide<?php echo $count; ?>" class="contentslider_wrapper">
<div class="fullpageimage">
<img id="slideImg<?php echo $count; ?>" src="<?php echo $subfield1; ?>" alt="<?php the_sub_field('background_image_alt'); ?>" />
<img id="textImg<?php echo $count; ?>" class="slidetext" src="<?php echo $subfield2; ?>" alt="<?php the_sub_field('text_image_alt'); ?>" />
</div>
</div>
<?php $count--; }; ?>

Categories