Load next and previous images list with codeigniter - php

I am trying to create an image gallery with some images inside my root folder. In my view, I have the following code to list first 25 images.
<?php
$i=0;
foreach ($list as $k){?>
<?php if($i==25) break; ?>
<a data-fancybox="gallery" href="../../TL_PHOTOS/<?php echo $ip.'/'.$k; ?>" > <img src="../../TL_PHOTOS/<?php echo $ip.'/'.$k; ?>" class="img-thumbnail" width=250 height=250 style="padding:25px;"></a>
<?php $i++; ?>
<?php }
?>
Here $list array have all the images. I need to put a button line 'Load next 25 images' and display the rest of 25 images along with the existing ones. Is it possible with PHP for loop itself?

You have to load next 25 images through ajax request and append it into your view page.
You can implement this in CodeIgniter with reference of below php example.
Refer for details:
https://www.codexworld.com/load-more-data-using-jquery-ajax-php-from-database/
Hope it helps

Related

How do i loop through an array within an array in a database?

I have two image arrays in my mysql database that i am trying to loop through and display them. One array holds an array called 'postImage' of full size images, the other called 'thumbnail' holds thumbnails of those same images. However one array is for an <a> link that i want to direct to the full size image(s) and the other array is for an <img> tag inside the <a> link. What i'm trying figure out is how i can loop through both arrays but have each <a> link go to the correct matching full size image.
here is my code:
<?php foreach(json_decode($post->thumbnail) as $thumbnail) { ?>
<?php foreach(json_decode($post->postImage) as $postImage) { ?>
<a href="/storage/photos/{{$postImage}}">
<img src="/storage/photos/{{$thumbnail}}" alt="" class="img-fluid">
</a>
<?php } ?>
<?php } ?>
Don't use nested loops, that will produce a cross-product of all thumbnails and images.
Loop over one array, and use its index to access the corresponding element of the other array.
$thumbnails = json_decode($post->thumbnail);
$postImages = json_decode($post->postImage);
foreach ($thumbnails as $i => $thumbnail) {
$postImage = $postImages[$i]; { ?>
<a href="/storage/photos/{{$postImage}}">
<img src="/storage/photos/{{$thumbnail}}" alt="" class="img-fluid">
</a>
<?php } ?>

foreach if user clicked on link?

Im using lightgallery so i need to load images before call them in lightgallery. Problem is that images are large so it takes too much time to load. Is there any way to load that specific gallery when user click on link.
<div id="lightgallery-<?php echo get_the_ID(); ?>" class="hidelightgallery">
<?php
foreach ($files as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
$attachment_title = get_the_title($image->ID);
$caption = get_post_field('post_excerpt', $image->ID);
?>
<a class="item" href="<?php echo $image_attributes ?>" data-sub-html="<?php echo $attachment_title; ?> <?php if($caption!= '') echo ' - ' ?> <?php echo $caption ?>"><img src="<?php echo $image_attributes ?>"></a>
<?php } ?>
</div>
Now what i want is if user click for example link with this id then do foreach. Is that possible?
Just follow these steps,
create new template / html page where you will write html and populate by foreach loop
add your id = lightgallery whole code to that html page
when you will click on your link (which you mentioned) fire an ajax
Ajax function will get some id or number of images need to show or your logic on how you will populate data in foreach loop
in php you will get all relevant data, and you will populate that data in html file you created in step 1
php function will return that data to ajax function
Ajax function will get all your dynamic html data
populate that html where ever you want or just append that html wherever you want
Go step by step, this will solve your problem.

How do I insert variables within html in a php array?

EDIT: I've updated this question with the working code, but I've left the content of the text alone so you can see what I was trying to do in the future and also because there were a few other things I wanted to do aside of the initial question.
I have an array that has an html list in it, the php is shuffling the list to echo a random order. Within the list items in the array, I want to include a php variable, right now this is what I have:
<?php include('includes/header_topright_content.php'); ?>
<ul data-options="animation:fade; slide_number:false; pause_on_hover:false; timer_speed:5500; navigation_arrows:false; next_on_click:true; timer:true; bullets:false;" data-orbit>
<?php
$links = array(
'<li data-orbit-slide="headline-1"><img /><div>'.$Slide1.'</div></li>',
'<li data-orbit-slide="headline-2"><img /><div>'.$Slide2.'</div></li>',
'<li data-orbit-slide="headline-3"><img /><div>'.$Slide3.'</div></li>',
);
shuffle($links);
foreach ($links as $link) { echo $link; }
?>
</ul>
I could have up to 10 or even 20 of these slides. The information the variables are linking to would be from a separate .php page that I "included." The information on that page is:
<?php
$Slide1 = "<h5>Slide 1</h5><h6>This is the content for slide 1!</h6>";
$Slide2 = "<h5>Slide 2</h5><h6>This is the content for slide 2!</h6>";
$Slide3 = "<h5>Slide 3</h5><h6>This is the content for slide 3!</h6>";
?>
If I scrap the variable idea and insert the html directly into the list item it works perfectly. If I try to put the variables in there, the slide will just be blank. I'm stuck on where to go from here, I'm great with html and css, but not so good with php, so any help is appreciated! I'm also open to any formatting tips and best practices, the cleaner the better.
Thanks in advance!
MORE INFO: There's a few complications behind as to why I'm looking to do this. The orbit image slider doesn't support a random order, and I found it much easier to just use php to randomize the order of the list items. The reason I want to use variables in these list items is because I'm using a cms (CouchCMS) to make that content editable - a simple solution would be to insert editable tags around that content, but that would only make one page editable, and this content is going to be 'included' in the header of every page. So I'm trying to find a way to put those variables on a separate page (I know 'including' it doesn't do that - maybe I can link it to the page like a css or js file?) to make that editable. If anyone has any ideas for this I'm open!
With string concatenation, your also need to include the variables file before assigning them variables to the array. You also dont need to wrap PHP variables in quotes when echoing.
<?php include('includes/variables.php'); ?>
<ul data-options="[orbit options go here]" data-orbit>
<?php
$links = array(
'<li data-orbit-slide="headline-1"><img /><div>'.$Slide1.'</div></li>',
'<li data-orbit-slide="headline-2"><img /><div>'.$Slide2.'</div></li>',
'<li data-orbit-slide="headline-3"><img /><div>'.$Slide3.'</div></li>',
);
shuffle($links);
foreach ($links as $link) { echo $link; } //<<< notice no quotes
?>
</ul>
Edit:
Can I make a suggestion to your code, by assigning to a slides array directly be it in an external file or not, you will be able to eventually dynamically or easily add new slides to the slider and not need to hard code into a second sub array before the loop. So something like. Also by changing to alternative syntax your keep a nice HTML structure.
<?php
$slide[] = "<h5>Slide 1</h5><h6>This is the content for slide 1!</h6>";
$slide[] = "<h5>Slide 2</h5><h6>This is the content for slide 2!</h6>";
$slide[] = "<h5>Slide 3</h5><h6>This is the content for slide 3!</h6>";
shuffle($slide);
?>
<ul data-options="animation:fade; slide_number:false; pause_on_hover:false; timer_speed:5500; navigation_arrows:false; next_on_click:true; timer:true; bullets:false;" data-orbit>
<?php foreach ($slide as $key=>$value):?>
<li data-orbit-slide="headline-<?php echo $key?>"><img /><div><?php echo $value; ?></div></li>
<?php endforeach;?>
</ul>

