<?php 
$gettweet = search_tweets('forex', 20);

$decodedItem = json_decode($gettweet, true);

$getTweets = $decodedItem['tweets'];

$total_tweet_array = array();

foreach ($getTweets as $tw_eets) {
	# code...
	$url = $tw_eets['url'];
	$text = $tw_eets['text'];
	$user = $tw_eets['user'];
	$fullurl = $url.$user;
	$getLang = substr($text, 7, 2);
	$text = substr($text, 36);
	$imgUser = $tw_eets['img'];
	$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
	$username = substr($user, 1);
	$date = $tw_eets['date'];

	$getDate = date('d M y H:i',$date);

	if ($getLang =='en') {
		# code...

		$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate);

		array_push($total_tweet_array, $makeArray);





	}

}


$secondTweet = search_tweets('NZDUSD', 20);

$secondDecodedTweet = json_decode($secondTweet, true);

$getSecondTweet = $secondDecodedTweet['tweets'];


foreach ($getSecondTweet as $tw_eets) {
	# code...
	$url = $tw_eets['url'];
	$text = $tw_eets['text'];
	$user = $tw_eets['user'];
	$fullurl = $url.$user;
	$getLang = substr($text, 7, 2);
	$text = substr($text, 36);
	$imgUser = $tw_eets['img'];
	$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
	$username = substr($user, 1);
	$date = $tw_eets['date'];

	$getDate = date('d M y H:i',$date);

	if ($getLang =='en') {
		# code...

		$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate);

		array_push($total_tweet_array, $makeArray);


	}

}



$thirdTweet = search_tweets('#nzdusd', 20);

$thirdDecodedTweet = json_decode($thirdTweet, true);

$getThirdTweet = $thirdDecodedTweet['tweets'];

foreach ($getThirdTweet as $tw_eets) {
	# code...
	$url = $tw_eets['url'];
	$text = $tw_eets['text'];
	$user = $tw_eets['user'];
	$fullurl = $url.$user;
	$getLang = substr($text, 7, 2);
	$text = substr($text, 36);
	$imgUser = $tw_eets['img'];
	$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
	$username = substr($user, 1);
	$date = $tw_eets['date'];

	$getDate = date('d M y H:i',$date);

	if ($getLang =='en') {
		# code...

		$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate);

		array_push($total_tweet_array, $makeArray);


	}

}





$fourthTweet = search_tweets('nzd', 20);

$fourthDecodedTweet = json_decode($fourthTweet, true);

$getFourthTweet = $fourthDecodedTweet['tweets'];

foreach ($getFourthTweet as $tw_eets) {
	# code...
	$url = $tw_eets['url'];
	$text = $tw_eets['text'];
	$user = $tw_eets['user'];
	$fullurl = $url.$user;
	$getLang = substr($text, 7, 2);
	$text = substr($text, 36);
	$imgUser = $tw_eets['img'];
	$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
	$username = substr($user, 1);
	$date = $tw_eets['date'];

	$getDate = date('d M y H:i',$date);

	if ($getLang =='en') {
		# code...

		$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate);

		array_push($total_tweet_array, $makeArray);
	}

}



?>






<style type="text/css">
	.search-box-twitter
	{
		background-color: #fff !important;
	}
	.search-box-twitter > div > div >h3
	{
		color: #202943 !important;
	}
	.search-box-twitter > div > div >form > div > div > select, .search-box-twitter > div > div >form > div > div > input
	{
		color: #202943 !important;
		background-color: #eee !important;
	}
	.tweettext
	{
		color: #fff;
	}
	.tweettextlight
	{
		color: #202943 !important;
	}
</style>



