duannaoben8011 2013-05-10 09:27 采纳率: 0%
浏览 50
已采纳

在foreach只有回声的最后一项?

I am trying to extract download links from a site. However i only get the last item in my array.

<?php
    require 'functions/simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
    $page_title = $html->find('title',0);
    ?>

    Title:<?php echo $page_title->plaintext; ?><br><br>
    Links:<br>
    <?php
    foreach($html->find('td.tlistdownload a') as $links){
        $dllinks[] = $links->href;
    }
    foreach($html->find('td.tlistname a') as $names){
        echo '<a href="'; 
        foreach ($dllinks as $value)
        {
            echo $value;
        }
        echo '">' . $names->innertext . '</a><br>';
    }
    foreach ($dllinks as $value)
    {
        echo $value . '<br>';
    } 
?>

When I use var_dump it shows all the download links in my array. But for some strange reason it only shows the last item in the second foreach loop.


EDIT:
Sorry it was supposed to be like this

    <?php
    require 'functions/simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
    $page_title = $html->find('title',0);
    ?>

    Title:<?php echo $page_title->plaintext; ?><br><br>
    Links:<br>
    <?php
    foreach($html->find('td.tlistdownload a') as $links){
        $dllinks[] = $links->href;
    }
    foreach($html->find('td.tlistname a') as $names){
        echo '<a href="'; 
        foreach ($dllinks as $value)
        {
            echo $value;
        }
        echo '">' . $names->innertext . '</a><br>';
    }
?>

展开全部

  • 写回答

1条回答 默认 最新

  • douxue7196 2013-05-10 09:44
    关注

    I kept this verbose so its easier to see whats going on... Basically, grab each row.. Find the name and the link from the row. spit it out..

    <?php
    require 'functions/simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://www.nyaa.eu/?page=torrents&user=64513');
    $page_title = $html->find('title',0);
    ?>
    
    Title:<?php echo $page_title->plaintext; ?><br><br>
    Links:<br>
    <?php
    foreach($html->find('.tlistrow') as $row){
        $link_nodes = $row->find('td.tlistdownload a');
        $name_nodes = $row->find('td.tlistname a');
        if (count($link_nodes) > 0 && count($name_nodes) > 0) {
          $link = $link_nodes[0]->href;
          $name = htmlentities($name_nodes[0]->innertext);
          echo "<a href='{$link}'>{$name}</a>
    ";
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部