doubushi0031 2011-08-29 13:18
浏览 30
已采纳

有没有更有效的方法来编写这个PHP / jQuery

I have this PHP:

<?php

$data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml');

$xml = simplexml_load_string($data);

$data1 = file_get_contents('http://www.skysports.com/rss/0,20514,11661,00.xml');

$xml1 = simplexml_load_string($data1);

$master[0] = $xml;
$master[1] = $xml1;

echo json_encode($master);

?>

And this jQuery:

    var myRSS =[];

function rssReader() {

    console.log('ran');

    $.getJSON('bbc.php', function(data){
        console.log(data);
        $.each(data[0].channel.item, function(index, item){
            // check if item title is stored in the array   
            if (jQuery.inArray(item.title, myRSS) != -1) { 
                //do nothing
            } else {
                // save item title in the array
                myRSS.push(item.title);

                // publish item  
                $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
                $("title").text('EMG: ' + item.title);
                $('#loader').remove();
            }
        });

        $.each(data[1].channel.item, function(index, item){
            // check if item title is stored in the array   
            if (jQuery.inArray(item.title, myRSS) != -1) { 
                //do nothing
            } else {
                // save item title in the array
                myRSS.push(item.title);

                // publish item  
                $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
                $("title").text('EMG: ' + item.title);
            }
        });

    });

}

rssReader();

setInterval(rssReader, 10000);

There seems to be a lot of repeated code and therefore isn't very DRY programming. The returned JSON actually has the same structure, from BBC and Sky Sports so there must be a more efficient way of writing this.

Thanks

  • 写回答

3条回答 默认 最新

  • douyakao5308 2011-08-29 13:27
    关注

    you can cut your jquery down to this(havent tested it there might be a typo):

      var myRSS =[];
    
        function rssReader() {
    
            console.log('ran');
    
            $.getJSON('bbc.php', function(data){
                console.log(data);
                $.each(data[0].channel.item, function(index, item){
                    linkhandler(item)
                    if (jQuery.inArray(item.title, myRSS) == -1) { 
                        $('#loader').remove();
                    }
                });
    
                $.each(data[1].channel.item, function(index, item){
                   linkhandler(item)
                });
    
            });
    
        }
        function linkhandler(item)
        { // check if item title is stored in the array   
           if (jQuery.inArray(item.title, myRSS) == -1) { 
                 // save item title in the array
                 myRSS.push(item.title);
                 $('.container').prepend("<a target='_BLANK' href='" + item.link + "' class='title' data-date='" + item.pubDate + "'>" + item.title + "</a>");
                 $("title").text('EMG: ' + item.title);
           }
        }
        rssReader();
    
        setInterval(rssReader, 10000);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效