0

I need to get ID´s from url:

http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID=1&ID=5&ID=24&ID=32

If i use $_GET['ID'] a still get only last ID value. I need to get all of them to array, or select.

Can anybody help me?

Albzi
  • 15,431
  • 6
  • 46
  • 63

4 Answers4

6

Use array syntax:

 http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID[]=1&ID[]=5&ID[]=24&ID[]=32

var_dump($_GET['ID']);

array(4) {
      [0]=>
      int(1)
      [1]=>
      int(5)
      [2]=>
      int(24)
      [3]=>
      int(32)
      }
} 

echo $_GET['ID'][2]; // 24
John Conde
  • 217,595
  • 99
  • 455
  • 496
5

The format in the URL is wrong. The second "ID" is overwriting the first "ID".. use an array:

http://www.example.org/?id[]=1&id[]=2&id[]=3

In PHP:

echo $_GET['id'][0]; // 1
echo $_GET['id'][1]; // 2
echo $_GET['id'][2]; // 3
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
1

To get this you need to make ID as array and pass it in the URL

http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID[]=1&ID[]=5&ID[]=24&ID[]=32 and this can be manipulated at the backend like this

$urls = $_GET['ID'];
foreach($urls as $url){
   echo $url;
}

OR

An alternative would be to pass json encoded arrays

http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID=[1,2,24,32]

which can be used as

$myarr = json_decode($_GET['ID']); // array(1,2,24,32)

I recommend you to also see for this here. http_build_query()

Community
  • 1
  • 1
Knight Rider
  • 186
  • 10
0

it's wrong but if you really want to do this

<?php

function getIds($string){
    $string = preg_match_all("/[ID]+[=]+[0-9]/i", $string, $matches);

    $ids = [];

    foreach($matches[0] as $match)
    {
       $c = explode("=", $match);
       $ids [] = $c[1];         
    }

    return $ids;
}


// you can change this with $_SERVER['QUERY_STRING']
$url = "http://www.aaaaa/galery.php?position=kosice&kategory=Castles&ID=1&ID=5&ID=24&ID=32";

$ids = getIds($url);


var_dump($ids);
Hmmm
  • 1,774
  • 2
  • 13
  • 23