<META NAME="robots" CONTENT="noindex,nofollow">


<?php
    include_once("simple_html_dom.php");
	include_once("ConnectToMarketData.php");

	try
	{
		$html = file_get_html("https://finance.yahoo.com/currencies");
		$tbody = $html->find('tbody',0);
		
		if(!$tbody)
		{
			throw new Exception('Exception message');
		}
		$tmp = $tbody->find('tr',0);
		if(!$tmp)
		{
				throw new Exception('Exception message');
		}
		$currencies["CURRENCIES"] = array();
		$first = true;
		
		foreach($tbody->find('tr') as $tr)
		{
			if($first)
			{
				$sql = "DELETE FROM `currencies`";
				$con->query($sql);
				$first = false;
			}
			
			$name = $tr->find('td', 1)->plaintext;
			$price = $tr->find('td', 2)->plaintext;
			$change = $tr->find('td', 3)->plaintext;
			if($name != '')
			{
				$currencie = array(
					'NAME'=>$name,
					'PRICE'=>$price,
					'CHANGE'=>$change
				);
				array_push($currencies["CURRENCIES"],$currencie);
				$sql = "INSERT INTO `currencies` (`name`, `price`, `changeval`) VALUES ('$name', '$price', '$change');";
				$con->query($sql);
			}
		}

		$currencies["success"] = 1;
		echo json_encode($currencies);
	}
	catch(Exception $e) {
		
		$sql = "SELECT `name` AS 'NAME',`price` AS 'PRICE',`changeval` AS 'CHANGE' FROM `currencies`";
		$rows = $con->query($sql); 
		$currencies["CURRENCIES"] = array();
		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($currencies["CURRENCIES"],$div);
			}
			$currencies["succes