I want to get the infinite scroll page id - php

I implemented Yii infinite scroll its working fine. When I added Google ads based on given condition its displaying four Google ads. Then when fetching next page, the values are repeating. ie (Google ads-showing empty results) so that I want to GET the infinite scroll page ID.
Please help me how to get the page ID(ie next page ID). I have added my code here :
<?php
$post_counter = 0;
$addnum = 0;
foreach($posts as $rec): ?>
<div class="post">
<?php $rec_id = $rec['recipe_id']; ?>
$post_counter++;
if (($post_counter == 7) AND ($addnum < 4)) {
$addnum = $addnum + 1;
$post_counter=1;
}
?>
<script>
// Display an ad unit
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<?php if(isset($addnum)>3)
echo exit($post_counter)?>
<?php
}
?>
i put like this it will work

go to extension/assets/yiiinfinitescroll.js
get ID from js file

Related

How to display banner after three post?

Below is the code to display the post. That loads every 10 posts. And I want to display banner after 3 post. Can someone tell me, code to display banner after 3 post? By completing the code below. Thank you so much for your help.
<?php
$stories = Wo_GetPosts(array('limit' => 10));
if (count($stories) <= 0) {
echo Wo_LoadPage('story/no-stories');
} else {
foreach ($stories as $wo['story']) {
echo Wo_LoadPage('story/content');
}
}
?>
You are trying to use count() in a way that this function is not intended. Per the PHP documentation:
Counts all elements in an array, or something in an object.
http://php.net/manual/en/function.count.php
What you need to do is create a counter variable that increments within your foreach loop, and when it hits 3 outputs the banner. Your code might look something like this:
<?php
$stories = Wo_GetPosts(array('limit' => 10));
// No stories; output error
if (count($stories) <= 0)
echo Wo_LoadPage('story/no-stories');
// Stories exist; show them!
} else {
$count = 0;
// Loop through $stories
foreach ($stories as $wo['story']) {
// Increment the value of $count by +1
$count++;
if ($count == 3) {
// Output my Banner here
}
echo Wo_LoadPage('story/content');
}
}
?>
One thing I would note is that the above code only outputs a banner one time; when the value of $count is 3. You could adjust this to run ever 3rd story by changing the match from if ($count ==3) to if ($count % 3 == 0) which essentially reads as If the value of $count is divisible by 3.
I guess you create some HTML Code with this? If true, I'd use some Javascript for this purpose. Something like this:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="stories">
<div>story 1</div>
<div>story 2</div>
<div>story 3</div>
<div>story 4</div>
</div>
<script type="text/javascript">
$("#stories>div:nth-child(3)").after('<div class="banner">your bannery</div> ');
</script>
</body>
</html>

How to create simple pagination in PHP page

Below is my Code.
<?php
$folder_name=$_GET['folder_name'];
$dirname = "customized_kits/images/";
$images = glob($dirname."*");
$filecount = count( $images );
$i=0;
for($i=0;$i<200;$i++) { ?>
<div class="col-md-2"> <div class="photos">
<div id="images1">
<a href=# data-lightbox="roadtrip">
<?php
if($filecount>$i) {
$filename = substr($images[$i], strrpos($images[$i], '/') + 1);
echo '<img src="'.$images[$i].'" style="width:150px;height:150px" /><br/>';
?>
<i class="fa fa-remove"></i>
<?php } else {
echo '<a ><img style="width:150px;height:150px;background-color:#eee" /></a>';
} ?>
</div>
</div>
</div>
<?php } ?>
Here I created 200 boxes with foreach loop I want to show 20 divs for single page and with help of pagination I want to show other divs.I searched in many websites I didnt get exact answer please help me to get out of this issue.
Okay so from your code you can have 200 values of $i and you want to paginate them in segments of 20. Let us start off by adding a script above your code to get the range that needs to be displayed. Page number comes as a GET parameter in number. ex example.com/feeds?number=2.
<?php
$pageno = $_GET['number']; // Page number from frontend
// Now this value will give you 1,2,3 etc
$start = $pageno*20;
// $start contains the initial offset or the value of `$i` from to start
for($i=$start;$i<$start+20;$i++) {
// Will only show 20 elements on the page
}
?>
Remember this is a very basic example. Although i would recommend using range in sql query instead so you don't have to load the complete dataset in each query.
More explanation
Suppose you have M results and you want to show N results on each page, the results on page x would start from x*N to x*(N+1) pure math here. In your code you are iterating $i over the whole loop rather just iterate over that specific range to get results. The value of x is the example is the page number that we get from number in GET verb.

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++;
} ?>

$_GET not working when trying to view php image in a lightbox style

