<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/52-week-high");
			$tbody = $html->find('tbody',0);
			if(!$tbody)
			{
				throw new Exception('Exception message');
			}
			$gainers["52HIGH"] = 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 `52_week_high`";
					$con->query($sql);
					$first = false;
				}
				$cname = $tr->find('td', 1)->plaintext;
				$ltp = $tr->find('td', 2)->plaintext;
				$ltp = $ltp.' [+]';
				$dayhigh = $tr->find('td', 3)->plaintext;

				$gainer = array(
					'NAME'=>$cname,
					'LTP'=>$ltp,
					'HIGH'=>$dayhigh
				);

				$sql = "INSERT INTO `52_week_high` (`cname`, `lprice`, `dayhigh`) VALUES ('$cname', '$ltp', '$dayhigh');";
				$con->query($sql);
				array_push($gainers["52HIGH"],$gainer);
			}

			$gainers["success"] = 1;
			echo json_encode($gainers);
		}
		catch(Exception $e) {

			$sql = "SELECT `cname` AS 'NAME',`lprice` AS 'LTP',`dayhigh` AS 'HIGH' FROM `52_week_high`";
			$rows = $con->query($sql); 
			$gainers["52HIGH"] = array();
			if($rows->num_rows > 0)
			{
				while($row = $rows->fetch_assoc())
				{
					$div = array();
					$div = $row;
					array_push($gainers["52HIGH"],$div);
				}

				$gainers["success"] = 1;
				echo json_encode($gainers);
			}
		}
	}
	else
	{
		include_once("ConnectToMarketData.php");
		$sql = "SELECT `cname` AS 'NAME',`lprice` AS 'LTP',`dayhigh` AS 'HIGH' FROM `52_week_high`";
		$rows = $con->query($sql); 
		$gainers["52HIGH"] = array();
		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($gainers["52HIGH"],$div);
			}
			$gainers["success"