需要使用PHP从两个XML文件中显示日期

时间:2011-11-16 18:46:31

标签: php xml multidimensional-array rss

我正在尝试找到一种方法来显示来自两个XML链接的数据。一个来自[vworker.com] [1],另一个来自[freelancer.com] [2]。

我需要的是显示来自自由职业者和vworker等的第一个链接。

以下是我用来获取和显示数据的内容,而分页也无效。

function getFeeds() {

$freeBook = file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_book-writing.xml");
$freeSub = file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Article-Submission.xml");
$freeBlog= file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Blog.xml");
$freeRewit= file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Article-Rewriting.xml");

$vBlog= file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Article-Submission.xml");
$vSub= file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Article-Submission.xml");
$vbook= file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Article-Submission.xml");
$vRewit= file_get_contents("http://www.freelancer.com/rss/affiliatejob_mister17fyve_Article-Submission.xml");

$freeB = new SimpleXMLElement($freeBook);
$freeS = new SimpleXMLElement($freeSub);
$freeBl = new SimpleXMLElement($freeBlog);
$freeR = new SimpleXMLElement ($freeRewit);

$vBl = new SimpleXMLElement($vBlog);
$vB = new SimpleXMLElement($vbook);
$vS = new SimpleXMLElement($vSub);
$vR = new SimpleXMLElement($vRewit);

$bookary = $freeB->channel->item;
$blogary = $freeBl->channel->item;
$rewitary = $freeR->channel->item;
$subary = $freeS->channel->item;

$vbookary = $vB->channel->item;
$vblogary = $vBl->channel->item;
$vsary = $vS->channel->item;
$vrary = $vR->channel->item;

echo $total =  count($blogary) + count($bookary) + count($rewitary) + count($subary) + count($vblogary) + count($vbookary) + count($vsary) + count($vrary);

$adjacents = 5;
$targetpage = "freelancer.php";     //your file name  (the name of this file)
$limit = 20;
$page = $_GET['page'];

if($page) 
{
    $start = ($page - 1) * $limit;  
}//first item to display on this page
else
{
    $start = 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/$limit);        //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;                      //last page minus 1

$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>\n";       
}
echo "<ul>";
$ip = 0;
foreach($bookary as $bookl)
{

    echo "<li>
                <div style='margin:15px 0px 2px 0px;'>
                <a style='color:red;font-weight:bold;padding:0px' href='".$bookl->link."'>".$bookl->title." ".$bookl->pubDate."</a>
                    <div style='padding-left:5px;float:right;font-size:9px;'>
                        Job Source:Freelancer 
                    </div> 
                <div style='font-weight:normal;font-size:12px;'>".$bookl->description."</div>   
                </div>              
                <br/>           
                <div style='margin:15px 0px 2px 0px;'>
                <a style='color:red;font-weight:bold;padding:0px' href='".$blogary[$i]['link']."'>".$blogary[$i]['title']." ".$blogary[$i]['pubDate']."</a>
                    <div style='padding-left:5px;float:right;font-size:9px;'>
                        Job Source:Vworker 
                    </div> 
                <div style='font-weight:normal;font-size:12px;'>".$blogary[$i]['description']."</div>   
                </div>


        </li>";
}
echo "</ul>";
echo $pagination;

}

1 个答案:

答案 0 :(得分:0)

制作RSS的两个数组并循环抛出一个并在第二个

中放置一个计数器
function getFeed($feed_url,$feed2) {

$content = file_get_contents($feed_url);

$x = new SimpleXmlElement($content);
$content2 = file_get_contents($feed2);

$x2 = new SimpleXmlElement($content2);
$arr = $x2->channel->item;
echo "<ul>";
$i = 0; 
foreach($x->channel->item as $entry) {

    echo "
    <li>
      <a href='$entry->link' title='$entry->title' target='_new'> " . $entry->title . "</a>
    </li>"
    <li>
      <a href='".$arr[$i]['link']."' title='".$arr[$i]['title']."' target='_new'> " . $arr[$i]['title'] . "</a>
    </li>";
 $i++; 
}

echo "</ul>";
}