2011-03-10 130 views
1

我想插入一些值从第一和第二foreach到数据库,但我遇到了一些麻烦。我在代码中写下我的问题。我无法解决双回路问题。我请求帮助。PHP foreach在foreach问题

<?php 
header('Content-type:text/html; charset=utf-8'); 
set_time_limit(0); 
require_once ('../conn.php'); 
require_once ('../simple_html_dom.php'); 
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=obama&key={api-key}"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, $url); 
$body = curl_exec($ch); 
curl_close($ch); 
$data = json_decode($body); 
foreach ($data->responseData->results as $result) { 
$title = html_entity_decode($result->titleNoFormatting); 
$link = html_entity_decode($result->unescapedUrl); 
$html = @file_get_html($link); 
foreach(@$html->find('h3') as $element) { 
    $table=$element; 
     echo $table;// here while the $table is empty, echo is null. 
} 
echo $table;// here while the $table is empty, echo will repeat the prev $table value. 
mysql_query("SET NAMES utf8"); 
mysql_query("INSERT INTO ...");// I want insert all the $title and $table into database. 
} 
echo '<hr />'; 
} 
?> 

我打印结果while the $table is empty, echo will repeat the prev $table value

Organizing for America | BarackObama.com 

Barack Obama - Wikipedia, the free encyclopedia 

President Barack Obama | The White House 
President Obama Nominates William Francis Kuntz, II to the United States District Court//the prev value 

Change.gov - The Official Web Site of the 
President Obama Nominates William Francis Kuntz, II to the United States District Court//here the $table is empty, it will repeat the prev $table value, and it should be empty. 

Barack Obama on Myspace 
Idle Friends▼ 

ob (obama) on Twitter 
Piè di pagina 

Barack Obama 
Advertise with the NY Daily News! 

Barack Obama on the Issues 
Voting Record 
+5

这里没问题,还是我错了?只是看到一堆代码。 – KingCrunch 2011-03-10 20:20:27

+0

那么,你的问题是什么?发生了什么事情,不应该发生什么或不应该发生什么? – 2011-03-10 20:29:35

+0

@Rocket,如果我想插入到数据库中,我将使用第二个'echo $ table;'(在第一个foreach循环中)。但是当我打印结果时,第二位总统奥巴马提名威廉弗朗西斯昆兹二世去美国地区法院//这里的美元表应该是空的,但它会重复美元表的价值并回应出来。不正确。 – cj333 2011-03-10 20:34:01

回答

6

PHP的变量初始化和范围规则很有趣。

在任何时候你初始化$table。它首先被引用两个foreach深。 PHP允许这样做,而不会抱怨它。

问题是,你不断尝试将其设置为一个值,但你从来没有真正重置它。内foreach

它初始化为null

$html = file_get_html($link); 
$table = null; // <-- New! 
foreach($html->find('h3') as $element) { 
    $table = $element; 
    echo $table; 
} 

这确保了,当完成了foreach$table要么是null,或者这将是HTML文档中的最后H3元素你取。 (顺便说一句,如果你真的想在最终H3,你可能只抢阵列find回报,并期待在最后一个元素,而不是循环通过。)

另外,请摆脱@错误沉默运营商,一直打开error_reporting,并确保已打开display_errors。你可能有其他错误潜伏,你故意忽略,并导致恐怖故事。

+0

太好了,你解决了我的问题,非常感谢。现在结果不会重复prev值。 :} – cj333 2011-03-10 20:59:27

+0

'$ html = @file_get_html($ link);'在这里,我想避免$ link是不是html链接,比如rss,pdf。所以'file_get_html($ link);'会报错,然后停止整个php脚本。我认为,如果它不是一个真正的html链接,'$ table'将插入一个空值到数据库中。 – cj333 2011-03-10 21:09:51

+0

当然有更好的方法来确定这些信息而不是压制错误? 'file_get_html'仅仅是一个'file_get_contents',然后将结果字符串加载到一个新的Simple HTML DOM对象中。您可以自己执行该步骤,在创建对象之前检测HTML。 – Charles 2011-03-10 22:47:09