<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/cryptocurrencies");
		$tbody = $html->find('tbody',0);
		if(!$tbody)
		{
			throw new Exception('Exception message');
		}
		$tmp = $tbody->find('tr',0);
		if(!$tmp)
		{
				throw new Exception('Exception message');
		}				
		
		$cryptos["CRYPTOS"] = array();
		$first = true;

		foreach($tbody->find('tr') as $tr)
		{
			if($first)
			{
				$sql = "DELETE FROM `cryptocurrencies`";
				$con->query($sql);
				$first = false;
			}
			
			$name = $tr->find('td', 1)->plaintext;
			$price = $tr->find('td', 2)->plaintext;
			$price = '$ '.$price;
			$changeper = $tr->find('td', 4)->plaintext;

			$crypto = array(
				'NAME'=>$name,
				'PRICE'=>$price,
				'CHANGEPER'=>$changeper
			);
			array_push($cryptos["CRYPTOS"],$crypto);
			$sql = "INSERT INTO `cryptocurrencies` (`name`, `price`, `changeval`) VALUES ('$name', '$price', '$changeper');";
			$con->query($sql);
		}

		$cryptos["success"] = 1;
		echo json_encode($cryptos);
	}
	catch(Exception $e) {	
		$sql = "SELECT `name` AS 'NAME',CONCAT('$ ',`price`) AS 'PRICE',`changeval` AS 'CHANGEPER' FROM `cryptocurrencies`";
		$rows = $con->query($sql); 
		$cryptos["CRYPTOS"] = array();

		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($cryptos["CRYPTOS"],$div);
			}
			$cryptos["suc