0

我有两个页面,一个是脚本本身,另一个是调用它的页面。我可以很好地显示我的提要,但我需要以相反的顺序显示它。(最旧的在前)。我已经尝试了这两个 asort 和 arsort 函数,但我无法让它们工作。

这是代码:

 $RSS_Content = array();

function RSS_Tags($item, $type)
{
    $y = array();
    $tnl = $item->getElementsByTagName("title");
    $tnl = $tnl->item(0);
    $title = $tnl->firstChild->data;

    $tnl = $item->getElementsByTagName("link");
    $tnl = $tnl->item(0);
    $link = $tnl->firstChild->data;

    //$tnl = $item->getElementsByTagName("description");
//      $tnl = $tnl->item(0);
//      $description = $tnl->firstChild->data;

    $y["title"] = $title;
    $y["link"] = $link;
    //$y["description"] = $description;
    $y["type"] = $type;

    return $y;
}
function RSS_Channel($channel)
{
global $RSS_Content;

$items = $channel->getElementsByTagName("item");

// Processing channel

$y = RSS_Tags($channel, 0);     // get description of channel, type 0
array_push($RSS_Content, $y);

// Processing articles

foreach($items as $item)
{
    $y = RSS_Tags($item, 1);    // get description of article, type 1
    array_push($RSS_Content, $y);
}
}

function RSS_Retrieve($url)
{
global $RSS_Content;

$doc  = new DOMDocument();
$doc->load($url);

$channels = $doc->getElementsByTagName("channel");

$RSS_Content = array();

foreach($channels as $channel)
{
     RSS_Channel($channel);
}

}


function RSS_RetrieveLinks($url)
{
global $RSS_Content;

$doc  = new DOMDocument();
$doc->load($url);

$channels = $doc->getElementsByTagName("channel");

$RSS_Content = array();

foreach($channels as $channel)
{
    $items = $channel->getElementsByTagName("item");
    foreach($items as $item)
    {
$y = RSS_Tags($item, 1);    // get description of article, type 1
        array_push($RSS_Content, $y);
    }

}

}


function RSS_Links($url, $size)
{
global $RSS_Content;

$page = "<ul>";

RSS_RetrieveLinks($url);
if($size > 0)
    $recents = array_slice($RSS_Content, 0, $size);

foreach($recents as $article)
{
    $type = $article["type"];
    if($type == 0) continue;
    $title = $article["title"];
    $link = $article["link"];
    $page .= "<li><a href=\"$link\">$title</a></li>\n";         
}

$page .="</ul>\n";

return $page;

}



function RSS_Display($url, $size)
{
global $RSS_Content;
asort($RSS_Content);
$opened = false;
$page = "";

RSS_Retrieve($url);
if($size > 0)
    $recents = array_slice($RSS_Content, 0, $size);

foreach($recents as $article)
{
    $type = $article["type"];
    if($type == 0)
    {
        if($opened == true)
        {
            $page .="</ul>\n";
            $opened = false;
        }
        $page .="<b>";
    }
    else
    {
        if($opened == false) 
        {
            $page .= "<ul>\n";
            $opened = true;
        }
    }
    $title = $article["title"];
    $link = $article["link"];
//  $description = $article["description"];
    $page .= "<p><a href=\"$link\">$title</a>";
//  if($description != false)
    {
        //$page .= "<br>$description";
    }
    $page .= "</p>\n";          

    if($type==0)
    {
        $page .="</b><br />";
    }

}

if($opened == true)
{   
    $page .="</ul>\n";
}
return $page."\n";

}

然后在第二页,我有这个:

$url = "feedurlhere.xml";
    echo RSS_Links($url, 10);
?>
</div>
4

1 回答 1

0

感谢 Scriptol,答案是:将 array_push 的所有实例更改为 array_switch

干杯

于 2013-02-13T20:40:01.300 回答