I have something that im currently working on, however it appears that the $_GET doesn't completely work.
I have a JavaScript light box that brings up an image in a little window, this works however i can only guess that it is using the same URL over and over again.
However when i view the source for the page (and even click one of the links in the source) it will display the correct data.
But the lightbox only seems to display the first image.
This is the JavaScript
<script>
//Checkes if any key pressed. If ESC key pressed it calls the lightbox_close() function.
window.document.onkeydown = function (e)
{
if (!e){
e = event;
}
if (e.keyCode == 27){
lightbox_close();
}
}
</script>
<script>
//This script makes light and fade divs visible by setting their display properties to block.
//Also it scrolls the browser to top of the page to make sure, the popup will be on middle of the screen.
function lightbox_open(){
window.scrollTo(0,0);
document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';
}
</script>
<script>
//This makes light and fade divs invisible by setting their display properties to none.
function lightbox_close(){
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
</script>
I wont show the CSS i dont think thats relivant (If someone wants it then ask away)
The relevant part that creates the links is this, its part of a ForEach statement all PHP
$i = 0;
foreach ($nrows as $nrow)
{
$id = $nrow['id'];
$rid = $nrow['RaidID'];
$bid = $nrow['BossID'];
$normal = $nrow['NormalKills'];
$heroic = $nrow['HeroicKills'];
$boss = substr($nrow['BossName'], 0, 3);
$p1 = $id + $bid.".php";
$image = $boss . $p1;
#echo $image;
echo $bid;
if ($oid != $rid)
{
$i = 0;
}
if ($i == 0) {
?><td style="width: 176px;"><center><b><?php echo $nrow['raid']; ?> </b></center></td> </tr><?php
$i++;
}
?><tr><td style="width: 176px;"><div align="left"><?php echo $nrow['BossName']; ?><div id="light"><img src="bossdata/template.php?boss=<?php echo $bid;?>"></a></div><div id="fade" onClick="lightbox_close();"></div>
</div>
<?php
if ($heroic == 0)
{
if ($normal > 0)
{
echo '<img src="images/whiteskull.png" align="right" alt="Normal Kill">';
}
else
{
echo '<img src="images/redx.png" align="right" alt="Not Killed">';
}
}
else
{
echo '<img src="images/redskull.png" align="right" alt="Normal Kill">';
}
?>
</td></tr><?php
$oid = $id;
}
Now this all works, and it actually displays an image with data, however no matter what link i click the boss data is always from the first one on the list.
To me this means that the data is getting through, and reaching the the right parts on image so its "Working", but all the links do the same thing and show the same data :(
*Removed last code Bulk
You have multiple div with the same ID "light" since you create them in a foreach loop.
<div id="light">
Your function lightbox_open() opens all the divs that have id "light".
document.getElementById('light').style.display='block';
That's why you always see the first lightbox. Because the others are behind the first one.
you should try something like this :
function lightbox_open(elem){
window.scrollTo(0,0);
elem.getElementByClass('light').style.display='block';
elem.getElementByClass('fade').style.display='block';
}
And change this :
<a href="#" onclick="lightbox_open();">
By this :
<a href="#" onclick="lightbox_open(this);">
And replace id by class in your div definition :
<div class="light">
$_GET is working correctly in your code.
The issue is in the way you are combining JavaScript and PHP in the second code box. First, all of your divs have the same ID: "light" which is wrong because they all IDs are meant to be unique within the HTML document. You need to identify them uniquely, for example appending the BossID to them.
After identifying each div uniquely you'll have to edit lightbox_open and lightbox_close so they can receive the BossID of the divs that you want to show and hide.

Next & previous links: Not showing a link if there isn't a next page

I've followed the documentation here (at the bottom) to create next and back buttons at the bottom of my page.
It seems to work fine until I get to the last page where the link just redirects me back to the first page. Is there a way to say if there isn't a next page to not show the link? I assumed thats what the if statement was supposed to do!!
<?php
$pagelist = get_pages('sort_column=menu_order&sort_order=asc');
$pages = array();
foreach ($pagelist as $page) {
$pages[] += $page->ID;
}
$current = array_search(get_the_ID(), $pages);
$prevID = $pages[$current-1];
$nextID = $pages[$current+1];
?>
<?php if (!empty($prevID)) { ?>
<a class="back" href="<?php echo get_permalink($prevID); ?>">BACK</a>
<?php } ?>
<?php if (!empty($nextID)) { ?>
<a class="next" href="<?php echo get_permalink($nextID); ?>">NEXT</a>
<?php } ?>
p.s Please don't move my question to the Wordpress Stack - that seems to be dying a bit of a death and doesn't get many responses!
My Pages are setup like this:
Parent page
Sub page 1
Sub page 2
Sub page 3
I've created a link on the parent page to goto the first subpage. Then on the subpage template I've got the code above. I just want the next link to appear on each page then when it gets to page 3 it shouldn't show the next link.
If you are saying that this is effectively looping around then $nextID must never be empty, which would be why the link was always displayed.
You could set a $firstID, ie
$firstID = pages[0];
and then check;
if ($firstID != $nextID ) {
// Display link
}

Categories