<?php require('includes/header.php'); ?>
<?php

$id = clean($_GET['id']);
$page = clean($_GET['page']);
$sub = clean($_GET['sub']);

switch($id){
case "6":
$title = "Browser Games";
break;
case "8":
$title = "Flash Games";
break;
case "49":
$title = "2D Games";
break;
case "50":
$title = "3D  Games";
break;
}

if($title){

$page = (int)$_GET['page'];
if($page < 1){
$page = 1;
}
$a = ($page - 1) * 15;




?>
<table align="center" width="100%" valign="top"><tr><td width="300" valign="top"><?php include('includes/rectangle.php'); ?></td><td valign="top">
<table border="0" align="center" width="85%" valign="top" cellpadding="4" cellspacing="0">
<tr><td align="center" colspan="3">Online Games: <?php

switch($id){
case "6":
echo '<a href="games.php?id=49" class="title">2D Games</a> | <a href="games.php?id=50" class="title">3D Games</a> | <span class="wtitle">Browser Games</span> | <a href="games.php?id=8" class="title">Flash Games</a>';
break;
case "8":
echo '<a href="games.php?id=49" class="title">2D Games</a> | <a href="games.php?id=50" class="title">3D Games</a> | <a href="games.php?id=6" class="title">Browser Games</a> | <span class="wtitle">Flash Games</span>';
break;
case "49":
echo '<span class="wtitle">2D Games</span> | <a href="games.php?id=50" class="title">3D Games</a> | <a href="games.php?id=6" class="title">Browser Games</a> | <a href="games.php?id=8" class="title">Flash Games</a>';
break;
case "50":
echo '<a href="games.php?id=49" class="title">2D Games</a> | <span class="wtitle">3D Games</span> | <a href="games.php?id=6" class="title">Browser Games</a> | <a href="games.php?id=8" class="title">Flash Games</a>';
break;
}

?></td></tr>
<?php

$sql_sub = mysql_query("SELECT * FROM `categories` WHERE `parent_num`='$id'");
$snum = mysql_num_rows($sql_sub);
$si = 0;
if($snum > 0){
echo '<tr><td align="center" colspan="3">';
while($si < $snum){

$sub_cat = mysql_result($sql_sub,$si,"category");
$sub_cat_id = mysql_result($sql_sub,$si,"category_id");

if($sub == $sub_cat_id){
echo $sub_cat;
$ssub_cat = $sub_cat;
}
else{
echo '<a href="games.php?id='.$id.'&sub='.$sub_cat_id.'">'.$sub_cat.'</a>';
}

$gi = $si + 1;
if($gi < $snum){
echo ' | ';
}

$si++;
}

echo '</td></tr>';

}

if($sub){
	$sql_count = mysql_query("SELECT count(`site_id`) from `sites` natural join `site_category` where `category_id`='$sub'");
	$sql = mysql_query("SELECT title,ogg_votes,total_hits_out,`sites`.`site_id`, `ogg_votes` as `ogg_rank` FROM `site_category` natural join sites left join `ogg_sites` on `ogg_sites`.`site_id`=`sites`.`site_id` WHERE `category_id`='$sub' order by `ogg_rank` DESC, `total_hits_out` ASC limit $a,15");
}
else{
$sql_count = mysql_query("SELECT count(`site_id`) from `sites` natural join `site_category` where `category_id`='$id'");
$sql = mysql_query("SELECT title,ogg_votes,total_hits_out,`sites`.`site_id`, `ogg_votes` as `ogg_rank` FROM `site_category` natural join sites left join `ogg_sites` on `ogg_sites`.`site_id`=`sites`.`site_id` WHERE `category_id`='$id' order by `ogg_rank` DESC, `total_hits_out` ASC limit $a,15");
}


$count = mysql_fetch_array($sql_count);
$total = $count[0];
$pages = ceil($total / 15);
$id = clean($id);
?>
<tr><td align="left" colspan="3"><hr /></td></tr>
<tr><td align="left" colspan="3">Now listing <strong<i><?php echo $title; ?><?php

if($sub){
echo '->'.$ssub_cat;
}

?></i></strong> ordered by MOST votes (1) & by least hits out (2)</td></tr>
<tr><td align="left" colspan="3"><hr /></td></tr>
<tr><td align="left" class="hdline"><?php echo $title; ?></td><td class="hdline" align="center">All time Votes In</td><td class="hdline" align="center">All time Hits Out</td></tr>
<tr><td align="left" colspan="3"><hr /></td></tr>
<?php

	$num = mysql_num_rows($sql);
	$i = 0;
	$k = $a + 1;
	if($num > 0){
	while($i < $num){
	
	$j = $i % 2;
	if($j == 1){
	$trcolor = "#0044aa";
	}
	else{
	$trcolor = "#003399";
	}
	
	$site_id = mysql_result($sql,$i,"site_id");
	$stitle = mysql_result($sql,$i,"title");
	echo '<tr bgcolor="'.$trcolor.'"><td align="left"><a href="game.php?id='.$site_id.'" class="sts">'.$k.'.'.mysql_result($sql,$i,"title").'</a></td><td align="center">'.mysql_result($sql,$i,"ogg_votes").'</td><td align="center">'.mysql_result($sql,$i,"total_hits_out").'</td></tr>';
		
	$k++;
	$i++;
	}
	
?>
<tr><td align="left" colspan="3"><hr /></td></tr>
<tr><td align="left">Pages: <?php 

$j = 1;
while($j <= $pages){

if($page == $j){
echo $j;
}
else{
	if($sub){
		echo '<a href="games.php?id='.$id.'&sub='.$sub.'&page='.$j.'">'.$j.'</a>';
	}
	else{
		echo '<a href="games.php?id='.$id.'&page='.$j.'">'.$j.'</a>';
	}
}

$l = $j + 1;
if($l <= $pages){
echo ' | ';
}

$j++;
}

?></td><td align="right"><?php 

if($page > 1){
$j = $page - 1;
if($sub){
	echo '<a href="games.php?id='.$id.'&sub='.$sub.'&page='.$j.'">-Previous Page-</a>';
}
else{
	echo '<a href="games.php?id='.$id.'&page='.$j.'">-Previous Page-</a>';
}
$pre = 'yes';
}
if($page != $pages){
if($pre == "yes"){
echo ' | ';
}
$j = $page + 1;
if($sub){
	echo '<a href="games.php?id='.$id.'&sub='.$sub.'&page='.$j.'">-Next Page-</a>';
}
else{
	echo '<a href="games.php?id='.$id.'&page='.$j.'">-Next Page-</a>';
}
}

?></td></tr>
<?php
	}
	else{
		echo '<tr><td align="left" colspan="3">There are no games in this category.</td></tr>';
	}
	?>
</table>
</td>
</tr>
</table>
<?php
}
else{
echo '<meta http-equiv="refresh" content="0;url=/">';
}
?>
<?php require('includes/footer.php'); ?>
