0

I have the following code. I want the code to get all of the data from the current url which is like

search.php?q=dvd+player&attr=23903940_portable_dvd_player&attr=23903945_cd&attr=23903935_mpeg_4

Code:

<?php if ($pagecount > 1)
    { 
        $paramsP1 = array_merge($_GET, array("page" => $pagecount-1));
        $new_page_1 = http_build_query($paramsP1);
    ?>

<li><a href="search.php?<?php echo $new_page_1; ?>"><b>&laquo; Previous</b></a></li>

    <?php } ?>

The code for some reason only obtains one of the GET variables which always seems to be the last one, so in this case its attr=23903935_mpeg_4 and adds "&page=X" onto it.

I have been looking at this code for a while however cant seem to understand why only one variable is returned.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75

2 Answers2

1

Instead of merging your get array with a new array, you could simply use the query string and append to it, like so:

$new_url = 'search.php?' . $_SERVER['QUERY_STRING'] . '&page=' . ($pagecount-1);
Ryan
  • 3,552
  • 1
  • 22
  • 39
  • Hi, I tried something similar to this however the only issue is that if say the user is on page 4 already and they wish to go on page 3 then "&page=4" will be on the url. search.php?search.php?q=dvd+player&attr=23903940_portable_dvd_player&attr=price_range_20_184&page=5&page=4 – user3134976 Jan 07 '14 at 14:11
0

Use parse_str function. But you have a incorect url because of same variables name.

   $url = 'search.php?q=dvd+player&attr=23903940_portable_dvd_player&attr=23903945_cd&attr=23903935_mpeg_4';
    parse_str($url, $output);
    var_dump($output);
sergio
  • 5,210
  • 7
  • 24
  • 46