<div class="row">

	<div class="col-md-12">
		
		<div class="crypt-gross-market-cap mt-4" id="searchBox">
                  <div class="row">
                     <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                        <h3 style="text-align: left;">Search</h3>
                        <div class="alert alert-danger" style="margin-top: 10px;display: none;" id="error">
                        	
                        </div>
                        <form method="post" action="" id="searchForm">
                        <div class="row">
                        	<div class="col-md-4">
                        	<select placeholder="Identifier" class="form form-control newformcontrol" name="choice" id="choice">
                               <option value="0">Select Your Choice</option>
                               <option value="1">Search By Username</option>
                               <option value="2">Search By Tweet</option>
                           </select>
                        	</div>
                        	<div class="col-md-4">
                        		<input type="text" class="form form-control newformcontrol" placeholder="Enter Keyword" name="kw" id="kw">	
                        	</div>
                        	<div class="col-md-4">
                        		<div><button type="button" name="submit" onclick="searchFunc()" class="crypt-button-green-full" style="border: none;">Search</button></div>
                        	</div>
                        </div>
                         </form>  
                           
                     </div>
                  </div>
               </div>

	</div>
	
</div>













<div class="row">

	<div class="col-md-12">
		
		<div class="crypt-gross-market-cap mt-4" id="sortBox">
                  <div class="row">
                     <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                        <h3 style="text-align: left;">Sort Result</h3>
                        <div class="alert alert-danger" style="margin-top: 10px;display: none;" id="error2">
                        	
                        </div>
                        <form method="post" action="" id="sortForm">
                        <div class="row">
                        	<div class="col-md-8">
                        	<select placeholder="Identifier" class="form form-control newformcontrol" name="sortChoice" id="sortChoice">
                               <option value="0">Select Your Choice</option>
                               <option value="1">Sort By Username ( Alphabetically ( A - Z ) </option>
                               <option value="2">Sort By Username ( Alphabetically ( Z - A ) </option>
                               <option value="3">Sort By Tweet ( Alphabetically ( A - Z ) </option>
                               <option value="4">Sort By Tweet ( Alphabetically ( Z - A ) </option>
                               <option value="5">Sort By Date ( Descending Order ) </option>
                               <option value="6">Sort By Date ( Asscending Order ) </option>
                           </select>
                        	</div>
                        	<div class="col-md-4">
                        		<div><button type="button" name="submitSort" style="border: none;" onclick="sortFunc()" class="crypt-button-green-full">Sort Result</button></div>
                        	</div>
                        </div>
                         </form>  
                           
                     </div>
                  </div>
               </div>

	</div>
	
</div>





<?php 


$sortingArray = array();



if (isset($_POST['sortChoice'])) {
	# code...

	$sortChoice = $_POST['sortChoice'];


			$firstSort = search_tweets('NZDUSD', 20);

			$firstDecodedSort = json_decode($firstSort, true);

			$getFirstSortTweet = $firstDecodedSort['tweets'];

			$count = 0;
			foreach ($getSecondTweet as $tw_eets) {
				# code...
				$url = $tw_eets['url'];
				$text = $tw_eets['text'];
				$user = $tw_eets['user'];
				$fullurl = $url.$user;
				$getLang = substr($text, 7, 2);
				$text = substr($text, 36);
				$imgUser = $tw_eets['img'];
				$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
				$username = substr($user, 1);
				$date = $tw_eets['date'];

				$getDate = date('d M y H:i',$date);

				$sortingDate = date('y-m-d H:i:s', $date);

				$count++;

				if ($getLang =='en') {
					# code...

					$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate, 'sortDate'=>$sortingDate);

					array_push($total_tweet_array, $makeArray);

				}

			}





			$secondSort = search_tweets('forex', 20);

			$secondDecodedSort = json_decode($secondSort, true);

			$getSecondSort = $secondDecodedSort['tweets'];

			foreach ($getSecondSort as $tw_eets) {
				# code...
				$url = $tw_eets['url'];
				$text = $tw_eets['text'];
				$user = $tw_eets['user'];
				$fullurl = $url.$user;
				$getLang = substr($text, 7, 2);
				$text = substr($text, 36);
				$imgUser = $tw_eets['img'];
				$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
				$username = substr($user, 1);
				$date = $tw_eets['date'];

				$getDate = date('d M y H:i',$date);

				$sortingDate = date('y-m-d H:i:s', $date);

				$count++;

				if ($getLang =='en') {
					# code...

					$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate, 'sortDate'=>$sortingDate);

					array_push($total_tweet_array, $makeArray);
				

				}

			}






			$thirdSort = search_tweets('#nzdusd', 20);

			$thirdDecodedSort = json_decode($thirdSort, true);

			$getThirdSort = $thirdDecodedSort['tweets'];

			foreach ($getThirdSort as $tw_eets) {
				# code...
				$url = $tw_eets['url'];
				$text = $tw_eets['text'];
				$user = $tw_eets['user'];
				$fullurl = $url.$user;
				$getLang = substr($text, 7, 2);
				$text = substr($text, 36);
				$imgUser = $tw_eets['img'];
				$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
				$username = substr($user, 1);
				$date = $tw_eets['date'];

				$getDate = date('d M y H:i',$date);

				$sortingDate = date('y-m-d H:i:s', $date);

				$count++;

				if ($getLang =='en') {
					# code...

					$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate, 'sortDate'=>$sortingDate);

					array_push($sortingArray, $makeArray);

				}

			}











			$fourthSort = search_tweets('nzd', 20);

			$fourthDecodedSort = json_decode($fourthSort, true);

			$getfourthSort = $fourthDecodedSort['tweets'];

			foreach ($getfourthSort as $tw_eets) {
				# code...
				$url = $tw_eets['url'];
				$text = $tw_eets['text'];
				$user = $tw_eets['user'];
				$fullurl = $url.$user;
				$getLang = substr($text, 7, 2);
				$text = substr($text, 36);
				$imgUser = $tw_eets['img'];
				$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
				$username = substr($user, 1);
				$date = $tw_eets['date'];

				$getDate = date('d M y H:i',$date);

				$sortingDate = date('y-m-d H:i:s', $date);

				$count++;

				if ($getLang =='en') {
					# code...

					$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate, 'sortDate'=>$sortingDate);

					array_push($sortingArray, $makeArray);

				}

			}



	if ($sortChoice == '1') {
		# code...
		usort($sortingArray, 'sortByUsername');
	}else if ($sortChoice == '2') {
		# code...
		usort($sortingArray, 'sortByUsername2');
	}else if ($sortChoice == '3') {
		# code...
		usort($sortingArray, 'sortByTweet');
	}else if ($sortChoice == '4') {
		# code...
		usort($sortingArray, 'sortByTweet5');
	}else if ($sortChoice == '5') {
		# code...
		usort($sortingArray, 'sortByDate');
	}else if ($sortChoice == '6') {
		# code...
		usort($sortingArray, 'sortByDate2');
	}

	$total_tweet_array = $sortingArray;

}


