dpcj32769 2016-12-18 18:54
浏览 57
已采纳

带有select(选项)框的RSS php阅读器

I create this PHP file. But this read only one link. How can I add the other two? The selection box read on page only one link ... http://www.kurir.rs/rss/vesti/"
http://www.blic.rs/rss/IT

<form action="index.php" method="POST">
    <select name="rss">
        <option value="http://www.kurir.rs/rss/vesti/">Kurir</option>
        <option value="http://www.blic.rs/rss/IT">Blic</option>
        <option value="http://www.b92.net/info/rss/tehnopolis.xml">B92</option>
    </select>
    <input type="submit" value="Select" />
</form>

<?php
$rss = new DOMDocument();
$rss->load('http://www.b92.net/info/rss/tehnopolis.xml');

$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array ( 
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        );
    array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p>'.$description.'</p>';
}

?>

  • 写回答

1条回答 默认 最新

  • dpwjx32578146 2016-12-18 20:28
    关注

    The simple answer, you need to use what is posted from your form to load the page. So something like this:

    $rss_url = isset($_REQUEST['rss']) ? $_REQUEST['rss'] : 'http://www.b92.net/info/rss/tehnopolis.xml';
    $rss = new DOMDocument();
    $rss->load( $rss_url );
    

    I even threw some validation in there to check if $_REQUEST['rss'] is set.

    Is this the best way to do this? no. You would need to further validate your input so people can post something unexpected. Also using POST, may be unnecessary for this. GET might work just fine. But for this exercise, it will work.

    Also, if you wanted the option box to show the selected url:

    <form action="index.php" method="POST">
        <select name="rss">
    
    
    <?php
    $selection = array (
        'Kurir' => 'http://www.kurir.rs/rss/vesti/', 
        'Blic' => 'http://www.blic.rs/rss/IT', 
        'B92' => 'http://www.b92.net/info/rss/tehnopolis.xml' );
    
    foreach ($selection as $title => $url) {
        if(! empty($_REQUEST) and isset($_REQUEST['rss']) and $_REQUEST['rss'] == $url ){
            $selected = 'selected';       
        } else {
            $selected = '';
        }
        print'<option value="'.$url.'" '.$selected.'>'.$title.'</option>';
        print "
    ";
    }
    
    
    ?>
    
        </select>
        <input type="submit" value="Select" />
    </form>
    
    <?php
    $rss_url = isset($_REQUEST['rss']) ? $_REQUEST['rss'] : 'http://www.b92.net/info/rss/tehnopolis.xml';
    print $rss_url;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的