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

		try
		{
			$html = file_get_html('https://www.moneycontrol.com/markets/irol.com/markets/indian-indices/');
			$tab = $html->find('table',1);

			if(!$tab)
			{
				throw new Exception('Exception message');
			}


			$indices["INDICES"] = array();
			$first = true;

			foreach($tab->find('table') as $table)
			{
				if($first)
				{
					$sql = "DELETE FROM `indian_indices`";
					$con->query($sql);
					$first = false;
				}

				$name = $table->find('td', 0)->plaintext;
				$price = $table->find('td', 1)->plaintext;
				$change = $table->find('td', 2)->plaintext;
				$changeper = $table->find('td', 3)->plaintext;


				$indice = array(
					'NAME'=>$name,
					'PRICE'=>$price,
					'CHANGE'=>$change,
					'CHANGEPER'=>$changeper
				);
				array_push($indices["INDICES"],$indice);

				$sql = "INSERT INTO `indian_indices` (`name`, `price`, `changeval`,`changeper`) VALUES ('$name', '$price', '$change','$changeper');";
				$con->query($sql);
			}

			$indices["success"] = 1;
			echo json_encode($indices);
		}
		catch(Exception $e) {

			$sql = "SELECT `name` AS 'NAME',`price` AS 'PRICE',`changeval` AS 'CHANGE',`changeper` AS 'CHANGEPER' FROM `indian_indices`;";
			$rows = $con->query($sql); 
			$indices["INDICES"] = array();
			if($rows->num_rows > 0)
			{
				while($row = $rows->fetch_assoc())
				{
					$div = array();
					$div = $row;
					array_push($indices["INDICES"],$div);
				}
				$indices["success"] = 1;
				echo json_encode($indices);
			}
		}
	}
	else
	{
			include_once("ConnectToMarketData.php");
			$sql = "SELECT `name` AS 'NAME',`price` AS 'PRICE',`changeval` AS 'CHANGE',`changeper` AS 'CHANGEPER' FROM `indian_indices`;";
			$rows = $con->query($sql); 
			$indices["INDICES"] = array();
			if($rows->num_rows > 0)
			{
				while($row = $rows->fetch_assoc())
				{
					$div = array();
					$div = $row;
					array_push($indices["INDICES"],$div);
				}
				$indices["succe