Jump to content

Recommended Posts

Sorry if I sound like a total idiot, but I don't know how to do this  :)

 

I have a bunch of array variables that I need to access based on an inputted value.  It's a dropdown menu, so the values are predetermined.

 

For example:

<?php
$var1 = array("blah1","blah2","blah3","blah4");
$var2 = array("foo1","foo2","foo3","foo4");
$var3 = array("nothing1","nothing2","nothing3","nothing4");
?>
<form method="get" action="this.php" name="this">
<select>
<option value="var1">1</option>
<option value="var2">2</option>
<option value="var3">3</option>
</select>
</form>

 

So I want it so that if the user selects option 1, var1 can be accessed and i can use the array to do the calculation needed... How can I use the value to access the variable?

Link to comment
https://forums.phpfreaks.com/topic/158262-find-variable-based-on-inputted-value/
Share on other sites

<?php
if (isset($select_var))
{
  switch($select_var)
    case 1:
      $var_array = array("blah1","blah2","blah3","blah4");
      break;
    case 2:
      $var_array = array("foo1","foo2","foo3","foo4");
      break;
    case 3:
      $var_array = array("nothing1","nothing2","nothing3","nothing4");
      break;
}

// do what ever you want with $var_array
?>
<select name='select_var'>
<option value="var1">1</option>
<option value="var2">2</option>
<option value="var3">3</option>
</select>

 

Think this will help

alternatively...

 

<?php
$var[1] = array("blah1","blah2","blah3","blah4");
$var[2] = array("foo1","foo2","foo3","foo4");
$var[3] = array("nothing1","nothing2","nothing3","nothing4");

// example:
print_r($var[$_GET['this']]);
?>
<form method="get" action="this.php" name="this">
<select>
<option value="var1">1</option>
<option value="var2">2</option>
<option value="var3">3</option>
</select>
</form>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.