for循环 - 新手PHP代码问题,求解关于simple_html_dom

WBOY
Release: 2016-06-06 20:28:23
Original
1228 people have browsed it

入门PHP中,不知道为什么循环读取数据无法实现,始终是重复读取同一个页面的数据,高手给指点一下,始终没招到代码的问题。

<code>
<?php header("Content-type: text/html; charset=utf-8"); 
include('simple_html_dom.php');
echo '<ul class="postlist">';
for($page = 1;$page find('.lolcatlist li') as $e){
        $item['url'] = $e->find('a',0)->href;
        $item['title'] = $e->find('h3',0)->plaintext;
        @$item['img'] = $e->find('img',0)->src;
        $articles[] = $item;
            if(!empty($articles[$i]['img'])){
            echo '<li><a href="'.%24articles%5B%24i%5D%5B'url'%5D.'"><img    style="max-width:90%"  style="max-width:90%" src="'.%24articles%5B%24i%5D%5B'img'%5D.'" alt="'.$articles[$i]['title'].'"></a></li>';
            }
        $i++;
    }
}
echo '';
?>
<style>
.postlist{
    list-style:none;
}
.postlist li{padding:20px;border:1px solid #ddd;float:left;}
</style>
 </code>
Copy after login
Copy after login

回复内容:

入门PHP中,不知道为什么循环读取数据无法实现,始终是重复读取同一个页面的数据,高手给指点一下,始终没招到代码的问题。

<code>
<?php header("Content-type: text/html; charset=utf-8"); 
include('simple_html_dom.php');
echo '<ul class="postlist">';
for($page = 1;$page find('.lolcatlist li') as $e){
        $item['url'] = $e->find('a',0)->href;
        $item['title'] = $e->find('h3',0)->plaintext;
        @$item['img'] = $e->find('img',0)->src;
        $articles[] = $item;
            if(!empty($articles[$i]['img'])){
            echo '<li><a href="'.%24articles%5B%24i%5D%5B'url'%5D.'"><img    style="max-width:90%"  style="max-width:90%" src="'.%24articles%5B%24i%5D%5B'img'%5D.'" alt="'.$articles[$i]['title'].'"></a></li>';
            }
        $i++;
    }
}
echo '';
?>
<style>
.postlist{
    list-style:none;
}
.postlist li{padding:20px;border:1px solid #ddd;float:left;}
</style>
 </code>
Copy after login
Copy after login

你用querylist吧,simplehtmldom太耗内存了

$articles在foreach前重新声明一下

<code>    $articles=array();
    foreach($html->find('.lolcatlist li') as $e){
        $item['url'] = $e->find('a',0)->href;
        $item['title'] = $e->find('h3',0)->plaintext;
        @$item['img'] = $e->find('img',0)->src;
        $articles[] = $item;
        if(!empty($articles[$i]['img'])){
            echo '<li><a href="'.%24articles%5B%24i%5D%5B'url'%5D.'"><img    style="max-width:90%"  style="max-width:90%" src="'.%24articles%5B%24i%5D%5B'img'%5D.'" alt="'.$articles[$i]['title'].'"></a></li>';
        }
        $i++;
    }</code>
Copy after login

你把$html = file_get_html($url);打印出来就晓得了。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template