<META NAME="robots" CONTENT="noindex,nofollow">


<?php
	include_once("simple_html_dom.php");
	include_once("ConnectToMarketData.php");    
	try
	{	
		$html = file_get_html("https://money.rediff.com/companies/market-capitalisation");
		$tbody = $html->find('tbody',0);
		if(!$tbody)
		{
			throw new Exception('Exception message');
		}
		$gainers["MCAP"] = 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_market_capital`";
				$con->query($sql);
				$first = false;
			}
			$name = trim($tr->find('td', 0)->plaintext);
			$price = trim($tr->find('td', 1)->plaintext);
			$changeper = str_replace(' ','',trim($tr->find('td', 2)->plaintext));
			$volume = trim($tr->find('td', 3)->plaintext);
			$equity = trim($tr->find('td', 4)->plaintext);
			$fv = trim($tr->find('td', 5)->plaintext);
			$marketCap = trim($tr->find('td', 6)->plaintext);
			$marketCap = $marketCap.' Cr';
	
			$gainer = array(
				'NAME'=>$name,
				'PRICE'=>$price,
				'CHANGEPER'=>$changeper,
				'VOLUME'=>$volume,
				'EQUITY'=>$equity,
				'FV'=>$fv,
				'MARKET_CAP'=>$marketCap
			);
			
			$sql = "INSERT INTO `top_market_capital` (`name`, `cur_price`, `changeper`, `volume`, `equity`, `fv`, `market_cap`) VALUES ('$name', '$price', '$changeper', '$volume', '$equity', '$fv', '$marketCap');";
			$con->query($sql);
			array_push($gainers["MCAP"],$gainer);
		}
		
		$gainers["success"] = 1;
		echo json_encode($gainers);
	}
	catch(Exception $e) {
  			
		$sql = "SELECT `name` AS 'NAME',`cur_price` AS 'PRICE',`changeper` AS 'CHANGEPER',`volume` AS 'VOLUME',`equity` AS 'EQUITY',`fv` AS 'FV',`market_cap` AS 'MARKET_CAP' FROM `top_market_capital`;";

		$rows = $con->query($sql); 
		$gainers["MCAP"] = array();
		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($gainers["MCAP"],$div);
			}

			$gainers["success"