<?php

/**
 * @link: http://www.Awcore.com/dev
 */
 
   function pagination($query, $per_hal = 10,$hal = 1, $url = '?'){        
    	$query = "SELECT COUNT(*) as `num` FROM {$query}";
    	$row = mysql_fetch_array(mysql_query($query));
    	$total = $row['num'];
        $adjacents = "2"; 

    	$hal = ($hal == 0 ? 1 : $hal);  
    	$start = ($hal - 1) * $per_hal;								
		
    	$prev = $hal - 1;							
    	$next = $hal + 1;
        $lasthal = ceil($total/$per_hal);
    	$lpm1 = $lasthal - 1;
    	
    	$pagination = "";
    	if($lasthal > 1)
    	{	
    		$pagination .= "<ul class='pagination'>";
                    $pagination .= "<li class='details'>Hal $hal dari $lasthal Total : $total data </li>";
    		if ($lasthal < 7 + ($adjacents * 2))
    		{	
    			for ($counter = 1; $counter <= $lasthal; $counter++)
    			{
    				if ($counter == $hal)
    					$pagination.= "<li><a class='current'>$counter</a></li>";
    				else
    					$pagination.= "<li><a href='{$url}hal=$counter'>$counter</a></li>";					
    			}
    		}
    		elseif($lasthal > 5 + ($adjacents * 2))
    		{
    			if($hal < 1 + ($adjacents * 2))		
    			{
    				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
    				{
    					if ($counter == $hal)
    						$pagination.= "<li><a class='current'>$counter</a></li>";
    					else
    						$pagination.= "<li><a href='{$url}hal=$counter'>$counter</a></li>";					
    				}
    				$pagination.= "<li class='dot'>...</li>";
    				$pagination.= "<li><a href='{$url}hal=$lpm1'>$lpm1</a></li>";
    				$pagination.= "<li><a href='{$url}hal=$lasthal'>$lasthal</a></li>";		
    			}
    			elseif($lasthal - ($adjacents * 2) > $hal && $hal > ($adjacents * 2))
    			{
    				$pagination.= "<li><a href='{$url}hal=1'>1</a></li>";
    				$pagination.= "<li><a href='{$url}hal=2'>2</a></li>";
    				$pagination.= "<li class='dot'>...</li>";
    				for ($counter = $hal - $adjacents; $counter <= $hal + $adjacents; $counter++)
    				{
    					if ($counter == $hal)
    						$pagination.= "<li><a class='current'>$counter</a></li>";
    					else
    						$pagination.= "<li><a href='{$url}hal=$counter'>$counter</a></li>";					
    				}
    				$pagination.= "<li class='dot'>..</li>";
    				$pagination.= "<li><a href='{$url}hal=$lpm1'>$lpm1</a></li>";
    				$pagination.= "<li><a href='{$url}hal=$lasthal'>$lasthal</a></li>";		
    			}
    			else
    			{
    				$pagination.= "<li><a href='{$url}hal=1'>1</a></li>";
    				$pagination.= "<li><a href='{$url}hal=2'>2</a></li>";
    				$pagination.= "<li class='dot'>..</li>";
    				for ($counter = $lasthal - (2 + ($adjacents * 2)); $counter <= $lasthal; $counter++)
    				{
    					if ($counter == $hal)
    						$pagination.= "<li><a class='current'>$counter</a></li>";
    					else
    						$pagination.= "<li><a href='{$url}hal=$counter'>$counter</a></li>";					
    				}
    			}
    		}
    		
    		if ($hal < $counter - 1){ 
    			$pagination.= "<li><a href='{$url}hal=$next'>Next</a></li>";
                $pagination.= "<li><a href='{$url}hal=$lasthal'>Last</a></li>";
    		}else{
    			$pagination.= "<li><a class='current'>Next</a></li>";
                $pagination.= "<li><a class='current'>Last</a></li>";
            }
    		$pagination.= "</ul>\n";		
    	}
    
    
        return $pagination;
    } 
?>