Apologies if this has already been asked (I cannot find an answer) but I am using PHP and I am building a slider, but would like two images per slide, not one. So, in theory the foreach() needs to include two per each.
An example of the setup is as follows:
<?php foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php endforeach; ?>
I was thinking I could do something like a count...
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
<li></li>
<?php endif; ?>
<?php endforeach; ?>
But I got a little confused as this would insert something every 2... not include two of whatever the foreach loop is fetching at once.
Hope this makes sense and thanks in advance
Why so complicated? Use a simple for loop instead:
<?php for ($i=0; $i<count($page->images)-1; $i+=2) { ?>
<img src="<?php echo $page->images[$i]->url; ?>"/>
<img src="<?php echo $page->images[$i+1]->url; ?>"/>
<?php } ?>
Or even more elegant, a do/while loop:
<?php $i=0; do { ?>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<img src="<?php echo $page->images[$i++]->url; ?>"/>
<?php } while ($i<count($page->images)) ?>
Compared to using a foreach loop these approaches have another advantage: you do not create copies of all objects. This can make a huge difference if those objects are non-trivial.
Okay, I managed to work this out... hopefully it will help.
<div class="each-slide">
<?php $index = 0; foreach ($page->images as $image) : ?>
<img src="<?php echo $image->url; ?>"/>
<?php $index++; ?>
<?php if ( $index % 2 == 0 && $index !=count($page->images) ) : ?>
</div><div class="each-slide">
<?php endif; ?>
<?php endforeach; ?>
</div>
Related
So I have this code I want to repeat. Uses wordpress acf to generate the image:
`<div class="row">
<?php if( get_field('image_1') ): ?>
<img class="this-image" src="<?php the_field('image_1'); ?>" />
<?php endif; ?>
</div>`
I'm just wondering how I can loop this in with which I would also need to increment the numbers (ie. image_1, image_2, image_3).
I'm having trouble figuring out the logic of PHP, if there already is a similar post, a kind link would be much appreciated, thanks!
Use below code - it is using for loop to iterate through all images from image_1 to image_X (where X can be defined by you). similar code can be modified by add an array to store all the image names, just in case if there is no patterns for the image names.
<div class="row">
<?php $numImages = 10; #Define how many images you have
for ($i=1; $i<= $numImages; $i++){ #starting from image_1 ?>
<?php if( get_field('image_' . $i)): ?>
<img class="this-image" src="<?php the_field('image_' . $i); ?>" />
<?php endif; ?>
<?php } ?>
</div>
You can try something like this
`<div class="row">
<?php
for (i=1; i<=3; i++) {
if( get_field('image_'.i) ): ?>
<img class="this-image" src="<?php the_field('image_'.i); ?>" />
<?php endif; } ?>
</div>`
I'm trying to figure this out for hours but I can't. I just want to show the second post and skip the first one, just like the Wordpress offset function. I'm using AW blog extension for Magento and I want to skip the first post in the recent blog posts. Below is my modified code showing one post recent post in a homepage block. I just want to create another block that will show the second recent post. :(
<?php $posts = $this->getPosts(); ?>
<div id="messages_product_view">
<?php Mage::app()->getLayout()->getMessagesBlock()-> setMessages(Mage::getSingleton('customer/session')->getMessages(true)); ?>
<?php echo Mage::app()->getLayout()->getMessagesBlock()->getGroupedHtml(); ?>
</div>
<?php
foreach ($posts as $post):
if ($i++ >= 1) break;
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php endforeach; ?>
I will recommend you to check the count and after printing it stop the loop, otherwise it will loop the entire array to the end and print just the second iteration.
$i=0;
foreach ($posts as $post){
if ($i==1) {
//print your div here
$i++; //added here after edit.
continue;
}else if ( $i>1 ){
break;
}
$i++;
}
This way it will only iterate twice.
Try changing:
foreach ($posts as $post):
if ($i++ >= 1) break;
to:
//set $i outside of loop
$i=0;
foreach ($posts as $post):
if ($i==0) {
$i++;
//skip the first record
continue;
}
Try this -
<?php $i=1;
foreach ($posts as $post):
if ($i > 1) {
?>
<div class="postWrapper">
<div class="postTitle">
<h3><a href="<?php echo $post->getAddress(); ?>" ><?php echo $post->getTitle(); ?></a></h3>
</div>
<div class="postContent"><?php echo $post->getPostContent(); ?></div>
</div>
<?php }
$i++;
endforeach; ?>
i have to modify this code, to echo every 4 thumbs (extraimage) inside a div...
I searched on Stackoverflow but all answers talk about setting a counter, i want to avoid this (if possible) using a counter that is already counting the extraimages.. i think it most be as easy as a conditional
if counter extraimages==3 echo div.. but how do i go back to 0 ,, or maybe i am missunderstanding the way to do this..
This is the part of the code where the array is set and the for each is set.
<?php if($extraimagecount >0){?>
<?php foreach ($extraimage as $key=>$value){?>
<?php }?>
<?php }?>
<a href="<?php echo DATA_DIR."/".$id."/".$this->get_variable('firstimage');?>" >
<img src="<?php echo DATA_DIR."/".$id."/".$this->get_variable('firstimage');?>" class="minis"/>
</a>
<?php if($extraimagecount >0){
$rotate=1;
$tumppr=0;
?>
<?php foreach ($extraimage as $key=>$value){
$rotate=$rotate+1;
?>
<a href="<?php echo DATA_DIR."/".$id."/".$value['image'];?>" >
<img src="<?php echo DATA_DIR."/".$id."/t_".$value['image'];?>" class="minis"/>
</a>
<?php
if($rotate==8)
{
$rotate=0;
$tumppr=$tumppr+1;
?>
<?php
}
?>
<?php }?>
<?php }?>
</div>
<?php
$lftstr="";
$rgtstr="";
if($extraimagecount >0)
{
$extcnt=count($extraimage);
$extcntnew=$extcnt+1;
$extdivide=intval(($extcntnew/8));
$extmode=($extcntnew % 8);
for($i=0;$i<$extdivide;$i++) //************ For Right Arrow ***************/
{
?>
<div id="rgt_<?php echo $i;?>" class="rgt" <?php if($i >0 || $extmode ==0){?>style="display: none;"<?php }?> ><img class="rgtimg" src="images/rnext.png"></div>
<?php
$rgtstr=$rgtstr.$i.'_';
}
for($ii=1;$ii<$extdivide;$ii++) //************ For left Arrow ***************/
{
?>
<div id="lft_<?php echo $ii;?>" class="lft" style="display: none;" ><img class="lftimg" src="images/lnext.png"></div>
<?php
$lftstr=$lftstr.$ii.'_';
}
if($extmode >0)
{
?>
<div id="lft_<?php echo $extdivide;?>" class="lft" style="display: none;" ><img class="lftimg" src="images/lnext.png"></div>
<?php
$lftstr=$lftstr.$extdivide.'_';
}
}
?>
Its actually quite easy, use the % operator. a%b will return the remainder of a/b. heres how you use it
for($i=0;$i<9;$i++)
{
echo $i%3." ";
}
this will print out
0 1 2 0 1 2 0 1 2
You can then use this to create groups of 4 in your case.
I am looking to offset a group of PHP array results to create multiple rows of data.
For example, the first row will contain the first four array results, with the second row displaying results 5-8 and the third row containing results 9-12.
Currently, I am printing out all 12 in one list, however I'd like a bit more display control (i.e. ordering the results into columns which I can style independent of each), hence why I'd like to offset the results.
My current PHP is below:
<?php
if (empty($tmdb_cast['cast'])) {
} else {?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body"><?php
foreach($tmdb_cast['cast'] as $key => $castMember){
if ($key < 12) {?>
<li class="actor_instance clearfix"><?php
if ($castMember['profile_path'] != '') {?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" /><?php
} else {?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" /><?php
}?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li><?php
}
}?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section><?php
}?>
P.S. Apologies for how I've formatted the code in the above block - I'm still not 100% sure how to make it look "nice" on StackOverflow.
Use array_chunk to break a single dimension array into a 2D array. Then you can loop through each chunk, and then each result, to get that "offset" effect between them.
$chunks = array_chunk($tmdb_cast['cast'], 4); // 4 here, is the number you want each group to have
foreach($chunks as $key => $chunk)
{
foreach($chunk as $castMember)
{
//display castMember here
}
// add code between groups of 4 cast members here
}
First: mixing php and html this way is a very bad habbit... one day you will have to maintain your code and that day you will vomit all over your desk because you mixed different languages in one file...
That being said..
and without creating a new array:
foreach($tmdb_cast['cast'] as $key => $castMember)
{
if ($key < 12)
{
// your code goes here
// if key is 3, or 7 the code below will close the listcontainer and open a new one...
if( ($key+1)%4 == 0 AND $key <11)
echo '</ul> <ul class="section_body">';
}
}
Also replace this:
if (empty($tmdb_cast['cast']))
{
}
else {
with this:
if (!empty($tmdb_cast['cast']))
{
I'm not entirely sure what you're looking for but here's how I would format your code. Alternative syntax for 'if' is a little more readable, and the use of for loops instead of a foreach will give you the control over row numbers you say you're looking for.
<?php if( ! empty($tmdb_cast['cast'])): ?>
<section class="cast">
<p class="title">Cast</p>
<ul class="section_body">
<?php
for($i = 0; $i < 12; $i++):
$castMember = $tmdb_cast['cast'][$i];
?>
<li class="actor_instance clearfix">
<?php if($castMember['profile_path'] != ''): ?>
<img src="http://cf2.imgobject.com/t/p/w45<?php echo $castMember['profile_path']; ?>" class="actor_image pull-left" alt="<?php echo $castMember['name']; ?>" title="<?php echo $castMember['name']; ?>" />
<?php else: ?>
<img src="assets/images/castpic_unavailable.png" class="actor_image pull-left" alt="No image available" title="No image available" />
<?php endif; ?>
<p class="actor"><?php echo $castMember['character']; ?> - <?php echo $castMember['name']; ?></p>
</li>
<?php endfor; ?>
<div class="morecast pull-right">[...view all cast]</div>
</ul>
</section>
<?php endif; ?>
Im using this to display a list of thumnails called from my cms:
<?php if($gallery_images) { ?>
<?php
$slide_page = 1;
foreach($gallery_images as $count => $image) { ?>
<li><a href="<?php echo $image->getResizedImage(); ?>" rel="example1" title="********"><img width="125" height="80" src="<?php echo $image->getThumbnailImage()
?>" /></a></li>
<?php if(($count+1) % 3 == 0) {
$slide_page += 1;
?>
It calls images from within my CMS and displays them in groups of 3, with some added jquery to scroll through the sets.
What im trying to do is merge this with my videos within the same list.
The video code is as follows:
<?php foreach($videos as $count => $video) { ?>
<img src="{thumbnail}" />Video A
<?php } ?>
Ive tried using the array_merge function but seem to be having difficulties any help would be greatly appreciated
It's easy:
foreach (array_merge($gallery_images, $videos) as $count => $value) { }
You may also want to look at array_chunk().
Update:
<? foreach (array_merge($images, $videos) as $key => $value): ?>
<? if (is_object($value) === true): ?>
<? if (method_exists($value, 'getLocation') === true): ?>
<img src="{thumbnail}" />Video A
<? elseif (method_exists($value, 'getResizedImage') === true): ?>
<img width="125" height="80" src="<?= $image->getThumbnailImage(); ?>" />
<? endif; ?>
<? endif; ?>
<? endforeach; ?>