<?
include "functions.php";


$limit=25;

	if($_POST['page']){
		$page=$_POST['page'];
	}else{
		$page=1;
	}
	$pages=$page-1;	
	
	$res		=	mysql_query("SELECT * FROM `invoices` group by client_name,client_email");
	$count		=	mysql_num_rows($res);
	$pagination	=	smart_pagination($_POST['page'],$count,$limit,'da')
	
?>
			<table width='100%' cellspacing='0' cellpadding='0'>
				<tr style='background:#003b8c; color:#fff;'>
					<td class='tdhead' width='20'>ID</td>
					<td class='tdhead' width='200'>Name</td>
					<td class='tdhead'>Email</td>
					<td class='tdhead' width='30'>Phone</td>
					<td class='tdhead' width='130'>Location</td>
					<td class='tdhead' width='60'>Invoices</td>
				</tr>
				<?
					$i=1;
		$res=mysql_query("SELECT * FROM `invoices` group by client_name,client_email limit  " . ($pages*$limit) . "," . $limit)or die(mysql_error());
		$cnt=mysql_num_rows($res);
		$i=1;
			while($row=mysql_fetch_array($res)){
			$row['client_address']=preg_replace("/\r/", '',preg_replace("/\n/", '\n', $row['client_address']));
			$res2=mysql_query("select * from `invoices` where client_name='".mysql_real_escape_string($row['client_name'])."' and  client_email='".mysql_real_escape_string($row['client_email'])."'")or die(mysql_error());
						?>
					<tr style='height:22px; cursor:pointer;' onclick="top.location='client.php?client_name=<?=urlencode($row['client_name'])?>&client_email=<?=urlencode($row['client_email'])?>'" onmouseover="$(this).css('background','#efffd0')" onmouseout="$(this).css('background','#fff')">
						<td class='tdhead2'><?=$i?></td>
						<td class='tdhead2'><?=strip_tags($row['client_name'])?></td>
						<td class='tdhead2'><?=$row['client_email']?></td>
						<td class='tdhead2'><?=$row['client_phone']?></td>
						<td class='tdhead2'><?=$row['client_location']?></td>
						<td class='tdhead2' align='right' style='border-right:1px solid #e6e6e6;'><?=mysql_num_rows($res2)?></td>
					</tr>	
						<?$i++;}
						if(mysql_num_rows($res)==0){
							echo "<tr><td class='tdhead2' style='border-right:1px solid #e6e6e6; text-align:center;' colspan='8'>No Clients Found</td></tr>";
						}
						?>
			</table>
			<div class='pagination'><?=$pagination?></div>