function sortByDate2($element1, $element2)
{
	$datetime1 = strtotime($element1['sortDate']); 
    $datetime2 = strtotime($element2['sortDate']); 
    return $datetime2 - $datetime1;
}


function sortByDate($element1, $element2)
{
	$datetime1 = strtotime($element1['sortDate']); 
    $datetime2 = strtotime($element2['sortDate']); 
    return $datetime1 - $datetime2;
}


function sortByTweet($username1, $username2)
{

	return strcmp($username1['text'], $username2['text']);

}

function sortByTweet2($username2, $username1)
{

	return strcmp($username2['text'], $username1['text']);

}



function sortByUsername($username1, $username2)
{

	return strcmp($username1['username'], $username2['username']);

}



function sortByUsername2($username2, $username1)
{

	return strcmp($username1['username'], $username2['username']);

}














$searchArray = array();


if (isset($_POST['submit'])) {
	# code...

	$choice = $_POST['choice'];
	$kw = $_POST['kw'];

	if ($choice == '1') {
		# code...
		$getUserTweets = user_tweets($kw, 100);

		$getUserDecoded = json_decode($getUserTweets, true);

		$getUserTw = $getUserDecoded['tweets'];


		foreach ($getUserTw as $tw_eets) {
			# code...
			$url = $tw_eets['url'];
			$text = $tw_eets['text'];
			$user = $tw_eets['user'];
			$fullurl = $url.$user;
			$getLang = substr($text, 7, 2);
			$text = substr($text, 36);
			$imgUser = $tw_eets['img'];
			$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
			$username = substr($user, 1);
			$date = $tw_eets['date'];

			$getDate = date('d M y H:i',$date);

			if ($getLang =='en' && $username == $kw) {
				# code...

				$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate);

				array_push($searchArray, $makeArray);
			}

		}


	}else if ($choice == '2') {
		# code...

		$getUserTweets = search_tweets($kw, 100);

		$getUserDecoded = json_decode($getUserTweets, true);

		$getUserTw = $getUserDecoded['tweets'];

		foreach ($getUserTw as $tw_eets) {
			# code...
			$url = $tw_eets['url'];
			$text = $tw_eets['text'];
			$user = $tw_eets['user'];
			$fullurl = $url.$user;
			$getLang = substr($text, 7, 2);
			$text = substr($text, 36);
			$imgUser = $tw_eets['img'];
			$imgUser = str_replace('_bigger.jpg', '_400x400.jpg', $imgUser);
			$username = substr($user, 1);
			$date = $tw_eets['date'];

			$getDate = date('d M y H:i',$date);

			if ($getLang =='en') {
				# code...

				$makeArray = array('img'=>$imgUser, 'url'=>$fullurl, 'username'=>$username, 'text'=>$text, 'date'=>$getDate);

				array_push($searchArray, $makeArray);
			}

		}



	}
	
	
	$total_tweet_array = $searchArray;


}


