<?php
function getPageNameURL() 
{
  $pageURL = 'http';
	 if (isset($_SERVER["HTTPS"]) and $_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
	 	$pageURL .= "://";
	 if ($_SERVER["SERVER_PORT"] != "80") {
	  	$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	 }else{
	  	$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	 }
 return basename($pageURL);
}
							
function pagingView($query2,$start,$numberResult="5",$numAll=0)
{
	$pageCount2 =0;
	$row = 0;
	if($query2 ==0 && $numAll!=0)
	{
		$pageCount2 = $numAll;
	}
	global $conn;
	global $align2;
	
	$listURL = getPageNameURL();
	if($pageCount2 !=0 )
	{
		$pageCount = (int)ceil($pageCount2/$numberResult);
		$row = $pageCount2;
	}
	
	if($query2 != '')
	{
		$result2 = mysqli_query($conn, $query2);
		$row2 = mysqli_fetch_array($result2);
		$row = $row2[0];
		$pageCount = (int)ceil($row2[0]/$numberResult);
	}
	
	if($row > $numberResult):
	?>
	<div class="dataTables_paginate paging_simple_numbers" id="example_paginate" style="float:<?php echo $align2;?>">
	<ul class="pagination">
	<?php 
			
	$currentPage =  1 + ($start/$numberResult);
	if($pageCount <= 9)
	{
				$lowerPage = 1;
				$upperPage = $pageCount;
				
			}
			else
			{
			    $lowerPage =  $currentPage - 5;
	    if($lowerPage <= 0 )
	    $lowerPage = 1;
	    else
	    {
		  $maxLowerPage = $lowerPage + 8;
		  if($maxLowerPage >= $pageCount)
		  $lowerPage = $pageCount - 8;
	    }
						     
	    $upperPage = $lowerPage + 8;
	    if($upperPage > $pageCount)
	    $upperPage = $pageCount;
	}
			
			if($lowerPage != 1){?>
            	<li class="paginate_button page-item previous"><a href="javascript:onclick=pageing('<?php echo $listURL;?>','0')" aria-controls="example" data-dt-idx="-4" tabindex="0" class="page-link">الاول</a></li>
		    <?php }?>
			
			<?php if(!empty($currentPage) && ($currentPage != 1)):?>
                <li class="paginate_button page-item previous" ><a href="javascript:onclick=pageing('<?php echo $listURL;?>','<?php echo ($currentPage-2)*$numberResult;?>')" aria-controls="example" data-dt-idx="-3" tabindex="0" class="page-link">السابق</a></li>
		    <?php endif;?>
						
			<?php while($lowerPage <= $upperPage):?>
				<?php if($currentPage != $lowerPage):?>
                 	<li class="paginate_button page-item "><a href="javascript:onclick=pageing('<?php echo $listURL;?>','<?php echo ($lowerPage-1)*$numberResult;?>')" aria-controls="example" data-dt-idx="<?php echo $lowerPage;?>" tabindex="0" class="page-link"><?php echo $lowerPage;?></a></li>
				<?php else:?>
               		 <li class="paginate_button page-item active"><a href="#" aria-controls="example" data-dt-idx="<?php echo $lowerPage;?>" tabindex="0" class="page-link"><?php echo $lowerPage;?></a></li>
				<?php endif;?>
			 <?php $lowerPage++;?>
		    <?php endwhile;?>
		    
			<?php if( !empty($currentPage) && ($currentPage != $pageCount)):?>
            	<li class="paginate_button page-item next" id="example_next"><a href="javascript:onclick=pageing('<?php echo $listURL;?>','<?php echo ($currentPage)*$numberResult;?>')" aria-controls="example" data-dt-idx="-1" tabindex="0" class="page-link">التالي</a></li>
		    <?php endif;?>
		    
			<?php if($upperPage != $pageCount):?>
            	<li class="paginate_button page-item next"><a href="javascript:onclick=pageing('<?php echo $listURL;?>','<?php echo ($pageCount-1)*$numberResult;?>')" aria-controls="example" data-dt-idx="-2" tabindex="0" class="page-link">الاخير</a></li>
		    <?php endif;?>                                        
		</ul>
	
	</div>
	<?php else:?>&nbsp;                            
	<?php endif;?>
     
<?php  }?>