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>
Related
How can I add a certain class to the ul based on how many images there are?
So when there is 2 images the ul get class"images one-photo" and when there are 5 images the ul gets the class class"images five-photo" and so on...
What I now have in code:
<?php if( have_rows('blog_item') ): ?>
<?php while( have_rows('blog_item') ): the_row(); ?>
<ul class="images">
<?php if( get_sub_field('photo-01') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-01'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-02') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-02'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-03') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-03'); ?>" alt="" /></li>
<?php endif; ?>
</ul>
<?php endwhile; ?>
<?php endif; ?>
Something like this would come to mind:
<?php
$count = 0;
$image_display = '';
for($i=1 ; $i <= 3; $i++){
if( get_sub_field('photo-0'.$i) ):
$count++; // count photos here
$image_display .= '<li><img src="https://www.website.com/'.the_sub_field("photo-0".$i).'" alt="" /></li>';
endif;
}
$class = '';
switch ($count) {
case 1: $class = 'one-photo';break;
case 2: $class = 'two-photo';break;
case 3: $class = 'three-photo';break;
case 4: $class = 'four-photo';break;
case 5: $class = 'five-photo';break;
}
echo '<ul class="images '.$class.'">'. $image_display.'</ul>';
?>
Is it possible to modify this code to return the categories in reverse order (i.e Highest ID first?) It's currently displaying the output with the lowest ID first. Many thanks in advance!
$imageCols = JRequest::getVar("imageCols");
$thumbWidth = JRequest::getVar("categoryThumbWidth", 4);
$columnWidth = floor(100 / $imageCols);
$document =JFactory::getDocument();
$document->addStyleSheet('components/com_groovygallery/css/groovygallery.css');
$document->addScript( JURI::base().'components/com_groovygallery/js/modernizr.custom.84782.js' );
$document->addScript( JURI::base().'components/com_groovygallery/js/masonry.pkgd.min.js' );
<?php if($this->params->get('show_page_heading')){?><h2><?php echo $this->params->get('page_heading');?></h2><?php } ?>
<div id="groovyGalleryCats" data-masonry-options='{ "columnWidth": 200, "itemSelector": ".groovyGalleryCat" }'>
<?php
foreach($this->categories as $key=>$category){
$params = json_decode($category->params);
$catImage = JURI::base().$params->image;
?>
<a class="groovyGalleryCat" href="<?php echo JRoute::_('index.php?option=com_groovygallery&view=images&filter_category='.$category->id); ?>" >
<?php if($params->image){ ?><img src="<?php echo JURI::base().'components/com_groovygallery/timthumb.php?src='. $catImage . '&zc=2&w='.$thumbWidth;?>" alt="<?php echo $category->title;?>" /> <?php } ?>
<?php echo $category->title;?>
<?php //echo $category->description;?>
</a>
<?php
}
?>
</div>
<style>
a.groovyGalleryCat {width:<?php echo $columnWidth;?>%;}
</style>
<script src="<?php echo JURI::base().'components/com_groovygallery/js/init-cat.js'; ?>" defer="defer"></script>
You can reorder the array like this (supposing that the id property is public...):
uasort($this->categories, function($a, $b) {
return $a->id > $b->id ? -1 : 1;
});
I want to add a script after showing 10 videos in my website. But I don't understand how to use an if condition for this. This is my php code which displays all the videos from my database. I want that after displaying 10 videos it display this script.
<script>
This may contain the code of chitika code.
<script>
This is PHP code.
<section class="videos">
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section> <hr>
<?php } ?>
<?php } ?>
</section>
Inside your loop you just need a simple condition and a counter.
Here's an example. This is a loop that runs 12 times using a while loop like your code.
$i is an incrementing variable ($i++ increments it's value at the end of each loop cycle). The if statement says if $i is equal to 10 then do something. In this case it echos "10 records reached".
Make sure that when you define your counter first ($i = 1) it is before the while loop starts.
<?php
$i = 1;
while ($i <= 12) {
echo "record $i<br/>";
if ($i == 10) {
echo "10 records reached<br/>";
}
$i++;
}
?>
So adapting that thought process to your code would look like this:
<section class="videos">
<?php $i = 1; // This is where the counter is defined ?>
<?php while ($res=$stmt_today->fetch()) { ?>
<?php if ($res === NULL) { ?>
<section class="box">
<a href="" class="video-box">
<img src="" width="190" height="90" alt="">
</a>
<strong class="title">Coming Soon</strong>
</section>
<?php } else {
$immg = basename($images);
$imagee = "img"."/".$immg; ?>
<section class="box" style="width:100%; padding-top:2px;">
<?php if($images!=''){?>
<a href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="<?php echo $imagee; ?>" width="190" height="90" alt="">
</a>
<?php } else {?>
<a style="margin-left:5px;"href="video.php?vid=<?php echo $video_id ?>" class="video-box">
<img src="http://img.youtube.com/vi/<?php echo $video_thumbnail; ?>/mqdefault.jpg" width="190" height="90" alt="">
</a>
<?php } ?>
</section><hr>
<?php } ?>
<?php if ($i == 10) { // This is the new condition ?>
<script>
This may contain the code of chitika code.
<script>
<?php } // condition ends here ?>
<?php $i++ // increment the counter ?>
<?php } ?>
</section>
If you don't want to include your null values in the count (your 'coming soon' videos), you can simply move the counter increment $i++ up into the else { ... } portion of your condition.
<?php if (res === NULL) {
...
} else {
...
$i++;
} ?>
I have the following code:-
<ul class="grid effect-2" id="grid">
<?php
if( have_rows('gallery') ):
while ( have_rows('gallery') ) : the_row(); ?>
<?php
$image_location = get_sub_field('image_location');
if (empty($_GET['filter'])) {
$image_filter = $image_location == 'Nottingham' || $image_location == 'Corby';
} else {
$image_filter = $image_location == $_GET['filter'];
}
?>
<?php
if($image_filter) {
?>
<li><a class="fancybox" rel="gallery" href="<?php the_sub_field('image'); ?>" title="<?php echo $image_location . ' # Planet Bounce ' . get_sub_field('image_location'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="<?php echo get_sub_field('image_caption') . ' # Planet Bounce ' . $image_location; ?>" /></a></li>
<?php } ?>
<?php endwhile;
else : endif;
?>
</ul>
$image_location can either be 'Nottingham' or 'Corby' or both. The image filter is basically filtering which images are being shown.
Although the above code works, I don't think it is right to do it this way.
If anybody could help me with a better practise way of doing the same query that would be much appreciated.
If you need me to explain in better detail, please let me know.
The condition can be wrapped in a two liner code with use of array.
$imageFilters = array('Nottingham'=>'Nottingham', 'Corby'=>'Corby');
$image_filter = isset($imageFilters[$_GET['filter']]) ? $imageFilters[$_GET['filter']] : $_GET['filter'];
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; ?>