<?php 
function draw_navigation($type,$category,$start)
{
	
	include ("db_settings.php"); 
	
	$handler	=	mysql_connect($db_server,$db_username,$db_password)
		or die("Could not connect database: " . mysql_error());
	mysql_select_db($db_database,$handler);
	
	$n_tamplets;
	$html_code;
	
	if(!strcmp($type,"budget"))
	{
		$result=mysql_query("SELECT id FROM templates WHERE status ='budget'",$handler);
		$n_templates=mysql_affected_rows($handler);
	}
	elseif(!strcmp($category,"all"))
	{
		$result=mysql_query("SELECT id FROM templates WHERE type ='$type' AND status <> 'budget'",$handler);
		$n_templates=mysql_affected_rows($handler);
	}
	elseif(!strcmp($type,"all"))
	{
		$result=mysql_query("SELECT id FROM templates WHERE category LIKE '%$category%' AND status <> 'budget'",$handler);
		$n_templates=mysql_affected_rows($handler);
	}
	
	if($n_templates<24)
	{
		return;
	}
	else
	{
		$current_page=($start/24)+1;
		$html_code.="Page: $current_page<br>";
	}
	
	$n_pages=ceil($n_templates/24);

	$file_name="";
	$fn=$_SERVER['PHP_SELF'];
	
	$fn_position=strrpos($fn,"/")+1;
	
	for ( ; $fn_position < strlen($fn) ; $fn_position++)
	{
		$file_name.=$fn[$fn_position];
	}
	
	for($x=0 ; $x < $n_pages ; $x++ )
	{
		$p=$x+1;
		$s=$x*24;
		$html_code.= "<a href='$file_name?start=$s&type=$type&category=$category'>$p</a> ";
	}
	
	mysql_close($handler);
	
	print "<p align='center'>$html_code</p>";
}

function draw_template_table($type,$category,$start)
{
	draw_navigation($type,$category,$start);
	
	include ("db_settings.php"); 
	$handler	=	mysql_connect($db_server,$db_username,$db_password)
		or die("Could not connect database: " . mysql_error());
	mysql_select_db($db_database,$handler);
	
	print "<table border='0' cellspacing='0' cellpadding='3'><tr>";

	if(!strcmp($type,"budget"))
	{
		$result=mysql_query("SELECT * FROM templates WHERE status = 'budget' ORDER BY id DESC LIMIT $start,24",$handler);
	}
	elseif(!strcmp($category,"all"))
	{
		$result=mysql_query("SELECT * FROM templates WHERE type ='$type'  AND status <> 'budget' ORDER BY id DESC LIMIT $start,24",$handler);
	}
	elseif(!strcmp($type,"all"))
	{
		$result=mysql_query("SELECT * FROM templates WHERE category LIKE '%$category%' AND status <> 'budget' ORDER BY id DESC LIMIT $start,24",$handler);
	}
	$pos=0;
	while($row=mysql_fetch_array($result))
	{
		$id=$row["id"];
		$name=$row["name"];
		$price=$row["price"];
		$thumb=$row["thumb"];
		$preview="http://www.flash-template.com/preview.php?tid=$id";
		if($pos%4==0 && $pos!=0)
		{
			print "</tr><tr>";
		}
		print "<td>";
		include ("skin.php");
		print "</td>";
		;

		$pos++;
	}
	print "</tr></table>";
	mysql_close($handler);

	print $html_code;
}
?>