?>



<div class="row">

	<div class="col-md-12">
		
		<div class="crypt-gross-market-cap mt-4" id="twitterBox">

			<h3 style="color: #F7614E;margin-bottom: 20px;">Twitter Feeds</h3>
<?php 


foreach ($total_tweet_array as $tweetsCheck) {
	# code...
	$tweetImageUrl = $tweetsCheck['img'];
	$tweetFullUrl = $tweetsCheck['url'];
	$tweetUsername = $tweetsCheck['username'];
	$tweetText = $tweetsCheck['text'];
	$tweetDate = $tweetsCheck['date'];
	?>
	<div class="row" style="margin-bottom: 10px;margin-top: 30px;">
		<div class="col-md-2">
			<img src="<?=$tweetImageUrl; ?>" class="img-thumbnail" style="width: 100%;">
		</div>
		<div class="col-md-8">
			<a href="<?=$tweetFullUrl ?>" class="tweeturl" target="_blank" style="font-size: 16px;color: #F7614E;"><?=$tweetUsername; ?></a>
			<p class="tweettext"><?=$tweetText; ?></p>
			<p style="color: #F7614E;margin-top: 20px;"><span style="font-weight: 900">Created at </span><?=$tweetDate; ?></p>
		</div>	
	</div>

	<?php 	
}


?>


</div>
</div>
</div>






<script type="text/javascript">
	
	function searchFunc()
	{
		var ch = $("#choice").val();
		var kw = $("#kw").val();

		if (kw == '') {

			$("#error").show();

			$("#error").html('You must enter keyword to search');

		}else if (ch == '0') {
			$("#error").show();
			$("#error").html('You must select choice');


		}else{

			$("#error").hide();
			$("#searchForm").submit();
		}
	}
	
</script>

<script type="text/javascript">
	function sortFunc()
	{
		var sort = $("#sortChoice").val();

		if (sort == '0') {

			$("#error2").show();
			$("#error2").html('Select Your Choice First');	
		}

		if (sort != '0') {

			$("#error2").hide();
			$("#sortForm").submit();
		}
	}
</script>


<!-- 	<div class="row" style="margin-bottom: 10px;">
		<div class="col-md-2">
			<img src="<?=$imgUser ?>" class="img-thumbnail" style="width: 100%;">
		</div>
		<div class="col-md-8">
			<a href="<?=$fullurl ?>" target="_blank" style="color: #F7614E;font-size: 16px;"><?=$username; ?></a>
			<p style="color: white;"><?=$text; ?></p>
			<p style="color: #F7614E;margin-top: 20px;"><span style="color: white;">Created at </span><?=$getDate; ?></p>
		</div>	
	</div> -->