My SimplePie install is a straight-up linux install. (no wordpress or anything)
I'm trying to add a banner in-between my feed articles. For instance if I have 10 feed articles displaying per page, I'd like to add one after the 5th one.
Any help is much appreciated... My feed page is very basic and visible here:
http://www.oil-gas-prices.com
In case you're unfamiliar with SimplePie code, here's basically a very similar code to what makes up the page above:
http://simplepie.org/wiki/setup/sample_page?rev=1341798869
To display how many articles I want on each page, I use:
// Set our paging values
$start = (isset($_GET['start']) && !empty($_GET['start'])) ? $_GET['start'] : 0; // Where do we start?
$length = (isset($_GET['length']) && !empty($_GET['length'])) ? $_GET['length'] : 10; // How many per page?
$max = $feed->get_item_quantity(); // Where do we end?
In your loop that outputs the articles, you can use a counter and the modulus operator:
$counter = 0;
foreach ($feed->get_items($start, $length) as $key=>$item) {
if ($counter % 5 == 0) { // use modulus operator
// display banner
}
// ...
$counter++;
}
See php modulus in a loop article. The code above will display the banner when $counter = 0, 5, 10, etc.
Related
I have a robots.txt file
in that i generate dynamic sitemap links.
I get the following links if i run the robots.txt file in the browser.
Here you get 5 sitemap links for each language.
Reason: there are 10 products in database.
i want to show only two products per link. so i divided the total no.of products with no.of products on one page.
Sitemap:http://demo.com/pub/sitemap_products.php?page=1&lang=it_IT
the part in bold is dynamic.
code in: sitemap_products.php
$Qproduct : returns an array of all the products in the db for all the languages.
So the bellow loop generates an xml having links of the products for the language in the sitemap url
for eg.
if the link is
Sitemap:http://demo.com/pub/sitemap_products.php?page=1&lang=it_IT
it will generate all the products present in IT language.
The xml links that are generated now are based on languages that we get from url.
but i want to divide them into chunks of 2 product's xml per sitemap link.
while($Qproduct->next())
{
if(!isset($page_language[$Qproduct->valueInt('language_id')]))
{
$page_language[$Qproduct->valueInt('language_id')] = mxp_get_page_language($MxpLanguage->getCode($Qproduct->valueInt('language_id')), 'products');
}
if($Qproduct->valueInt('language_id') == $QproductLang->valueInt('languages_id'))
{
$string_to_out .= '<url>
<loc>' . href_link($page_language[$Qproduct->valueInt('language_id')], $Qproduct->value('keyword'), 'NONSSL', false) . '</loc>
<changefreq>weekly</changefreq>
<priority>1</priority>
</url>';
}
}
what i wish to do is apply a condition so that it gives me exactly two products links in xml when page=1(see in the sitemap links) instead of all the 10 products link in xml.
similarly if page=2 it should display next 2 products. and so on.
I am a bit confused in the condition that i am supposed to apply.
Please help me out.
First of all, use an XML library to create the XML, not string concatenation. Example:
$loc = href_link($page_language[$Qproduct->valueInt('language_id')], $Qproduct->value('keyword'), 'NONSSL', false);
$url = new SimpleXMLElement('<url/>');
$url->loc = $loc;
$url->changefreq = 'weekly';
$url->priority = 1;
In your case, you can even easily wrap that into a function that just returns such an element and which has two parameters: $Qproduct and $page_language (as string, not array (!)).
But that's just some additional advice, because the main point you ask about is the looping and more specifically the filtering and navigating inside the loop to the elements you're interested in.
First of all you operate on all results by looping over them:
while ($Qproduct->next())
{
...
}
Then you say, that you're only interested in links of a specific language:
while ($Qproduct->next())
{
$condition = $Qproduct->valueInt('language_id') == $QproductLang->valueInt('languages_id');
if (!$condition) {
continue;
}
...
}
This already filters out all elements not interested in. What is left to keep track and decide which elements to take:
$page = 1;
$start = ($page - 1) * 2;
$end = $page * 2 - 1;
$count = 0;
while ($Qproduct->next())
{
$condition = $Qproduct->valueInt('language_id') == $QproductLang->valueInt('languages_id');
if (!$condition) {
continue;
}
$count++;
if ($count < $start) {
continue;
}
...
if ($count >= $end) {
break;
}
}
Alternatively, instead writing this all the time your own, create an Iterator for $Qproduct iteration and the use FilterIterator and LimitIterator for filtering and pagination.
I am using fpdf AddLink function to create an internal link and at the end of all page generation I am using the below function to reorder the pages after that Links are not working. What could be the reason.Thanks
public function movePages($oldIndex, $nrPages, $newIndex) {
$newPages = array();
for($i=1; $i<=sizeof($this->pages); $i++) {
if($i < $newIndex) {
$newPages[$i] = $this->pages[$i];
} elseif($i < $newIndex+$nrPages) {
$newPages[$i] = $this->pages[$oldIndex+$i-$newIndex+1];
} else {
$newPages[$i] = $this->pages[$i-$nrPages];
}
$newPages[$i] = str_replace('{pageNr}', $i, $newPages[$i]);
}
$this->pages = $newPages;
}
Right.
Just ran into this myself. Found a solution.
When the page switching occurs, it appears that the text elements are stored in one kind of mapping scheme, while the visual elements are stored in another. When the page is switched, the text moves and the hotspots do not. They're still linked and work, but are hard to find now on their original page.
The trick I found that worked was to store the Abscissa co-ordinates and page number to link to in an array (hash indexed by the original page number), do the switching within a loop (manually setting the page number of $this->page for each link) and use $this->Link($x, $y, $w, $h, $link) to make the hotspot where I want it to and then $this->SetLink($link, 0, $pageNo) to set it to the right page.
Then reset the page count or it won't render properly.
I was trying to find a way to create multiple links automatically in Dokuwiki.
I tried enabling php and using the following code:
<php>
for ( $counter = 1; $counter <= 3; $counter += 1) {
echo "[[page$counter]]";
echo $counter;
}
</php>
I thought that the "echo" would work like in HTML and its output would be interpreted by Dokuwiki as its own command
(creating 3 links), but instead of that I just get the following text output (no links):
[[page1]]1[[page2]]2[[page3]]3
Any help on how to use a loop (not necessarily with PHP) in Dokuwiki to create multiple links?
THANKS!
You should put you loop in a syntax plugin.
For example: take the skeleton plugin, replace the render function by:
function render($mode, &$renderer, $data) {
for ( $counter = 1; $counter <= 3; $counter += 1) {
$renderer->internallink("page$counter", "title$counter");
}
return true;
}
After installing it, the tag "<TEST>" will be substituted by an invocation of your plugin.
For more info you might want to check:
The documentation
The nspages plugin which use a loop to add internal links
I am setting up a series of Twitter feed displays on one page. One shows the MOST RECENT status, in a particular fashion. The other (I am hoping) will show the next 4 statuses, while NOT including the most recent status. Here is part of the code that I think needs attention in order for this idea to work out:
$rss = file_get_contents('https://api.twitter.com/1/statuses/user_timeline.rss?
screen_name='.$twitter_user_id);
if($rss) {
// Parse the RSS feed to an XML object.
$xml = simplexml_load_string($rss);
if($xml !== false) {
// Error check: Make sure there is at least one item.
if (count($xml->channel->item)) {
$tweet_count = 0;
// Start output buffering.
ob_start();
// Open the twitter wrapping element.
$twitter_html = $twitter_wrap_open;
// Iterate over tweets.
foreach($xml->channel->item as $tweet) {
Here is the website which has lent me the code for this task:
< Pixel Acres - Display recent Twitter tweets using PHP >
Your foreach loop goes over each item in the feed. You want to skip certain elements based on the position in the feed, so you could add an index variable to the foreach and an if after the foreach:
foreach($xml->channel->item as $i => $tweet) {
if ($i == 0 || $i > 4)
continue;
I used an alternate method to solve the issue I was having. It included using a string replace on the latest tweet's URL to obtain the Tweet ID, which then allowed me to query tweets using (Tweet ID - 1) as the max_id term.
I have a few questions regarding a PHP Pagination Script For Flat File Database I found.
I have posted the script below.
<?php
echo '<html><body>';
// Data, normally from a flat file or some other source
$data = "Item1|Item2|Item3|Item4|Item5|Item6|Item7|Item8|Item9|Item10";
// Put our data into an array
$dataArray = explode('|', $data);
// Get the current page
$currentPage = trim($_REQUEST[page]);
// Pagination settings
$perPage = 3;
$numPages = ceil(count($dataArray) / $perPage);
if(!$currentPage || $currentPage > $numPages)
$currentPage = 0;
$start = $currentPage * $perPage;
$end = ($currentPage * $perPage) + $perPage;
// Extract ones we need
foreach($dataArray AS $key => $val)
{
if($key >= $start && $key < $end)
$pagedData[] = $dataArray[$key];
}
foreach($pagedData AS $item)
echo ''. $item .'<br>';
if($currentPage > 0 && $currentPage < $numPages)
echo '« Previous page<br>';
if($numPages > $currentPage && ($currentPage + 1) < $numPages)
echo 'Next page »<br>';
echo '</body></html>';
?>
My first problem seems to be in line 9. I could change the line to:
$currentPage = trim(#$_REQUEST[page]);
But this change won't fix the error, it will just hide it. What needs to be done to line 9 to rid my page of the error?
Secondly, I would like to fetch the data on line 5 in a different way. I would like to get the data from a text file, let's call it "items.txt", that has entries like below, one per line.
Fun
Games
Toys
Sports
Fishing
Pools
Boats
Please recommend alternate code to fetch the desired data.
Lastly, I would like to include links to the "First page" and "Last page" as well as "Previous page" and "Next page", as is the current code.
I apologize for my sloppy posting, but would be real appreciative of anybody who could help me understand the changes needed to produce my desired results. Thanks.....
Problem With Line 9
$_REQUEST[page] has two separate problems.
1) page is being read as the name of a constant because it is not quoted. PHP then notices there is no constant called page, so it takes a guess that you meant the string page -- which it would be if page was quoted -- and triggers an error to notify you. Therefore, use $_REQUEST['page'] instead.
2) 'page' is not necessarily a key of $_REQUEST because the data is not guaranteed to be given. Therefore, you cannot refer to $_REQUEST['page'] before you ensure that it exists. This may be done by isset($_REQUEST['page']).
Your final code should look something like this, then.
if (isset($_REQUEST['page'])) {
$currentPage = $_REQUEST['page'];
} else {
$currentPage = 'some default value';
}
Problem With Data Source
The file() function reads the lines of a file into an array -- for example, the fourth value of the array is also the fourth line in the file. Therefore, you can set $dataArray simply as $dataArray = file('text.dat');.