foreach if user clicked on link? - php

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.

Related

Pass image url from one page to another in PHP

I am fetching data from database table and showing result in HTML table. What I want to achieve is that, there is image column in every row, and every image is fetched from its URL (stored in database). I am opening an image in new tab whenever it is clicked.
My question is that, how can I store url of image so that on the next page I don't want to show that real url?
The next page will look like this link:
www.example.com/full_size_image.php
and add
img tag
there to show that page
how can I store that specific image url when someone click on it?
<img src= "<?php echo $_SESSION['link'] ;">
My current code is :
while($result = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>".$result['c']."</td>";
echo "<td>".$result['UserName']."</td>";
echo "<td>".$result['UserProblemKeyword']."</td>";
echo "<td> <a href ='".$result['UserProblemPicture']."' target='_blank'><img src='".$result['UserProblemPicture']."' height='62' width='62'> </a> </td>";
echo "</tr>";
}
Use a parameter via the GET Request?
So instead of directing to just /full_size_image.php direct to
/full_size_image.php?img=filename.png
then in your php code for /full_size_image.php
You can fetch the file name via
<?php
$img = isset($_GET['img']) ? htmlspecialchars($_GET['img']) : "";
if ($img == "") {
//img not set
}
//do whatever with the filename

Load next and previous images list with codeigniter

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

Check if div ID exists in PHP

I have same php to generate HTML in 2 ocasions:
Generate a link with target="_new";
Generate a link without target property.
The only way that I have to differentiate both of them is to create the parent div as different ID (eg: <div id="new"> for the 1st, '' for the 2nd.
Is there any way to check if has some #new in html and them set target?
Here's the way that I've tried so far:
<?php $new = $html->find("#new"); ?>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" <?php if (is_object($new)): ?> target="_new" <?php else : ?> <?php endif; ?> >
If you are following HTMLDOMPARSER then you can follow:
$html = file_get_html('http://www.google.com/');
$is_new_exist=false;
if($html->find('div#new'))
$is_new_exist=true;
And now you can use that flag for your checking
For further query please checkout HTMLDOMPARSER
I would presume you would be able to use something like
$new = $html->find("#new");
if ($new) {
echo something
} else {
echo something else
}
Based on the assumption you are using domdocument or a html parser.
You would not use target however but rather do something like <a href="#new" or if it were on another page <a href="somepage.php#new"

wordpress - how to get jquery slide caption and title

I am beginner in PHP, so maybe for some of you this question is ridiculous.
I would like to get image title and caption from jquery slider. (Themeforest Delight theme - http://www.pixedelic.com/themes/delight/), to show them in the same way as in (http://themes.themegoods.com/dk_wp/).
I have tried almost everything.
Thanks in advance for your help.
Here is the code from header:
<?php
function default_bg(){
if(get_pix_option('pix_general_background')=='slideshow'){ ?>
<div class="pix_slide" data-position="fixed" data-top="0" data-bottom="not">
<?php
$slide_general = get_pix_option('pix_array_slide_general_');
foreach ($slide_general as $slide) {
echo '<div data-src="'. $slide .'" data-thumb="'. get_pix_thumb($slide, 'exTh') .'" data-content="'. get_pix_content($slide) .'"></div>';
}
?>
I have found some script, which get the content, but can't implement it.
Here it is:
content: jQuery('#pix_credits_pictures').html(),
events: {
show: function(event, api) {
api.set('content.text', jQuery('#pix_credits_pictures').html());
}
},
You will need to modify the way the jQuery slider to include custom attributes for the images to include the title and the caption. Something like:
<?php
$slide_captions = array("caption1","caption2"...);
$slide_title = array("title1","title2"...);
?>
<?php
function default_bg(){
if(get_pix_option('pix_general_background')=='slideshow'){ ?>
<div class="pix_slide" data-position="fixed" data-top="0" data-bottom="not">
<?php
$slide_general = get_pix_option('pix_array_slide_general_');
$c = 0;
foreach ($slide_general as $slide) {
echo '<div data-src="'. $slide .'" data-thumb="'. get_pix_thumb($slide, 'exTh') .'" data-content="'. get_pix_content($slide) .'" image_title="'.$slide_title [$c].'" image_caption="'.$slide_captions[$c].'" class="slide_item"></div>';
$c++;
}
?>
You can implement a more elegant solution for "storing" the titles and captions instead of an array, I guess that depends on the way your plugin works and how it is storing the images.
Next up you can access the title and captions using jQuery like so:
var image_title= $('.slide_item').attr('slide_title');
var image_caption= $('.slide_item').attr('image_caption');
Obviously you will have to decide where exactly you want to use the title and when, but that is how you access them. The best way is probably to modify the slider library on the active image event.

Stripping Specific HTML & Content from a page with PHP for RSS

I am building a mobile version of my company website, and one thing we are in need of is an RSS feed.
I have the RSS pulling in fine with this code:
<?php
$url = 'http://www.someurl.com/rss/articles';
$feed = simplexml_load_file($url, 'SimpleXMLIterator');
$filtered = new LimitIterator($feed->channel->item, 0, 15);
foreach ($filtered as $item) { ?>
<li data-icon="false">
<h2><?php echo $item->title; ?></h2>
<p class="desc"><?php echo $item->description; ?></p>
<br />
<p class="category"><b><?php echo $item->category; ?></b></p>
<a class="link" href="<?php echo $item->link; ?>">Read More</a>
<br />
<p class="pubDate"><?php echo $item->pubDate; ?></p>
<br />
</li>
<?php } ?>
What I would like to do is utilize either the fopen() or file_get_contents() to handle the clicking of the 'Read More' link and strip all of the contents of the incoming page except for the <article> tag.
I have searched Google the past day, and have not been successful in finding any tutorials on this subject.
EDIT:
I would like to load the stripped HTML contents into their own view within my framework.
SECOND EDIT:
I would just like to share how I solved this problem.
I modified my $item->link; to be passed through the URL as a variable:
Read More
On the article.php page, I collect the variable with a if() statement:
if (isset($_GET['rss_url']) && is_string($_GET['rss_url'])) {
$url = $_GET['rss_url'];
}
Then building on the suggestions of the comments below, I built a way to then collect the incoming URL and strip the necessary tags to then format for my mobile view:
<div id="article">
<?php
$link = file_get_contents($url);
$article = strip_tags($link, '<title><div><article><aside><footer><ul><li><img><h1><h2><span><p><a><blockquote><script>');
echo $article;
?>
</div>
Hopefully this helps anyone else who may encounter this problem :)
I'm not sure if I understand it correctly but are you trying to output the contents on the current page whenever someone clicks the more link?
I would probably use Javascipt to do that, maybe jQuery's .load() function which loads html from another page and allows you to load only specific fragments of a page.. but if you need to use php I would look into Simple HTML DOM Parser
$html = file_get_html($yourUrl);
$article = $html->find('article', 0); // Assuming you only have 1 article/page
echo $article;
The only way I can see is to set up your own separate script to route the links through.
So, instead of echo $item->link use
echo 'LinkProcessor.php?link='.$item->link
Then, setup a script called LinkProcessor.php and use file_get_contents on that page. You can then process the XML to only show the article tag and echo the results:
$article = file_get_contents($_GET['link']);
$xml = new SimpleXMLElement($article);
$articleXml = $xml->xpath('//article');
echo articleXml[0];
Note that the code is untested, but it should be OK.

Categories