PHP foreach over uniquely styled divs

I'm trying to pull information from instagram and twitter, and display the information as a collection of square and rectangle boxes arranged on screen.
I was using a foreach statement to display the content, but because the containing divs are not consistent in size, I have to end the foreach statement and start a new one. The content of the new foreach is exactly the same as the content of the previous. I'm not sure if I'm going about this the right way and would appreciate any push in the right direction.
The block of code below displays the first 4 most recent instagram photos.
<?php $i = 0; foreach ($instagram_data->data as $latest_post): if (++$i == 5) break; ?>
<div class="engage-block"><img src="<?= $latest_post->images->standard_resolution->url ?>"></div>
<?php endforeach ?>
After that, I display the latest twitter content.
<div class="engage-horizontal engage-block"><span>"<?php echo $latest_tweet->text ?>"</span></div>
And then I repeat another foreach similar to the first to display more instagram content. This however repeats the exact same content from the code above (shows the latest 4 photos instead of the 4 photos after the original photos).
You could put a variable in the foreach to make the twitter div if it's the fifth, like this
<?php
$i = 0; foreach ($instagram_data->data as $latest_post){
?>
if ($i==5){?>
<div class="engage-horizontal engage-block"><span>"<?php echo $latest_tweet->text ?>"</span></div>
$i=0;
}
else{?>
<div class="engage-block"><img src="<?= $latest_post->images->standard_resolution->url ?>"></div>
<?php
}
$i++;
} ?>

Drupal 7: Display images instead of nodes titles

Normaly in Drupal 7 we have node.tpl.php:
<?php print render($title_prefix); ?>
<?php if (!$page): ?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
<?php endif; ?>
<?php print render($title_suffix); ?>
It is taking $node_url and putting it on a Title in every Node.
I do have 5 nodes (pages) displayed:
First
Second
Third etc
I have created images First.gif, Second.gif and I want to load that images instead of Title.
I did check various implementation, but didn't find any resolution for me.
[Update] I did try to edit template.php file and to add functions for replacing title with image-if images exists. I need this in Drupal 7 - please see here - http://drupal.org/node/221854
Is there any help? Thanks..
Since each node has it's own id then what I'd suggest to you, is to have in your specific files folder images such as node-12, node-13 etc.
In your node.tpl.php
<?php
// path to our file depending on node id
$file = "path/to/file/node-{$node->nid}.gif";
// check if file exists
if (file_exists($file)) {
// theme $title as image with theme_image()
$title = theme('image', array('path' => $file));
}
?>
<h2<?php print $title_attributes; ?>>
<?php print $title; ?>
</h2>
However I'm sure this is not the best way of development but it work for your question.

Categories