Просмотр поста

.
suliman

Ыы
кодик постраничной разбивки

class Paging
{

/**
* Тип постраничной разбивки
*/

private $_tip;

/**
* Всего записей
*/

private $_total;

/**
* Всего страниц
*/

private $_totalPage;

/**
* Номер страницы для вывода
*/

private $_page;

/**
* Начало листинга
*/

private $_start;

/**
* Конец листинга
*/

private $_end;

/**
* Результат
*/

public $get;

/**
* Количество выбраных записей
*/

public $count;

/**
* Конструктор обьекта (Определение типа)
*/

public function __construct($tip = 'db')
{
if ($tip == 'db' || $tip == 'text' || $tip == 'smile'|| $tip == 'file') {
$this->_tip = $tip;
} else {
exit('Неопределён тип постраничной разбивки!');
}
}

/**
* Постаничная разбивка
*/

public function construct($q, $size)
{
if ($this->_tip == 'db') {
$this->_total = mysql_result(mysql_query(preg_replace('~^SELECT(.*?)FROM~isU', ' COUNT(*) ', $q)), 0);
$this->_totalPage = ceil($this->_total / $size);
$this->_page = isset($_POST['page']) ? (!empty($_POST['page']) && ctype_digit($_POST['page']) && $_POST['page'] >= 1 && $_POST['page'] <= $this->_totalPage ? $_POST['page'] : 1) : (!empty($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $this->_totalPage ? $_GET['page'] : 1);
$this->_start = ($this->_page * $size) - $size;
$this->_end = $this->_start + $size < $this->_total ? $this->_start + $size : $this->_total;
$this->get = mysql_query($q.' LIMIT '.$this->_start.', '.$size);
$this->count = mysql_num_rows($this->get);
} else if ($this->_tip == 'text') {
$this->_total = mb_strlen($q, 'utf-8');
$this->_totalPage = ceil($this->_total / $size);
$this->_page = isset($_POST['page']) ? (!empty($_POST['page']) && ctype_digit($_POST['page']) && $_POST['page'] >= 1 && $_POST['page'] <= $this->_totalPage ? $_POST['page'] : 1) : (!empty($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $this->_totalPage ? $_GET['page'] : 1);
$this->get = mb_substr($q, ($this->_page * $size) - $size, $size, 'utf-8');
if ($this->_page != 1) {
$pre = mb_substr($q, (($this->_page - 1) * $size) - $size, $size, 'utf-8');
$this->get = mb_substr($pre, mb_strrpos($pre, ' ', 'utf-8'), $size, 'utf-8').$this->get;
}
if ($this->_page != $this->_totalPage) {
$this->get = mb_substr($this->get, 0, mb_strrpos($this->get, ' ', 'utf-8'), 'utf-8');
}
} else if ($this->_tip == 'smile') {
$array = array();
$dir = scandir($q);
$total_dir = count($dir);
for ($i = 0; $i < $total_dir; $i++) {
if ($dir[$i] != '.' && $dir[$i] != '..') {
$_n = explode('.', $dir[$i]);
if ($_n[1] == 'gif' || $_n[1] == 'png' || $_n[1] == 'jpg') {
$array[] = $dir[$i];
}
}
}
$this->_total = count($array);
$this->_totalPage = ceil($this->_total / $size);
$this->_page = isset($_POST['page']) ? (!empty($_POST['page']) && ctype_digit($_POST['page']) && $_POST['page'] >= 1 && $_POST['page'] <= $this->_totalPage ? $_POST['page'] : 1) : (!empty($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $this->_totalPage ? $_GET['page'] : 1);
$this->_start = ($this->_page * $size) - $size;
$this->_end = $this->_start + $size < $this->_total ? $this->_start + $size : $this->_total;
$this->get = array();
for ($i = $this->_start; $i < $this->_end; $i++) {
$this->get[] = $array[$i];
}
$this->count = count($this->get);
} else {
$array = file($q);
$this->_total = count($array);
$this->_totalPage = ceil($this->_total / $size);
$this->_page = isset($_POST['page']) ? (!empty($_POST['page']) && ctype_digit($_POST['page']) && $_POST['page'] >= 1 && $_POST['page'] <= $this->_totalPage ? $_POST['page'] : 1) : (!empty($_GET['page']) && ctype_digit($_GET['page']) && $_GET['page'] >= 1 && $_GET['page'] <= $this->_totalPage ? $_GET['page'] : 1);
$this->_start = ($this->_page * $size) - $size;
$this->_end = $this->_start + $size < $this->_total ? $this->_start + $size : $this->_total;
$this->get = array();
for ($i = $this->_start; $i < $this->_end; $i++) {
$this->get[] = $array[$i];
}
$this->count = count($this->get);
}
}

/**
* Вывод меню навигации
*/

public function links()
{
paging('?'.($_SERVER['QUERY_STRING'] ? preg_replace('~&page=[0-9]*~', '', strtok($_SERVER['QUERY_STRING'], session_name())) : ''), $this->_page, $this->_totalPage, $this->_tip, $this->_start, $this->_end, $this->_total);
}

}

/**
* Функция выводящая меню постраничной навигации
*/

function paging($link, $page, $total_page, $tip, $start, $end, $total)
{
$link_list = '';
$_start = $page - 3;
$_end = $page + 3;
for ($i = $_start; $i <= $_end; $i++) {
$link_list .= ($i <= $total_page && $i >= 1 ? ($page != $i ? ' <a href="'.$link.'page='.$i.'&amp;'.SID.'">'.$i.'</a> ' : ' ('.$i.') ') : '');
}
echo hr().'<form action="'.$link.SID.'" method="post">
<div style="text-align: center;">
'.($page > 1 ? '<a href="'.$link.'page='.($page - 1).'&amp;'.SID.'">&lt;&lt;Пред. </a>' : '&lt;&lt;Пред. ').' | '.($page < $total_page ? '<a href="'.$link.'page='.($page + 1).'&amp;'.SID.'"> След.&gt;&gt;</a>' : ' След.&gt;&gt;').'<br />
Стр.: '.($_start > 1 ? '<a href="'.$link.'page=1&amp;'.SID.'">1</a> ... ' : '').$link_list.($_end < $total_page ? ' ... <a href="'.$link.'page='.$total_page.'&amp;'.SID.'">'.$total_page.'</a>' : '').'<br />
К стр.: <input name="page" maxlength="3" size="5" value="'.$page.'" />
<input type="submit" value="Go" /><br />
'.($tip == 'text' ? '' : 'Показано с '.($start + 1).' по '.$end.' из '.$total).'
</div>
</form>';
}