ВаленокPC
Пользователь
Всем привет, тут случайным образом обнаружил небольшой "баг" в generate_pagination. Если у ссылки в конце нет дополнительных параметров, то при перелистывание на следующую страницу выдаст ошибку!
Пример:
Исправление:
Заменить в файле functions.php
на это
и в page_header.tpl это
На
Пример:
index.php
. И мы листаем на следующую страницу списка. то по идее должно быть так index.php?start=20
, а он открывает так index.php&start=20
. И это не правильно.Исправление:
Заменить в файле functions.php
PHP:
//
// Pagination routine, generates
// page number sequence
//
function generate_pagination ($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{
global $lang, $template;
// Pagination Mod
$begin_end = 3;
$from_middle = 1;
/*
By default, $begin_end is 3, and $from_middle is 1, so on page 6 in a 12 page view, it will look like this:
a, d = $begin_end = 3
b, c = $from_middle = 1
"begin" "middle" "end"
| | |
| a b | c d |
| | | | | | |
v v v v v v v
1, 2, 3 ... 5, 6, 7 ... 10, 11, 12
Change $begin_end and $from_middle to suit your needs appropriately
*/
$total_pages = ceil($num_items/$per_page);
if ($total_pages == 1 || $num_items == 0)
{
return '';
}
$on_page = floor($start_item / $per_page) + 1;
$page_string = '';
if ($total_pages > ((2*($begin_end + $from_middle)) + 2))
{
$init_page_max = ( $total_pages > $begin_end ) ? $begin_end : $total_pages;
for ($i = 1; $i < $init_page_max + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "&start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $init_page_max)
{
$page_string .= ", ";
}
}
if ($total_pages > $begin_end)
{
if ($on_page > 1 && $on_page < $total_pages)
{
$page_string .= ( $on_page > ($begin_end + $from_middle + 1) ) ? ' ... ' : ', ';
$init_page_min = ( $on_page > ($begin_end + $from_middle) ) ? $on_page : ($begin_end + $from_middle + 1);
$init_page_max = ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? $on_page : $total_pages - ($begin_end + $from_middle);
for ($i = $init_page_min - $from_middle; $i < $init_page_max + ($from_middle + 1); $i++)
{
$page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "&start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $init_page_max + $from_middle)
{
$page_string .= ', ';
}
}
$page_string .= ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? ' ... ' : ', ';
}
else
{
$page_string .= ' ... ';
}
for ($i = $total_pages - ($begin_end - 1); $i < $total_pages + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "&start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $total_pages)
{
$page_string .= ", ";
}
}
}
}
else
{
for ($i = 1; $i < $total_pages + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "&start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $total_pages)
{
$page_string .= ', ';
}
}
}
if ($add_prevnext_text)
{
if ($on_page > 1)
{
$page_string = ' <a href="' . $base_url . "&start=" . ( ( $on_page - 2 ) * $per_page ) . '">' . $lang['PREVIOUS_PAGE'] . '</a> ' . $page_string;
}
if ($on_page < $total_pages)
{
$page_string .= ' <a href="' . $base_url . "&start=" . ( $on_page * $per_page ) . '">' . $lang['NEXT_PAGE'] . '</a>';
}
}
$pagination = ($page_string) ? '<a class="menu-root" href="#pg-jump">'. $lang['GOTO_PAGE'] .'</a> : '. $page_string : '';
$pagination = str_replace('&start=0', '', $pagination);
$template->assign_vars(array(
'PAGINATION' => $pagination,
'PAGE_NUMBER' => sprintf($lang['PAGE_OF'], ( floor($start_item/$per_page) + 1 ), ceil( $num_items / $per_page )),
'PG_BASE_URL' => $base_url,
'PG_PER_PAGE' => $per_page,
));
return $pagination;
}
PHP:
//
// Pagination routine, generates
// page number sequence
//
function generate_pagination ($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{
global $lang, $template;
// Pagination Mod
$begin_end = 3;
$from_middle = 1;
/*
By default, $begin_end is 3, and $from_middle is 1, so on page 6 in a 12 page view, it will look like this:
a, d = $begin_end = 3
b, c = $from_middle = 1
"begin" "middle" "end"
| | |
| a b | c d |
| | | | | | |
v v v v v v v
1, 2, 3 ... 5, 6, 7 ... 10, 11, 12
Change $begin_end and $from_middle to suit your needs appropriately
*/
$total_pages = ceil($num_items/$per_page);
if ($total_pages == 1 || $num_items == 0)
{
return '';
}
$on_page = floor($start_item / $per_page) + 1;
$page_string = '';
$url_delim = (strpos($base_url, '?') === false) ? '?' : ((strpos($base_url, '?') === strlen($base_url) - 1) ? '' : '&');
if ($total_pages > ((2*($begin_end + $from_middle)) + 2))
{
$init_page_max = ( $total_pages > $begin_end ) ? $begin_end : $total_pages;
for ($i = 1; $i < $init_page_max + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "{$url_delim}start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $init_page_max)
{
$page_string .= ", ";
}
}
if ($total_pages > $begin_end)
{
if ($on_page > 1 && $on_page < $total_pages)
{
$page_string .= ( $on_page > ($begin_end + $from_middle + 1) ) ? ' ... ' : ', ';
$init_page_min = ( $on_page > ($begin_end + $from_middle) ) ? $on_page : ($begin_end + $from_middle + 1);
$init_page_max = ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? $on_page : $total_pages - ($begin_end + $from_middle);
for ($i = $init_page_min - $from_middle; $i < $init_page_max + ($from_middle + 1); $i++)
{
$page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "{$url_delim}start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $init_page_max + $from_middle)
{
$page_string .= ', ';
}
}
$page_string .= ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? ' ... ' : ', ';
}
else
{
$page_string .= ' ... ';
}
for ($i = $total_pages - ($begin_end - 1); $i < $total_pages + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "{$url_delim}start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $total_pages)
{
$page_string .= ", ";
}
}
}
}
else
{
for ($i = 1; $i < $total_pages + 1; $i++)
{
$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "{$url_delim}start=" . ( ( $i - 1 ) * $per_page ) . '">' . $i . '</a>';
if ($i < $total_pages)
{
$page_string .= ', ';
}
}
}
if ($add_prevnext_text)
{
if ($on_page > 1)
{
$page_string = ' <a href="' . $base_url . "{$url_delim}start=" . ( ( $on_page - 2 ) * $per_page ) . '">' . $lang['PREVIOUS_PAGE'] . '</a> ' . $page_string;
}
if ($on_page < $total_pages)
{
$page_string .= ' <a href="' . $base_url . "{$url_delim}start=" . ( $on_page * $per_page ) . '">' . $lang['NEXT_PAGE'] . '</a>';
}
}
$pagination = ($page_string) ? '<a class="menu-root" href="#pg-jump">'. $lang['GOTO_PAGE'] .'</a> : '. $page_string : '';
$pagination = str_replace("{$url_delim}start=0", '', $pagination);
$template->assign_vars(array(
'PAGINATION' => $pagination,
'PAGE_NUMBER' => sprintf($lang['PAGE_OF'], ( floor($start_item/$per_page) + 1 ), ceil( $num_items / $per_page )),
'PG_BASE_URL' => $base_url,
'PG_PER_PAGE' => $per_page,
));
return $pagination;
}
HTML:
<script type="text/javascript">
function go_to_page ()
{
var page_num = (parseInt( $('#pg-page').val() ) > 1) ? $('#pg-page').val() : 1;
var pg_start = (page_num - 1) * {PG_PER_PAGE};
window.location = '{PG_BASE_URL}&start=' + pg_start;
return false;
}
</script>
HTML:
<script type="text/javascript">
function go_to_page ()
{
var base_url = '{PG_BASE_URL}';
var page_num = (parseInt( $('#pg-page').val() ) > 1) ? $('#pg-page').val() : 1;
var pg_start = (page_num - 1) * {PG_PER_PAGE};
if (base_url.indexOf('?') == -1)
{
window.location = base_url + '?start=' + pg_start;
}
else
{
window.location = base_url.replace(/&/g, '&') + '&start=' + pg_start;
}
return false;
}
</script>
Спасибо за внимание.