入门PHP中,不知道为什么循环读取数据无法实现,始终是重复读取同一个页面的数据,高手给指点一下,始终没招到代码的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <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 src="/static/imghw/default1.png" data-src="' . $articles [ $i ][ 'img' ]. '" class="lazy" style="max-width:90%" style="max-width:90%" . 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中,不知道为什么循环读取数据无法实现,始终是重复读取同一个页面的数据,高手给指点一下,始终没招到代码的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <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 src="/static/imghw/default1.png" data-src="' . $articles [ $i ][ 'img' ]. '" class="lazy" style="max-width:90%" style="max-width:90%" . 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前重新声明一下
1 2 3 4 5 6 7 8 9 10 11 | <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 src="/static/imghw/default1.png" data-src="' . $articles [ $i ][ 'img' ]. '" class="lazy" style="max-width:90%" style="max-width:90%" . alt="' . $articles [ $i ][ 'title' ]. '"></a></li>' ;
}
$i ++;
}</code>
|
로그인 후 복사
你把$html = file_get_html($url);
打印出来就晓得了。