<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://ticker.finology.in/market");
			$div = $html->find('div[id=mainContent_MostActivevalue]',0);
			$tbody = $html->find('tbody',1);
			if(!$tbody)
			{
				throw new Exception('Exception message');
			}
			$gainers["MOSTACTIVE"] = array();
			$tmp = $tbody->find('tr',0);
			if(!$tmp)
			{
					throw new Exception('Exception message');
			}
			$first = true;

			foreach($tbody->find('tr') as $tr)
			{
				if($first)
				{
					$sql = "DELETE FROM `most_active_val`";
					$con->query($sql);
					$first = false;
				}
				$cname = trim($tr->find('td', 0)->plaintext);
				$val = $tr->find('td', 1)->plaintext;

				$gainer = array(
					'NAME'=>$cname,
					'VAL'=>$val
				);

				$sql = "INSERT INTO `most_active_val` (`cname`, `val`) VALUES ('$cname', '$val');";
				$con->query($sql);
				array_push($gainers["MOSTACTIVE"],$gainer);
			}

			$gainers["success"] = 1;
			echo json_encode($gainers);
		}
		catch(Exception $e) {

			$sql = "SELECT `cname` AS 'NAME',`val` AS 'VAL' FROM `most_active_val`";
			$rows = $con->query($sql); 
			$gainers["MOSTACTIVE"] = array();
			if($rows->num_rows > 0)
			{
				while($row = $rows->fetch_assoc())
				{
					$div = array();
					$div = $row;
					array_push($gainers["MOSTACTIVE"],$div);
				}

				$gainers["success"] = 1;
				echo json_encode($gainers);
			}
		}
	}
	else
	{
		include_once("ConnectToMarketData.php");
		$sql = "SELECT `cname` AS 'NAME',`val` AS 'VAL' FROM `most_active_val`";
		$rows = $con->query($sql); 
		$gainers["MOSTACTIVE"] = array();
		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($gainers["MOSTACTIVE"],$div);
			}
			$gainers["success"