Page index gremlins (We hatez Sources).

Started by Antechinus, September 15, 2019, 06:39:54 AM

Previous topic - Next topic

Antechinus

Ok, so I had a bright idea of adding an "improvement" to my nifty WIP page index mod. The plan is to append an extra class to the markup for the first and last pages, to give some extra presentation possibilities.

Getting it working for the first page was easy. That is already sorted. Original code looked like this:

if($max_value > $num_per_page)
{
$dropdown_url = ($flexible_start ? $base_url : strtr($base_url, array('%' => '%%')) . ';start=');
if(strpos($dropdown_url, '%1$d') === false){
if(strpos($dropdown_url, 'start=') === false){
$dropdown_url .= ';start=%1$d';
}
else{
$dropdown_url = str_replace('start=', 'start=%1$d', $dropdown_url);
}
}

$pageindex = '<span class="pageindex_wrapper"><select class="navPages_select" onchange="javascript:window.location=(\'' . $dropdown_url . '\').replace(/%1\$d/, this.value);">';
$display_page = 0;
$myPage = ($start / $num_per_page);
$tmpMaxPages = (int) (($max_value - 1) / $num_per_page) * $num_per_page;
for ($counter = 0; $counter < $max_value; $counter += $num_per_page)
{
$pageindex .= '<option value="' . ($num_per_page * $display_page) . '"';
if($display_page == $myPage) $pageindex .= ' selected="selected"';
if($display_page == $myPage){
$pageindex .= '>'.sprintf($txt['navPages_selected'], ++$display_page, ($tmpMaxPages / $num_per_page + 1));
}
else{
$pageindex .= '>'.sprintf($txt['navPages_select'], ++$display_page);
}
$pageindex .= '</option>';
}
$pageindex .= '</select>';
// Show 'Prev'
if ($start >= $num_per_page)
$pageindex .= sprintf($base_link, $start - $num_per_page, 'previous_page', $txt['navPages_previous']);
// Show 'Next'.
$display_page = ($start + $num_per_page) > $max_value ? $max_value : ($start + $num_per_page);
if ($display_page <= $tmpMaxPages)
$pageindex .= sprintf($base_link, $display_page, 'next_page', $txt['navPages_next']);
$pageindex .= '</span>';
}


So I just figured if I nicked the bit that says "if ($start >= $num_per_page)" and frigged around with it I could get my appended class for the first page, and I could. This works nicely:

$pageindex = '<span class="pageindex_wrapper';

if ($start < $num_per_page)
$pageindex .= ' firstpage';

$pageindex .= '"><select class="navPages_select" onchange="javascript:window.location=(\'' . $dropdown_url . '\').replace(/%1\$d/, this.value);">';


The trouble is with the class for the last page. The original function uses $tmpMaxPages to figure out if there should be a "previous page" link, but doesn't define $tmpMaxPages until after where I want to stick my class in. So I figured if I went and used the same calculation, being this:

(int) (($max_value - 1) / $num_per_page) * $num_per_page;

then I could use that to jam an ersatz $tmpMaxPages in where I wanted it. Doesn't work. :P Have tried this:

if ($display_page > (int) (($max_value - 1) / $num_per_page) * $num_per_page)
$pageindex .= ' lastpage';


and that doesn't append anything. Neither does throwing  in === or == instead of >. Neither does this:

if ($display_page == (int) (($max_value - 1) / $num_per_page))

The whole thing works just fine for switching pages. It just won't append the class on the last page.

Any takers? :D


d3vcho

"Greeting Death as an old friend, they departed this life as equals"

Antechinus

Lolz. Nevermind. Nailed it. I had to use $start instead of $display_page. I hadn't noticed that $display_page wasn't defined at that point either.

This code works:

$pageindex = '<span class="pageindex_wrapper';

if ($start < $num_per_page)
$pageindex .= ' firstpage';

if ($start == (int) (($max_value - 1) / $num_per_page) * $num_per_page)
$pageindex .= ' lastpage';

$pageindex .= '"><select class="navPages_select" onchange="javascript:window.location=(\'' . $dropdown_url . '\').replace(/%1\$d/, this.value);">';


Yay! I iz PHP expert now.

Advertisement: