Martin Matfiak
Martin Matfiak

Reputation: 25

Get VALUES from url in PHP

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?

Upvotes: 0

Views: 188

Answers (4)

Hmmm
Hmmm

Reputation: 1764

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

Upvotes: 0

Knight Rider
Knight Rider

Reputation: 188

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

Upvotes: 1

Daniel W.
Daniel W.

Reputation: 32290

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

Upvotes: 5

John Conde
John Conde

Reputation: 219814

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

Upvotes: 6

Related Questions