0

I'm a real beginner with php and I really need help. Here is my code for my first file where I need to call an insertnewmarker function from my second file.

I really dont know how and where to call the function. (the code of the second file is below) I need to insert $name in database

1st file code:

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>

Second file code:

<?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>";

    }

}

?>
4
  • What is your question? You want to call the function from inside the first file? Commented Oct 19, 2011 at 19:20
  • yes, but i dont know how and where to call function insertnewmarker from second file Commented Oct 19, 2011 at 19:22
  • Which file are you trying to call it from? One or two? Commented Oct 19, 2011 at 19:23
  • I try to call second file where is my code for insert into database (function insertnewmarker) in first file. Commented Oct 19, 2011 at 19:25

1 Answer 1

2

[1 file] after:

$longitude = (float) $longitude;

[1 file] add:

include('second_file.php');
insertNewMarker($name, $lat, $lng, $slika, $link, $type, $popust, $vaziDo);

and make sure function arguments $name, $lat, $lng, $slika, $link, $type, $popust, $vaziDo are assigned - i can't help you in this, cause i dont understand your language, anyways it's your job :)

Sign up to request clarification or add additional context in comments.

1 Comment

i think that include is not need becouse i have requie_once on first line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.