入门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>
入门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>
你用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>
你把$html = file_get_html($url);
打印出来就晓得了。