我是一个真正的初学者,我真的需要帮助。这是我的第一个文件的代码,我需要从第二个文件中调用insertnewmarker函数。
我真的不知道如何以及在何处调用该函数。 (第二个文件的代码如下)我需要在数据库中插入$name
第一个文件代码:
require_once('includes/php.config.db.php');
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
libxml_use_internal_errors(true);
$dom = new DOMDocument();
@$dom->loadHTMLFile('http://www.kupime.com/aktivne-ponude/');
$xpath = new DOMXPath($dom);
$entries = $xpath->query("//ul[@id='list_offers']//table//a/@href");
$output = array();
$i = 1;
foreach($entries as $e) {
$dom2 = new DOMDocument();
@$dom2->loadHTMLFile('http://www.kupime.com' . $e->textContent);
$xpath2 = new DOMXPath($dom2);
$data = array();
$data['link']= ('http://www.kupime.com' . $e->textContent);
$data['naslov'] = trim($xpath2->query("//h1[@id='offer_title']")->item(0)->textContent);
$data['opis'] = trim($xpath2->query("//div[@id='company_info']")->item(0)->textContent);
$data['slika'] = trim($xpath2->query("//div[@id='img_border']/img/@src")->item(0)->textContent);
$location = $xpath2->query("//div[@id='location']//script")->item(1)->textContent;
preg_match('/var\s+lat\s+=\s+(\d+\.\d+)\s*;/', $location, $lat);
preg_match('/var\s+lang\s+=\s+(\d+\.\d+)\s*;/', $location, $lng);
$data['lat'] = $lat[1];
$data['lng'] = $lng[1];
$data['popust'] = trim($xpath2->query("//li[@class='discount']")->item(0)->textContent);
$data['firma'] = trim($xpath2->query("//div[@id='company_info']/h3")->item(0)->textContent);
$data['telefon'] = trim($xpath2->query("//div[@id='company_info']/p[2]")->item(0)->textContent);
$data['email'] = trim($xpath2->query("//div[@id='company_info']/p[3]/a[1]/@href")->item(0)->textContent);
$data['sajt'] = trim($xpath2->query("//div[@id='company_info']/p[3]/a[2]/@href")->item(0)->textContent);
//vreme
$data['sat'] = trim($xpath2->query("//div[@class='countdown hasCountdown']/strong[1]")->item(0)->textContent);
$data['minut'] = trim($xpath2->query("//div[@class='countdown hasCountdown']/strong[2]")->item(0)->textContent);
$data['sekund'] = trim($xpath2->query("//div[@class='countdown hasCountdown']/strong[3]")->item(0)->textContent);
$data['vreme'] = time() + $data['sekund']+ $data['minut']*60 + $data['sat']*3600;
$image_url = 'http://www.kupime.com'.$data['slika'];
$naslov = $data['naslov'];
$latitude = $data['lat'];
$longitude = $data['lng'];
$latitude = (float) $latitude;
$lin = $data['link'];
$vreme = $data['vreme'];
$popust = str_replace ('POPUST','',$data['popust']);
$firma = $data['firma'];
$telefon = $data['telefon'];
$email = $data['email'];
$sajt = $data['sajt'];
$type = 'bar';
$latitude = (float) $latitude;
$longitude = (float) $longitude;
$output[] = $data;
}
?>
</body>
</html>
第二个文件代码:
<?php
error_reporting(true);
$link = mysql_connect('localhost', 'user', 'pass') or die('<p>Connection imposible!</p>');
mysql_select_db('tablename', $link);
mysql_query("SET NAMES utf8");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'");
function insertNewMarker($name='', $lat='', $lng='', $slika='', $link='', $type='', $popust='', $vaziDo='') {
if(!empty($name) and !empty($lat) and !empty($lng) and !empty($link)) {
$name = mysql_real_escape_string(trim($name));
$sql = "INSERT INTO `markers` (`name`, `lat`, `lng`, `address`, `totolink`, `type`, `popust`, `vaziDo`) VALUES ('$name', '$lat', '$lng', '$slika', '$link', '$type', '$popust', '$vaziDo')";
mysql_query($sql) or print "<p>Error in SQL Statement ($sql):<br />". mysql_error() .'</p>';
}
else {
print "<p>Polja 'name', 'lat' i 'lng' can't be empty!<br />Vi ste uneli <br />Name:$name<br />Lat:$lat<br />Lng:$lng</p>";
}
}
?>
答案 0 :(得分:2)
[1档案]之后:
$longitude = (float) $longitude;
[1个档案]添加:
include('second_file.php');
insertNewMarker($name, $lat, $lng, $slika, $link, $type, $popust, $vaziDo);
并确保分配了函数参数$name, $lat, $lng, $slika, $link, $type, $popust, $vaziDo
- 我无法帮助你,因为我不懂你的语言,无论如何这都是你的工作:)