3

this is probably quite simple but I could do with some help.

I am trying to create a small PHP function that will display a single value form a multidimensional array when the user used two dropdown boxes to select the row and column of the array.

So, the user will make a selection from the first dropdown box, which will contain the titles of the rows, and then make a selection from a second dropdown box, which will contain the titles of the columns. Once the selections have been made, the function then needs to output the value for the specific row and column selected.

I thought I had created an array that would work but, sadly no. I have 6 rows and 6 columns in my data table.

Also, is there a JQuery or Javascript alternative?

Just looking for a few pointers to get me on my way.

Thanks in advance,

Micanio

2
  • If you post some code it would be easier to understand what you are trying to do
    – meouw
    Feb 5, 2010 at 16:05
  • No code at the moment, am struggling with where to start....
    – Micanio
    Feb 5, 2010 at 16:22

3 Answers 3

1

You could either do this on the server-side or through JS.

JS: Have the script update a hidden form field with the value using the onChange() event of the drop downs. Then simply grab that hidden field when the form is posted back to the server (of source always checking for valid data).

PHP: The form will provide the two values $_POST['field1'] and $_POST['field2'] (which of course you will sanitize before using). The script could define a multidimensional array that you could feed those two values into:

$finalValue = $mdArray[$SanitizedField1][$SanitizedField2];

From there just store the $finalValue however you'd like.

1
  • dead simple ... I thought the same.
    – Adam Kiss
    Feb 6, 2010 at 18:33
0
$data = array();
for ($i=0;$i<6;$i++) {
    $data[$i] = array();
    for ($j=0;$j<6;$j++)
        $data[$i][$j] = rand(0,100);
}

This should create an array similar to what you have described.

You can then access it like so...

echo $data[0][3];

Your form would need two select fields, both will values 0-5 then you can take the both of them and use them to access a value in your array.

0

If I understand your question correctly, you need something like a user selects a drop down list. Once selected, the option populates a second list. A real world example would be a user selects on Country to fill in a form and it will populates the States drop down list.

This kind of functionality is usually done with javascript not server side php.

http://javascript.internet.com/forms/country-state-drop-down.html

1
  • That's close to what I'm looking for, but not quite. It's almost like I'm specifying coordinates - I need the user to input X and Y into the dropdown boxes and that will give me a value Z that is stored at those co-ordinates.... Is that too simplistic? Thanks for your answer though...
    – Micanio
    Feb 5, 2010 at 16:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.