tags:

views:

223

answers:

4

Hello, I'm creating pagging for some data. For example, I have a page:

http://example.com/news.php?type=bla&smth=bla

There are I have a list of news with links to another pages. Link to the first page is:

http://example.com/news.php?type=bla&smth=bla&page=1

Here is script, which creates pages links:

print '<a href="?'.$_SERVER['QUERY_STRING'].'&page=1"><<</a>';

But after clicking to another pages the link URL is very large and it looks like:

http://example.com/news.php?type=bla&amp;smth=bla&amp;page=1&amp;page=2&amp;page=1&amp;page=3

How can I change that?

A: 

Every time you run that script a page parameter will be appended to the query string. For instance when you get to page one the query string will be:

http://site.com/news.php?type=bla&amp;smth=bla

and when you get to page to the query string will be

http://site.com/news.php?type=bla&amp;smth=bla&amp;page=1

and so on for everyone page that you go to.

I would suggest instead writing a function that takes parameters from an array and formats them as a query string and then change them as needed.

So something along the lines of:

<?php

$parameters = array(
  'type' => 'bla',
  'smth' => 'bla',
  'page' => 1,  // Of course, logic up front to change the value of this
);

print print '<a href="?' . format_url_parameters($parameters) .'>Link</a>';

// format_url_parameters producing a valid query string here
codeincarnate
I understand my problem, but i didn't know, how to repair it. Now i understand and have some ideas. Thank you.
Ockonal
+3  A: 

You are just append the new paremeter to the old ones but you don’t replace it if already existing. So you rather need to merge the old query string with the new one:

// either by merging both arrays
$query = array_merge($_GET, array('page'=>1));
// or by the union of both
$query = array('page'=>1) + $_GET;
// or by altering the array
$query = $_GET;
$query['page'] = 1;

And PHP does already have a http_build_str that can build you a query string from an associative array:

print '<a href="?' . htmlspecialchars(http_build_str($query)) . '">&lt;&lt;</a>';


Edit    Here’s an alternative definition of http_build_str:

if (!function_exists('http_build_str')) {
    function http_build_str($query, $prefix='', $arg_separator='') {
        if (!is_array($query)) {
            return null;
        }
        if ($arg_separator == '') {
            $arg_separator = ini_get('arg_separator.output');
        }
        $args = array();
        foreach ($query as $key => $val) {
            $name = $prefix.$key;
            if (!is_numeric($name)) {
                $args[] = rawurlencode($name).'='.urlencode($val);
            }
        }
        return implode($arg_separator, $args);
    }
}
Gumbo
Hm...Call to undefined function http_build_str()Why?
Ockonal
requirement for http_build_str() - PECL pecl_http >= 0.23.0
thephpdeveloper
Are there any function like http_build_str? Or can i make your code working in other ways?
Ockonal
Great work! Thanks.
Ockonal
A: 

You could also simply use

print '<a href="http://'. $_SERVER["HTTP_HOST"] . $_SERVER['REQUEST_URI'].'?page=1"><<</a>';

this generates an absolute URL each time.

Alex
+1  A: 

The simplest is to do the following:

$params = $_GET;     // make a copy of the querystring params
$params['page'] = 1; // will replace any existing 'page' parameter
echo '<a href="?'.http_build_query($params)'">Page 1</a>';
Ciaran McNulty