-1

i am getting category name using $_GET['category']

and assigning it to $category like this.

// if $_GET['category'] contains space then create array of categories

if(strpos($_GET['category'],' ') !== false) {

$category = explode (' ',$_GET['category']);

}else{

$category = $_GET['category'];

}

so $category can either be an array or string

and i have dropdown select box on website and would like to show the user searched category by setting selected="selected" to the specific option in dropdown select.

so my question is can i compare string with an array ?, without checking if its an array , like this.

<option value="option1"<?= (isset($category) && ($category == 'option1')) ? ' selected="selected"' : '';?>>option1</option>
<option value="option2"<?= (isset($category) && ($category == 'option2')) ? ' selected="selected"' : '';?>>option2</option>
<option value="option3"<?= (isset($category) && ($category == 'option3')) ? ' selected="selected"' : '';?>>option3</option>
<option value="option4"<?= (isset($category) && ($category == 'option4')) ? ' selected="selected"' : '';?>>option4</option>

or i have to check whether the $category is not an array like this.

<option value="option1"<?= (isset($category) && (!is_array($category)) && ($category == 'option1')) ? ' selected="selected"' : '';?>>option1</option>
<option value="option2"<?= (isset($category) && (!is_array($category)) && ($category == 'option2')) ? ' selected="selected"' : '';?>>option2</option>
<option value="option3"<?= (isset($category) && (!is_array($category)) && ($category == 'option3')) ? ' selected="selected"' : '';?>>option3</option>
<option value="option4"<?= (isset($category) && (!is_array($category)) && ($category == 'option4')) ? ' selected="selected"' : '';?>>option4</option>
2
  • So what exactly is your question? Commented Jul 23, 2014 at 5:03
  • 1
    instead of string, if you can make $category as an array, it would be easier for you. In else block, if you will write $category = array($_GET['category']);, then in your HTML code, you can check with only array not with string. Commented Jul 23, 2014 at 5:08

3 Answers 3

2

you need not to set category value as a string. just use as following.

if(isset($_GET['category'])) { 
$category = explode (' ',$_GET['category']);
}

<option value="option1"<?= (isset($category) && in_array('option1', $category)) ? ' selected="selected"' : '';?>>option1</option>
<option value="option2"<?= (isset($category) && in_array('option2', $category)) ? ' selected="selected"' : '';?>>option2</option>
Sign up to request clarification or add additional context in comments.

5 Comments

thatys not the issue here , the issue is do have to check whether given data is array or not before comparing it to the string.
@amb You code wrongly. You get a good advice. You should not say ' This is not the issue here. '
@AMB if you compare a string with an array you'll always get false. You could have tested that yourself in a couple of minutes. This answer is a good method for doing what you want.
@MikeW i tried with both cases and its working fine but wanted to make sure it wont cause any issue after .
sorry for the downvote , i didnt read the whole answer, just made my descision after i saw isset, please edit the answer so i can remove downvote.
1

You should always use explode. Like this you get always an array.

$category = explode (' ',$_GET['category']);

After, you can test if the value is in the array with

if(in_array($option, $category)) echo 'selected="selected";

Comments

0

of course you can test an array with a string witout testing if it is array or it is string .. but you should test if the variable isset because empty string is equal to null value if you're using the operator ==like you did on your code , otherwise you can use the === instead

Comments

Your Answer

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