Re: Comparing string to array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Davey wrote:
Hi Stut,

Tuesday, June 19, 2007, 10:16:02 AM, you wrote:

If you can't control $userparam and it has to look like you have it then
you're parsing of it is a little more involved, but still fairly simple.

What are you actually trying to do? Where will $userparam actually come
from? There is almost certainly a better way to do this, but without knowing all the details I'd be peeing in the wind.

Thank you for your code so far. Here is a more detailed explanation of
what I'm trying to do:

The designers here can create forms with whatever form elements they
like on them. They can name the form elements with any valid name. As
you know sometimes it is useful to give the form elements names which
will convert them into arrays in PHP, i.e.:

<input type="checkbox" name="test[color][]" value="red2">red2<br>
<input type="checkbox" name="test[color][]" value="green2">green2<br>
<input type="checkbox" name="test[color][]" value="blue2">blue2<br>

So $_POST['test']['color'] would contain an array of all the checked
values.

So far so good. The problem comes in that I don't know what the form
elements will be named, but I still need to check to see if they exist
within the $_POST array.

So knowing that $input_name = 'test[color][]' I then need to see if
$_POST['test']['color'] exists and get the value if it does.

To make matters worse it's perfectly legal to have a form element
named like:

<input type="checkbox" name="test['bob']['jazz'][]" value="red">red
<input type="checkbox" name="test['bob']['jazz'][]" value="green">green
<input type="checkbox" name="test['bob']['jazz'][]" value="blue">blue

Which when bought back into PHP will come out as:

array(1) {
  ["test"]=>
  array(2) {
    ["'bob'"]=>
    array(1) {
      ["'jazz'"]=>
      array(3) {
        [0]=>
        string(3) "red"
        [1]=>
        string(5) "green"
        [2]=>
        string(4) "blue"
      }
    }

Does that make it any clearer?

I have been playing with the RecursiveIteratorIterator this morning in
an attempt to solve it, but the results from that are less than
useless :(

I'm happy to try and explore the RAW post value instead if that would
be easier. I just figured there must be an easier way?

Here is the complete page you can test with:

-------------------------------- START
<pre>
<?php
    var_dump($_POST);

    $iterator =  new RecursiveIteratorIterator(new RecursiveArrayIterator($_POST));

    while($iterator->valid())
    {
        echo $iterator->key() . ' -- ' . $iterator->current();
echo "\n";

        $iterator->next();
    }
$userparam = "test['bob'][jazz][]";
    //  How to determine if $userparam exists in $_POST
?>
</pre>

<form method="post">

<input type="checkbox" name="test['bob'][jazz][]" value="red">red<br>
<input type="checkbox" name="test['bob'][jazz][]" value="green">green<br>
<input type="checkbox" name="test['bob'][jazz][]" value="blue">blue<br>
<input type="checkbox" name="test[sam][]" value="red2">red2<br>
<input type="checkbox" name="test[sam][]" value="green2">green2<br>
<input type="checkbox" name="test[sam][]" value="blue2">blue2<br>

<input type="submit">

</form>
-------------------------------- END

Remember the whole crux of this problem is that I have no control over
what the form name will be. It will be *valid*, but that is all. They
could nest the resulting array as deep in $_POST as they like.

If you have no control over what the fields in the form will be, what are you doing with the data? Surely if you're writing logic that requires you to know what the fields are called, you need to have control over it.

On the other hand, if you're just squidging the data somewhere can't you just iterate over the contents of $_POST?

I understand now that you're not in control of the form. What are you doing with the data that's submitted?

-Stut

--
http://stut.net/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux