使用 php 分页显示 rss 提要 [英] displaying rss feed with php pagination

查看:32
本文介绍了使用 php 分页显示 rss 提要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 RSS 提要和 php 分页的新手.我正在尝试解析 rss 提要并在单独的页面上显示每个提要详细信息.我从 http://www.phpeasystep.com/phptu/29.html 复制并粘贴了分页代码.当我在本地主机上运行 index.php 时,没有显示任何内容.

我使用 curl 下载 rss 提要没有问题.我也试过 print_r($data) 并且数据显示正常.

有人能指出我哪里出错了吗?谢谢.

我的代码:

xPath('/rss/channels/item');$count=count($items);//每边应该显示多少个相邻的页面?$相邻=3;$total_pages = $count;/* 设置查询变量.*/$targetpage = "index.php";//你的文件名(这个文件的名字)$limit = 1;//每页显示多少项$page = $_GET['page'];如果($页)$start = ($page - 1) * $limit;//本页显示的第一项别的$开始= 0;//如果没有给出页面变量,设置start为0/* 设置显示页面变量.*/如果 ($page == 0) $page = 1;//如果没有给出页面变量,默认为1.$prev = $page - 1;//上一页是page - 1$next = $page + 1;//下一页是page + 1$lastpage = ceil($total_pages/$limit);//最后一页是=总页数/每页项目,四舍五入.$lpm1 = $lastpage - 1;//最后一页减1/*现在我们应用我们的规则并绘制分页对象.我们实际上是将代码保存到一个变量中,以防我们想要多次绘制它.*/$分页=";if($lastpage > 1){$pagination .= "

解决方案

好的,所以这段代码有一些问题:

首先变量 $pagination; 是在函数内创建的,因此在全局范围之外不可用.对此的快速解决方法是:

在页面开头添加:

在函数内部添加:

function parseRSS($xml){全局$分页;

接下来Xpath位置错误

改变:

$items=$xml->xPath('/rss/channels/item');

进入

$items=$xml->xPath('/rss/channel/item');

这些更改应该会给您一些输出以开始使用,我认为您的分页存在更多问题,但是一旦您有一些输出,您应该设法解决它.

要仅获取所需的链接,您应该更改:

进入:

 $item){if( ( $key >= $start) && ($key < $start + $limit) ){echo("<a href='".$item[0]->link."'>".$item[0]->title."</a>");}}

I'm new to rss feeds and php pagination. I'm trying to parse rss feed and display each feed details on separate pages. I copied and pasted the pagination code from http://www.phpeasystep.com/phptu/29.html. When i run index.php on localhost, nothing gets displayed.

I have no problem downloading the rss feed using curl. I have also tried print_r($data) and data is displaying fine.

Can someone point me on where i'm going wrong? thank you.

my code:

<?php
if (function_exists("curl_init")){
    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,"http://feeds.feedburner.com/rb286");
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $data=curl_exec($ch);
    curl_close($ch);
    $doc=new SimpleXmlElement($data);
    //print_r($doc);
}

function parseRSS($xml){
    $items=$xml->xPath('/rss/channels/item');

    $count=count($items);

    // How many adjacent pages should be shown on each side?
    $adjacents =3;


    $total_pages = $count;

    /* Setup vars for query. */
    $targetpage = "index.php";  //your file name  (the name of this file)
    $limit = 1;                                 //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0


    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class="pagination">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href="$targetpage?page=$prev">� previous</a>";
        else
            $pagination.= "<span class="disabled">� previous</span>"; 

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class="current">$counter</span>";
                else
                    $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";                 
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class="current">$counter</span>";
                    else
                        $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";
                $pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href="$targetpage?page=1">1</a>";
                $pagination.= "<a href="$targetpage?page=2">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class="current">$counter</span>";
                    else
                        $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href="$targetpage?page=$lpm1">$lpm1</a>";
                $pagination.= "<a href="$targetpage?page=$lastpage">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href="$targetpage?page=1">1</a>";
                $pagination.= "<a href="$targetpage?page=2">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class="current">$counter</span>";
                    else
                        $pagination.= "<a href="$targetpage?page=$counter">$counter</a>";                 
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href="$targetpage?page=$next">next �</a>";
        else
            $pagination.= "<span class="disabled">next �</span>";
        $pagination.= "</div>
";       
    }

        foreach($items as $item){
              echo("<a href='".$item[0]->link."'>".$item[0]->title."</a>");
        }
}
if (isset($doc->channel))parseRSS($doc);

?>
<?php 
 echo($pagination);
?>
</body>

解决方案

Okay, so there are some issues with this code :

First the variable $pagination; is created within the function and therefor not available outside in the global scope. the quick fix for this is :

At the beginning of the page add :

<?php 
$pagination = '';

Inside the function add :

function parseRSS($xml){
    global $pagination;

Next the Xpath location is wrong

change :

$items=$xml->xPath('/rss/channels/item'); 

into

$items=$xml->xPath('/rss/channel/item');

These changes should give you some output to get started, i think there are more issues with your pagination but you should manage to sort it out once you have some output.

EDIT : To get only the needed link you should change :

<?php  
foreach($items as $item){
    echo("<a href='".$item[0]->link."'>".$item[0]->title."</a>");
}

into :

<?php  
foreach($items as $key => $item){
    if( ( $key >= $start) && ($key < $start + $limit) ){
        echo("<a href='".$item[0]->link."'>".$item[0]->title."</a>");
    }
}

这篇关于使用 php 分页显示 rss 提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