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?

4
  • @R.CanserYanbakan He has 3 different IDs, getting it simply with the $_GET is not going to solve the problem he has. Commented Dec 10, 2014 at 16:15
  • @Jordy then use $_SERVER['QUERY_STRING'] :) Commented Dec 10, 2014 at 16:16
  • 2
    doesnt look like a valid request string to me, ID[]=1&ID[]=5 would make sense Commented Dec 10, 2014 at 16:16
  • use different name for vars or use a form method post with array =) Commented Dec 10, 2014 at 16:16

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

1 Comment

What if he does not want to use [] (array) in his query string? You should write that solution either.
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

Comments

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()

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.