Kaka
Kaka

Reputation: 395

Get current URL for pagination

I have a paging function for multiple pages.

Instead of setting the URL in the function. e.g mypaginatefunction($current_page_number, 'forum/?action=showthread&id=$threadid, $per_page I just wanna PHP to figure it out.

Currently I'm using:

$link = strtok($_SERVER['REQUEST_URI'],'?').'?'.http_build_query($_GET);
<a href=\"$link&page=$page_number\">$page_number</a>

But that don't work so good. It will keep the previous $_GET too

For example lets say I'm on http://xxx/forum/?action=showthread&id=181

And I press on page 2 http://xxx/forum/?action=showthread&id=181&page=2

but then if I press page 3 it becomes: http://mysite/forum/?action=showthread&id=181&page=2&page=3

I'd only want &page=3

What should I do?

Upvotes: 1

Views: 1912

Answers (3)

Expedito
Expedito

Reputation: 7805

$link = strtok($_SERVER['REQUEST_URI'],'?').'?page='.$_GET['page']);

If you want the action part you can do it with:

$link = strtok($_SERVER['REQUEST_URI'],'?').'?action=showthread&page='.$_GET['page']);

Upvotes: 0

Jan.J
Jan.J

Reputation: 3080

Your approach is not the best, but for a temporary solution you can use this:

$url = 'http://xxx/forum/?action=showthread&id=181&page=2';
$new_url = preg_replace('/page=[\d+]/', 'page=3', $url);

Upvotes: 0

EJTH
EJTH

Reputation: 2219

$queryParams = $_GET;
$queryParams['page'] = $page_number;
$link = strtok($_SERVER['REQUEST_URI'],'?').'?'.http_build_query($queryParams);

Upvotes: 2

Related Questions