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; ?>
Related
I am just trying to remake manufacturers carousel so that it will show categories with their images. Part of it I have done. Now i jast want to show them in table 4 in a row. Here is the code that is file catalog/view/module/carousel.tpl/
<div id="carousel<?php echo $module; ?>" class="owl-carousel">
<div class="category">
<table>
<?php if($categories) { ?>
<?php $count = 0;?>
<?php foreach ($categories as $category) { ?>
<?php if($count % 4 == 1){?>
<tr>
<?php}?>
<td>
<a href="<?php echo $category['href']; ?>" <?php echo $category['name']; ?></a>
<img src="<?php echo $category['image'] ?>" alt="<?php echo $category['name']; ?>" title="<?php echo $category['name']; ?>" class="img-thumbnail" />
<p><?php echo $category['name']?>
</td>
<?php if($count % 4 == 0){?>
</tr>
<?php}?>
<?php } ?>
<?php } ?>
</table>
</div>
</div>
<script type="text/javascript"><!--
$('#carousel<?php echo $module; ?>').owlCarousel({
items: 15,
autoPlay: 1000000,
navigation: false,
navigationText: ['<i class="fa fa-chevron-left fa-5x"></i>', '<i class="fa fa-chevron-right fa-5x"></i>'],
pagination: false
});
--></script>
As a result of it I am getting error unexpected end of file. What am I doing wrong? Are their better solutions? I am using opencart version 2.0.1.1
You need to make some minor changes and this will work:
<?php if($count % 4 == 0){?>
</tr>
<?php } ?> //here you need to change
$count++;
Give the space after <?php tag. due to this the error occurs. There is require a space after this tag.
And you are increasing the value of $count.
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>
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'm using joomla! to output some extra fields into an article. The fields are a list of images (max of 10) which are displayed using backslider jquery plugin.
Here is the code i've used which works:
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
$img1 = $this->item->extrafields['image_1'];
$img2 = $this->item->extrafields['image_2'];
$img3 = $this->item->extrafields['image_3'];
$img4 = $this->item->extrafields['image_4'];
$img5 = $this->item->extrafields['image_5'];
$img6 = $this->item->extrafields['image_6'];
$img7 = $this->item->extrafields['image_7'];
$img8 = $this->item->extrafields['image_8'];
$img9 = $this->item->extrafields['image_9'];
$img10 = $this->item->extrafields['image_10'];
?>
<?php if($img1) { ?>
<li><img src="<?php echo $img1; ?>"></li>
<?php } ?>
<?php if($img2) { ?>
<li><img src="<?php echo $img2; ?>"></li>
<?php } ?>
<?php if($img3) { ?>
<li><img src="<?php echo $img3; ?>"></li>
<?php } ?>
<?php if($img4) { ?>
<li><img src="<?php echo $img4; ?>"></li>
<?php } ?>
<?php if($img5) { ?>
<li><img src="<?php echo $img5; ?>"></li>
<?php } ?>
<?php if($img6) { ?>
<li><img src="<?php echo $img6; ?>"></li>
<?php } ?>
<?php if($img7) { ?>
<li><img src="<?php echo $img7; ?>"></li>
<?php } ?>
<?php if($img8) { ?>
<li><img src="<?php echo $img8; ?>"></li>
<?php } ?>
<?php if($img9) { ?>
<li><img src="<?php echo $img9; ?>"></li>
<?php } ?>
<?php if($img10) { ?>
<li><img src="<?php echo $img10; ?>"></li>
<?php } ?>
</ul>
</div>
I'm not a php expert but is there a better way of optimising this code, I'm thinking maybe putting the $img variables into an array and using a foreach loop to output each list item?
A little help wouldn't go a miss :)
Just iterate over them and use the index to reference the array indices:
for ($i = 1; $i <= 10; ++$i) {
if (!empty($this->item->extrafields["image_$i"])) {
echo '<li><img src="', htmlspecialchars($this->item->extrafields["image_$i"]), '"></li>';
}
}
Assuming there are up to 10 items to investigate.
The solution below will avoid evaluating strings, cache's everything you can, to keep your application running smoothly, and keeps your code nice and tidy.
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
// Generate length of our image array / store known length of array
$images = 10;
// Loop through images
for( $i = 1; $i <= $images; $i++ ) {
// Store it for optimization sake.
$field = $this->item->extrafields['image_' . $i];
// Check it's not empty
if( !empty( $field ) ) {
// If not, print to browser
printf(
'<li><img src="%s" alt=""></li>',
htmlspecialchars($field)
);
}
}
?>
</ul>
</div>
I think this code will do same thing.
<div id="bs0" class="backslider">
<ul class="bs-slides">
<?php
for($i = 1; $i <= 10; $i++)
{
$img = $this->item->extrafields['image_'.$i]
if($img) { ?>
<li><img src="<?php echo $img; ?>"></li>
<?php }
}
?>
</ul>
</div>
Overview
I am trying to get a photo feed on to my site using Flickr's api and the phpflickr library. I can successfully get the photoset on to my site, but it shows all the photos from every photoset, what I was hoping to achieve was to show the primary photo from each photoset, and then if the user clicked on the image it would show the full photoset in a lightbox/shadowbox.
My Code
<div id="images" class="tabnav">
<ul class="items">
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<li>
<a rel="shadowbox['<?=$count;?>']" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<p><?=$ph_set['description'];?></p>
</a>
</li>
<?php endforeach; ?>
<?php $count++; ?>
<?php endforeach; ?>
</ul>
</div>
Another Attempt
I have also tried calling the getPhotos function differently, instead of sending it without any parameters I sent it with parameters
$photos = $f->photosets_getPhotos($photoset_id, NULL, NULL, 1, NULL);
The above code stopped the showing all the photos from each photoset and started showing just the primary image, but it also stopped making the rest of the photos accesible to me.
Is there something I can do to make this work? I am totally out iof ideas.
Regards and thanks
I came up with this soltion, thought I would post it in case anyone else hits this problem,
<?php $count = 1; ?>
<?php foreach ($photosets['photoset'] as $ph_set): ?>
<?php $parentID = $ph_set['parent']; ?>
<li>
<?php $photoset_id = $ph_set['id'];
$photos = $f->photosets_getPhotos($photoset_id);
foreach ($photos['photoset']['photo'] as $photo): ?>
<?php if($parentID == $ph_set['parent']): ?>
<a rel="lightbox[album<?=$count;?>]" href="<?= $f->buildPhotoURL($photo, 'medium') ?>" title="<?= $photo['title'] ?>">
<?php endif;?>
<img src="<?= $f->buildPhotoURL($photo, 'rectangle') ?>" alt="<?= $photo['title'] ?>" width="210" height="160" title="<?= $photo['title'] ?>" />
<h3><?=$ph_set['title']?></h3>
<?php if($ph_set['description'] != null) :?>
<p><?=$ph_set['description'];?></p>
<?php endif; ?>
<?php if($parentID == $ph_set['parent']): ?>
</a>
<?php endif;?>
<?php endforeach; ?>
</li>
<?php $count++; ?>
What you'll probably want to do is starting by iterating through the whole array and grouping each album into a separate array first and making a special array for your album's main photo.
Then you can easily iterate through the arrays to display each album and the code becomes much more maintainable.