1

I have a strange error with a $_GET Value. I'm using this code for a query: array($_GET['cats'])

If I insert the get parameter manually, like: array(3,328) everything works fine. But if I use: array($_GET['cats']) and submit the cats by URL like ?cats=3,328 it does not work. What could be the issue?

1
  • make sure you can differentiate when something is an error, and when it is just showing behavior that you don't expect...in this case, as the 5 answers below show, what you are seeing is not an error, you are just expecting different results. reading the docs can be a good way to check this. Commented Oct 12, 2010 at 13:35

6 Answers 6

4

You can't plug a value like that. array($_GET['cats']) is equivalent to array('3,328'), if the value of $_GET['cats'] is 3,328. So basically, the value is a string, not a list of integers. What you want is:

explode(',', $_GET['cats'])
Sign up to request clarification or add additional context in comments.

Comments

4

array($_GET['cats']) will create an array containing the single element that’s value is the value of $_GET['cats'], no matter what value it is. In case of the string value 3,328 is would be identical to array('3,328').

If you want to turn the string value 3,328 into an array identical to array(3,328), use explode to split the string at , into strings and array_map with intval to turn each string into an integer:

$arr = array_map('intval', explode(',', $_GET['cats']));

Now this resulting array is really identical to array(3,328):

var_dump($arr === array(3,328));  // bool(true)

7 Comments

If it do it in your way, I get bool(true) but the query still does not work. This is my full code:
'$query_new = array_map('intval', explode(',', $_GET['cats'])); var_dump($query_new === array(3,328)); $args = array( 'category__and' => array($query_new), );'
The result ist still different to that one, where I set 3,328 manually
@Cindy: In your case $query_new already is the array in the form you want it. Doing var_dump($query_new === array(3,328)); is the proof that $query_new identical to array(3,328) (that what you wanted).
Yep. But the results are still different to those I get, if I insert 3,328 manuallay.
|
1

As others have said, $_GET['cats'] is a string as you're doing things at the moment.

However, if you change your URI querystring to ?cats[]=3,328 then $_GET['cats'] will be the array(3,328) ready for you to use.

Comments

1

Solution 1: Send HTTP GET parameters as an array in your URL

URL: parameters ?cats[]=3&cats[]=328

var_dump($_GET["cats"]) will result in:

array(2) {
  [0]=>
  string(1) "3"
  [1]=>
  string(3) "328"
}

Solution 2: Send numbers as one string in URL and process it with PHP

URL parameters: ?cats=3,328

... and then process it with some PHP code:

$cats = array_map("intval", explode(",", $_GET["cats"]));

var_dump($_GET["cats"]) will result in:

array(2) {
  [0]=>
  string(1) "3"
  [1]=>
  string(3) "328"
}

Comments

0

$_GET['cats'] is a simple string. If you want to get 3 and 328 as seperate values you need to use explode. You can than use foreach to print your exploded values.

Comments

0

You need to split up the GET-Parameters

$values = explode(',', $_GET['cats'])

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.