我想插入一些值从第一和第二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
这里没问题,还是我错了?只是看到一堆代码。 – KingCrunch 2011-03-10 20:20:27
那么,你的问题是什么?发生了什么事情,不应该发生什么或不应该发生什么? – 2011-03-10 20:29:35
@Rocket,如果我想插入到数据库中,我将使用第二个'echo $ table;'(在第一个foreach循环中)。但是当我打印结果时,第二位总统奥巴马提名威廉弗朗西斯昆兹二世去美国地区法院//这里的美元表应该是空的,但它会重复美元表的价值并回应出来。不正确。 – cj333 2011-03-10 20:34:01