<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/most-traded/nse");
		$tbody = $html->find('tbody',0);
		if(!$tbody)
		{
			throw new Exception('Exception message');
		}
		$tmp = $tbody->find('tr',0);
		if(!$tmp)
		{
				throw new Exception('Exception message');
		}
		
		$mosts["MOSTS"] = array();
		$first = true;
		
		foreach($tbody->find('tr') as $tr)
		{
			if($first)
			{
				$sql = "DELETE FROM `most_active`";
				$con->query($sql);
				$first = false;
			}
			
			$name = $tr->find('td', 0)->plaintext;
			$price = $tr->find('td', 1)->plaintext;
			$change = $tr->find('td', 2)->plaintext;

			$most = array(
				'NAME'=>$name,
				'PRICE'=>$price,
				'CHANGE'=>$change
			);
			array_push($mosts["MOSTS"],$most);
			
			$sql = "INSERT INTO `most_active` (`name`, `price`, `changeval`) VALUES ('$name', '$price', '$change');";
			$con->query($sql);
		}

		$mosts["success"] = 1;
		echo json_encode($mosts);
	}
	catch(Exception $e) {
		
		$sql = "SELECT `name` AS 'NAME',`price` AS 'PRICE',`changeval` AS 'CHANGE' FROM `most_active`";
		$rows = $con->query($sql); 
		$mosts["MOSTS"] = array();
		if($rows->num_rows > 0)
		{
			while($row = $rows->fetch_assoc())
			{
				$div = array();
				$div = $row;
				array_push($mosts["MOSTS"],$div);
			}
			$mosts["s