<?php

	# mark core  v1.0
    # author Drk in
	# date 24.10.19 	

	# class page

	class page {

		CONST element = 10;

		public function __construct($cn){

		$elements = self :: element;	

		$page =  isset( $_GET['page'] ) ? abs(intval($_GET['page'])): 1;

		$total = ceil($cn / $elements); 

		if ($page < 1) $page = 1;
		
		if ($page > $total) $page = $total;

		$start = abs($page * $elements) - $elements;

		$this-> page = $page;

		$this-> total = $total;

		$this-> start = $start;

		$this-> cn = $cn;

		$this-> elements = $elements;

		$this-> limit = " LIMIT {$start}, {$elements}";

		}

		public function get($url){

		$cn = $this -> cn;	

		$elements = self :: element;

		if ($cn > $elements):		

		$page = $this -> page;

		$total = $this -> total;	

		echo '<div class ="ma"><div class="wrapper"><div class="pag wbg"><div class="pagn pgl">
		<div class="pgar oh font0"><table class="table__wrap"><tbody><tr> 
		'.( $page == 1 ? '
		<td class="table__cell" width="35%"> <div class="pagw">   <span class="dis"> ← Назад </span></div> </td>' : '
		<td class="table__cell" width="35%"> <div class="pagw"><a href="' . $url . '&page='.( $page - 1 ).'"> ← Назад </a></div></td>').'
		<td class="table__cell"><div class="pagw pagwb"><span class="blk"> '.$page.' из '.$total.' </span></div></td>
		'.( $page < $total ? '
		<td class="table__cell" width="35%"> <div class="pagw"><a href="' . $url . '&page='.( $page + 1 ).'"> Вперёд → </a>   </div> </td>' : '
		<td class="table__cell" width="35%"><div class="pagw"><span class="dis"> Вперёд → </span></div></td>').'
		</tr></tbody></table></div><div class="pagn pgl"> <table class="table__wrap"> <tbody><tr>
		'.( $page == 1 ? '
		<td class="table__cell"> <div class="pagw pagwr"><span class="cur mr">1</span></div> </td>':'
		<td class="table__cell"> <div class="pagw pagwr"><a href="' . $url . '&page=1" class="mr">1</a></div> </td>').'
		'; 
		if( $total < 9 ):
		for ( $i= - 6; $i <= 6; $i++ ):	
		echo '
		'.( $page + $i > 1 && $page + $i < $total  ? '
		'.( $page > 8 ? ''.( $i == -2 && $page + $i > 2 ? '<td class="table__cell "><i class="dot">...</i></td>':'' ).'':'').'
		'.( $i != 0  ? '
		<td class="table__cell"><div class="pagw pagwr"><a href="' . $url . '&page='.($page + $i).'" class="mr">'.($page + $i).'</a></div>':'
		<td class="table__cell"> <div class="pagw"><span class="cur">'.($page + $i).'</span></div> </td>').'
		'.( $i == 6 && $page + $i < $total - 1 ? '<td class="table__cell "><i class="dot">...</i></td>':'').'':'').'';	
		endfor;
		endif;
		if( $total >= 9 ):
		for ($i= - 2; $i <= 2; $i++):
		echo '
		'.( $page + $i > 1 && $page + $i < $total ? '
		'.( $i == -2 && $page + $i > 2 ? '<td class="table__cell "><i class="dot">...</i></td>':'').'
		'.( $i != 0 ? '
		<td class="table__cell"> <div class="pagw pagwr"><a href="' . $url . '&page='.($page + $i).'" class="mr">'.($page + $i).'</a></div> </td>':'
		<td class="table__cell"> <div class="pagw"><span class="cur">'.($page + $i).'</span></div> </td>').'	
		'.( $i == 2 && $page + $i < $total - 1 ? '<td class="table__cell "><i class="di">...</i></td>':'').'':'').'';	
		endfor;
		endif;	
		echo '
		'.( $page != $total ? '
		<td class="table__cell"> <div class="pagw"><a href="' . $url . '&page='.$total.'">'.$total.'</a></div> </td>':'
		<td class="table__cell"> <div class="pagw"><span class="cur">'.$total.'</span></div> </td>').'
		</tr></tbody></table></div></div></div></div></div>';
		
		endif;	

		}	

	}	
?>