0

I am getting data via $_GET['value'] and I want that value to represent a variable of an array.

I'm building a sort by drop-down menu with 3 options.

I want to search an array for a match.

So if the $_GET['value'] == 'name' then I want to search my array for 'name' and use that value as the data sent to my query.

Sort By:

<select name='1' value='name'>Name</select>
<select name='1' value='manufacturer'>Brand</select>

So when php gets the values of the select menu I want to do an array search for that name and use its value to represent a variable to be sent to my query.

1
  • show your html, it will make more sense Commented Jul 25, 2011 at 2:04

3 Answers 3

1

Determine if the value received in $_GET is in your array, then retrieve it by the index.

// Your values are stored in this array
$your_array = array("name" => "some name", "place" => "some place");

// Check if the `value` is a key in your array
if (array_key_exists($_GET['value'], $your_array)) {
   $search_value = $your_array[$_GET['value']];
}
else {
  // Not found.. Use some default value instead.
}
Sign up to request clarification or add additional context in comments.

2 Comments

@RPM that's a typo! It should be $your_array[$_GET['value']] Fixed above. That way, it will find any key passed in GET
Thanks I find this as a very useful function because it isnt a live query to database is pre defined
0

You can check if the value is in the array by using in_array() method

Example:

$yourArray = array(...);

if ($_GET['value'] == 'name') {
   if (in_array($theName,$yourArray)) {
      // build your query
   }
}

Comments

0
if (isset($_GET['value'])) {
     if ($value = $your_array[$_GET['value']]) {
         //do something by this $value
     }
}
else {
   echo 'nothing';
}

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.