<?php
  include "include.php";
  session_start();
?>


  <script type="text/javascript">  

  $(function() {
    $( "input[type=submit], a, button" )
      //.button()
      .click(function( ev ) {
        /*event.preventDefault();*/
		$('a').css({ 'font-weight': '', 'text-decoration': 'underline' });
		$(ev.target).css({ 'font-weight': 'bold', 'text-decoration':'none' });
		var url = $(ev.target).attr('xref');
		$("#populate-phones").load(url);
      });
  });
 
$(document).ready(function(){
    $("div.tool-tip").hide();
    
    $("ul#sortable .ui-state-default").mouseover(function(){
        $(".tool-tip",this).show();
    }).mouseout(function(){
        $(".tool-tip",this).hide();
    });
});
</script>
  </head>

  <body>
  <?php
  session_start();
  //create array with values between commas
  $SelectedNeeds = explode(",", $_GET["array"]);
  
	$FeaturesArray = array();
	$SuitablePhonesArray = array();
	$arrays = stripslashes($_GET["arrays"]);
	
	//Get the length of the array
	$length = count($SelectedNeeds);
//	if (!$_GET["array"]) $length = 0;
	
/* 2. FIND THE RELATED FEATURES */	
$con = get_database_connection();
  
 $FeaturesArray = array();
 
// loop through the SelectedNeeds  
for($x=0;$x<$length;$x++)
{
	
	$arrayValue = $SelectedNeeds[$x];

	//find the related FeatureID for each selected Requirement
	$result = mysqli_query($con,"SELECT FeatureID FROM FeatureRequirement WHERE RequirementID ='".$arrayValue."'");

	//place it in the Features Array
	while($row = mysqli_fetch_array($result)){
		$ID = $row['FeatureID'];
		array_push($FeaturesArray,$ID);
	}

}

	 	
	foreach($FeaturesArray as $y){
		$searchQuery = "SELECT DISTINCT PhoneID FROM PhoneFeature WHERE FeatureID ='".$y."' AND Value = '1'";
		//Get the Feature Type

		$result2 = mysqli_query($con,$searchQuery);
	while($row = mysqli_fetch_array($result2))
			$SuitablePhonesArray[] = $row['PhoneID'];
			
		//insert the suitable phones into the SuitablePhonesArray
	
	}
	
		if (!$length) {
			$res = mysqli_query($con, "SELECT PhoneID From Phone");
			while($row = mysqli_fetch_array($res))
				$SuitablePhonesArray[] = $row['PhoneID'];
		}
	
		
		//find the length of the FeaturesArray (aka the number of related features)
		$noOfFeatures = count($FeaturesArray);
		
		if (!$noOfFeatures) $noOfFeatures = 1;
		
		//find the count of the PhoneIDs in the SuitablePhonesArray
		$PhoneIDOccurence = array_count_values($SuitablePhonesArray);
	
		//If the length is the same as the count we have a MATCH
		$Phones = array_keys($PhoneIDOccurence, $noOfFeatures);
		
		

	
$_SESSION['Phones'] = $Phones;



	foreach($Phones as $array_value){
		$query1String = "PhoneID = '".$array_value."' OR ";
		$string1 = $string1." ".$query1String;
	}
	$phoneCountID = 0;
	//PHONES			
	
	$result = mysqli_query($con,"SELECT COUNT(PhoneID) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string1."PhoneID = '0' GROUP BY `Brand`");
	
	while($row = mysqli_fetch_array($result)){
		$phoneCountID++;
    $count = $row['PhoneCount'];
	$total = $total + $count;
	}
	
			echo "<div class='phone-panel'>
<div class='phone-listing'>
        <h3>Phone Recommendations</h3>
        <div class='step1-box'>
        <!--ul-->";
	
	
	//Display the Brand Names, followed by the count of the models in brackets
	$result = mysqli_query($con,"SELECT COUNT(*) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string1."PhoneID = '0' GROUP BY `Brand`");
	while($row = mysqli_fetch_array($result)){
			$count = $row['PhoneCount'];
			$brand = $row['Brand'];
			echo "<a href='#' xref='phonescroller.php?Brand=".$brand."&Priority=!&arrays=".$arrays."'>".$brand." (".$count.")</a><br/>";
		
	}

echo"</div></div>";	  
		  

	echo "<div id='populate-phones'>";	

echo "</div></div>";
	?>
