<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/index.html");
			$div = $html->find('div[class=div_secto_tabs]',0);
			if(!$div)
				throw new Exception('Exception message');
			$gainers["INDICES"] = array();
			$first = true;

			foreach($div->find('a') as $a)
			{
				if($first)
				{
					$sql = "DELETE FROM `sectoral_indices`";
					$con->query($sql);
					$first = false;
				}

				$sector = $a->find('b', 0)->plaintext;
				$ltp = $a->find('h5', 0)->plaintext;
				$chngper = $a->find('h6', 0)->plaintext;

				$gainer = array(
					'SECTOR'=>$sector,
					'LTP'=>$ltp,
					'CHANGEPER'=>$chngper
				);
				$sql = "INSERT INTO `sectoral_indices` (`sector`, `ltrade`, `changeper`) VALUES ('$sector', '$ltp', '$chngper');";
				$con->query($sql);
				array_push($gainers["INDICES"],$gainer);
			}

				$gainers["success"] = 1;
				echo json_encode($gainers);
			}
		catch(Exception $e)
		{
				$sql = "SELECT `sector` AS 'SECTOR',`ltrade` AS 'LTP',`changeper` AS 'CHANGEPER' FROM `sectoral_indices`";

				$rows = $con->query($sql); 
				$gainers["INDICES"] = array();
				if($rows->num_rows > 0)
				{
					while($row = $rows->fetch_assoc())
					{
						$div = array();
						$div = $row;
						array_push($gainers["INDICES"],$div);
					}

					$gainers["success"] = 1;
					echo json_encode($gainers);
				}
		}
	}
	else
	{
				include_once("ConnectToMarketData.php");    
				$sql = "SELECT `sector` AS 'SECTOR',`ltrade` AS 'LTP',`changeper` AS 'CHANGEPER' FROM `sectoral_indices`";

				$rows = $con->query($sql); 
				$gainers["INDICES"] = array();
				if($rows->num_rows > 0)
				{
					while($row = $rows->fetch_assoc())
					{
						$div = array();
						$div = $row;
						array_push($gainers["INDICES"],$div);
					}

					$gainers["success"] = 1;