<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://money.rediff.com/gainers/nse");
			$html1 = file_get_html("https://money.rediff.com/gainers/bse");
			$tbody = $html->find('tbody',0);
			if(!$tbody)
			{
				throw new Exception('Exception message');
			}
			$gainers["GAINERS"] = 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 `top_gainer`";
					$con->query($sql);
					$first = false;
				}
				$sys = '.NS';
				$name = $tr->find('td', 0)->plaintext;
				$name = str_replace('\t', '', $name);
				$name = trim($name);
				$prePrice = $tr->find('td', 1)->plaintext;
				$price = $tr->find('td', 2)->plaintext;
				$price = trim($price);
				$changeper = $tr->find('td', 3)->plaintext;
				$change = ($prePrice*substr($changeper, 1))/100;
				$change = round($change, 2);
				$change = ''.$change;
				$changeper = trim($changeper).'%';

				$gainer = array(
					'SYMBOL'=>$sys,
					'NAME'=>$name,
					'PRICE'=>$price,
					'CHANGE'=>$change,
					'CHANGEPER'=>$changeper
				);

				$sql = "INSERT INTO `top_gainer` (`symbol`, `name`, `price`, `changeprice`, `changeper`) VALUES ('$sys', '$name', '$price', '$change', '$changeper');";
				$con->query($sql);
				array_push($gainers["GAINERS"],$gainer);
			}

			//BSE
			$tbody = $html1->find('tbody',0);
			if(!$tbody)
			{
				throw new Exception('Exception message');
			}
			$tmp = $tbody->find('tr',0);
			if(!$tmp)
			{
					throw new Exception('Exception message');
			}
			foreach($tbody->find('tr') as $tr)
			{
				$sys = '.BO';
				$name = $tr->find('td', 0)->plaintext;
				$name = str_replace('\t', '', $name);
				$name = trim($name);
				$prePrice = $tr->find('td', 2)->plaintext;
				$price = $tr->find('td', 3)->plaintext;
				$price = trim($price);
				$changeper = $tr->find('td', 4)->plaintext;
				$change = ($prePrice*substr($changeper, 1))/100;
				$change = round($change, 2);
				$change = ''.$change;
				$changeper = trim($changeper).'%';
				$changeper = str_replace(' ', '', $changeper);

				$gainer = array(
					'SYMBOL'=>$sys,
					'NAME'=>$name,
					'PRICE'=>$price,
					'CHANGE'=>$change,
					'CHANGEPER'=>$changeper
				);

				$sql = "INSERT INTO `top_gainer` (`symbol`, `name`, `price`, `changeprice`, `changeper`) VALUES ('$sys', '$name', '$price', '$change', '$changeper');";
				$con->query($sql);
				array_push($gainers["GAINERS"],$gainer);
			}


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

			$sql = "SELECT `symbol` AS 'SYMBOL',`name` AS 'NAME',`price` AS 'PRICE',`changeprice` AS 'CHANGE',`changeper` AS 'CHANGEPER' FROM `top_gainer`";
			$rows = $con->query($sql); 
			$gainers["GAINERS"] = array();
			if($rows->num_rows > 0)
			{
				while($row = $rows->fetch_assoc())
				{
					$div = array();
					$div = $row;
					array_push($gainers["GAINERS"],$div);
				}

				$gainers["success"] = 1;
				echo json_encode($gainers);
			}
		}
	}
	else
	{
		include_once("ConnectToMarketData.php");
		$sql = "SELECT `symbol` AS 'SYMBOL',`name` AS 'NAME',`price` AS 'PRICE',`changeprice` AS 'CHANGE',`changeper` AS 'CHANGEPER' FROM `top_gainer`";
		$rows = $con->query($sql); 
		$gainers["GAINERS"] = array();
		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($gainers["GAINERS"],$div);
			}
			$gainers["suc