<?php
class Country{

	    private $db; // پہلے declare کریں

	public function __construct() {
        $this->db = new Database;
	 }
	 public function checkCountry(){
	 	$real_ip_adress = $_SERVER['REMOTE_ADDR'];
	 	$url_ip = "https://api.hostip.info/country.php?ip=".$real_ip_adress;
	   $xml = file_get_contents($url_ip);
	  	$this->db->query('SELECT * FROM countries WHERE ip_com = :id');
        $this->db->bind(':id', $xml);
         $row = $this->db->single();
        $count = $this->db->rowCount();
        if($count < 1){
        	return false;
        }else{
        	return true;
        }
	 }
	 public function SaveCountry($data_array){

	 	$this->db->query('SELECT * FROM countries WHERE ip_com = :id');
        $this->db->bind(':id', $data_array['country']);
        $row = $this->db->single();
         $count = $this->db->rowCount();
         if($count < 1){
         	$this->db->query('INSERT INTO countries(ip_com) VALUES (:con)');
            $this->db->bind(':con',$data_array['country']);
            $this->db->execute();
         }	
	 	return true;	
	 }

	 public function loadCountry(){

	 	 $this->db->query('SELECT * FROM countries');
        $result = $this->db->resultSet();
        $order_id = 1;
        foreach ($result as $value) {
        	$value->RecordID = $value->id;
        	$value->OrderID = "49349-38".$order_id++;

        	$value->address = $value->ip_com;
        	$value->RecordID = $value->id;
        	$value->OrderID = "49349-38".$value->id;
        }
        
        $count = count($result);
        $data = array('recordsTotal'=>$count,'recordsFiltered'=>$count,'data'=>$result);
        return $data;

	 }
}