首先我需要提到我是编码新手,所以请记住这一点。我渴望学习并且总是在提问之前尝试查找内容,这是我在这里的第一篇文章。
我想做的是从我的数据库中提取一个 url 列表并获取某些值以添加到我的数据库中。当我遍历代码时,它会正确抓取所有内容并从每个 url 获取所有信息,但是当需要将信息放入数据库时,它会一遍又一遍地写入数组的第一个值,而不是写入下一个值。示例:我有 3 个网址,它会提取所有三个网址的信息
Array
(
[0] => Array
(
[title] => Data
[promo] => Data
[date] => Data
[location] => Data
)
[1] => Array
(
[title] => Data
[promo] => Data
[date] => Data
[location] => Data
)
[2] => Array
(
[title] => Data
[promo] => Data
[date] => Data
[location] => Data
)
)
当我检查我的数据库时,第一个数据集在那里 3 次,第二个和第三个被忽略。我附上了下面的代码,希望有人能指出我正确的方向。
谢谢。
include 'simple_html_dom.php';
$select_db = mysql_connect('******','*******', '');
$connect_database = mysql_select_db('grab', $select_db);
$url=mysql_query("SELECT * FROM graber");
while($row = mysql_fetch_array($url))
{
// Create DOM from URL or file
$html = file_get_html($row['urls']);
// Find all article blocks
foreach($html->find('div.event_detail') as $article)
{
$item['title'] = $article->find('h1', 0)->plaintext;
$item['promo'] = $article->find('h2', 0)->plaintext;
$item['date'] = $article->find('div.authors_info span.date', 0)->plaintext;
$item['location'] = $article->find('div.authors_info span.author', 0)->plaintext;
$articles[] = $item;
}
$address=urlencode($articles[0]['location']);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$title = $articles[0]['title'];
$location = $articles[0]['location'];
$promo = $articles[0]['promo'];
$olddate = $articles[0]['date'];
$date = date('Y-m-d H:i:s', strtotime($olddate));
$today = date('Y-m-d H:i:s');
echo $lat.'<br />'.$long.'<br />'.$title.'<br />'.$location.'<br />'.$promo.'<br />'.$date.'<br /><br />';
$dbc = mysqli_connect('********','*****','*******','*********')
or die('Error I could not connect to DB');
$query="INSERT INTO events(`id`,`parent`,`catid`,`contentid`,`type`,`title`,`location`,`summary`,`description`,`creator`,`startdate`,`enddate`,`permission`,`avatar`,`thumb`,`invitedcount`,`confirmedcount`,`declinedcount`,`maybecount`,`wallcount`,`ticket`,`allowinvite`,`created`,`hits`,`published`,`latitude`,`longitude`,`offset`,`allday`,`repeat`,`repeatend`)".
"VALUES('','0','1','0','profile','$title','$location','','$promo','24','$date','$date','0',NULL,NULL,'0','0','0','0','0','0','1','$today','0','1','$lat','$long','-6','1',NULL,'0000-00-00')";
$results=mysqli_query($dbc,$query);
echo mysqli_error($dbc);
}
mysqli_close($dbc);
mysql_close($select_db);