mysql_connect('localhost', 'root', 'admin'); 
mysql_query("SET NAMES 'utf8'");
mysql_select_db('WineList'); 
$sql="SELECT DISTINCT Category FROM Wines WHERE QtyInStock > 0 AND WineType = 'Red' ORDER BY Category";
 
$records = mysql_query($sql); 

$linecount = 1;
$reds = array();
$whites = array();
$redCount=0;
$whiteCount=0;

while($wine=mysql_fetch_assoc($records)){
     if(!empty($wine['Category'])){
          $redCount = $redCount + 1;
          $reds[$redCount] = $wine['Category'];
        
    }
}

$sql="SELECT DISTINCT Category2 FROM Wines WHERE QtyInStock > 0 AND WineType = 'Red' ORDER BY Category2";
 
$records = mysql_query($sql); 
while($wine=mysql_fetch_assoc($records)){
     if(!empty($wine['Category2'])){
          $wineFound = 0;
           for ($x = 1; $x <= $redCount; $x++) {
                 if($reds[$x] == $wine['Category2']){
                  
                        $wineFound = 1;
                  }
           }
           if($wineFound == 0){
              $redCount = $redCount + 1;
              $reds[$redCount] = $wine['Category2'];
          }
    }
}

sort($reds);






$sql="SELECT DISTINCT Category FROM Wines WHERE QtyInStock > 0 AND WineType = 'White' ORDER BY Category";
 
$records = mysql_query($sql); 

while($wine=mysql_fetch_assoc($records)){
    if(!empty($wine['Category'])){
          $whiteCount = $whiteCount + 1;
          $whites[$whiteCount] = $wine['Category'];
         //  echo $whites[$whiteCount];
      }
}

$sql="SELECT DISTINCT Category2 FROM Wines WHERE QtyInStock > 0 AND WineType = 'White' ORDER BY Category2";
 
$records = mysql_query($sql); 
while($wine=mysql_fetch_assoc($records)){
     if(!empty($wine['Category2'])){
          $wineFound = 0;
           for ($x = 1; $x <= $whiteCount; $x++) {
                 if($whites[$x] == $wine['Category2']){
                  
                        $wineFound = 1;
                  }
           }
           if($wineFound == 0){
              $whiteCount = $whiteCount + 1;
              $whites[$whiteCount] = $wine['Category2'];
          }
         //  echo $whites[$whiteCount];
    }
}

sort($whites);




if($whiteCount > $redCount){
   $listItems = $whiteCount;
}
else{
   $listItems = $redCount;
}

echo "<body><table style='margin-left: auto; margin-right: auto;'>";
        echo "<col width='45%'>";
        echo "<col width='5%'>";
  	echo "<col width='45%'>";
echo "<tr  class='Varietals'><td class='Varietals'><b><font color='Black' size='+2'><a class='byProfile' href='http://192.168.104.254/wordpress/byprofile/?Listing=WhitePrice'>Whites</a></font></b></td><td></td><td class='Varietals'><b><font color='Black' size='+2'><a class='byProfile'  href='http://192.168.104.254/wordpress/byprofile/?Listing=RedPrice'>Reds</a></font></b></td></tr>";

      for($x = 0; $x <= $listItems-1; $x++){
             echo "<tr>";
             if($x <= $redCount){
            
                   $redstr = "<td class='Varietals'><font size='+2'><a href='http://192.168.104.254/wordpress/byVarietal/?Listing=".$reds[$x]."'>".$reds[$x]."</a></font></td>";
             }
             else{
              $redstr = "<td></td>";
             }
             
             if($x <= $whiteCount){
            
                   $whitestr = "<td class='Varietals'><font size='+2'><a href='http://192.168.104.254/wordpress/byVarietal/?Listing=".$whites[$x]."'>".$whites[$x]."</a></font></td>";
             }
             else{
              $whitestr = "<td></td>";
             }
            
             echo $whitestr;
             echo "<td></td>";
             echo $redstr;
             echo "<tr>";
             

      }

echo "</table>";