Page 1 of 1

get array values?

Posted: Mon Aug 21, 2006 8:22 am
by train
Hello,

I am looking at posted variables like so:

Code: Select all

foreach ($_POST as $key => $value) {
       echo "$key has a value of $value<br>";
But it is the values in the array that I am after... and the resultant output:

Code: Select all

custom_12 has a value of Array
first_name has a value of George
middle_name has a value of Samuel
Doesnt help me too much. How can I take a look at what is IN those arrays?

The array custom_12 is a series of checkboxes, and depending on which is checked, I want to send an email to a particular person. It will be a bunch of if statements, I guess... if this box is checked, send a mail to this person and if this box is checked, send an email to this person... and so on...

Thanks!

Posted: Mon Aug 21, 2006 8:33 am
by SteveMellor
I think what you might be after is the following.

Assume that I have sent a form with the following field names:
name
address
postCode

now this form is sent as Post data. When the form is recieved by your script you can refer to the variables thus:

$_POST['name']
$_POST['address']
$_POST['postCode']

Any help??

Posted: Mon Aug 21, 2006 8:37 am
by train
SteveMellor wrote:I think what you might be after is the following.

Assume that I have sent a form with the following field names:
name
address
postCode

now this form is sent as Post data. When the form is recieved by your script you can refer to the variables thus:

$_POST['name']
$_POST['address']
$_POST['postCode']

Any help??
I get 'Array' back for 'custom_12' and it IS an array... but I need to see what is IN the array.

Thanks a lot...

Posted: Mon Aug 21, 2006 8:39 am
by Benjamin
You can nest foreach loops if your dealing with arrays containing arrays..

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            echo "{$key}={$value}<br />\n";
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}

Posted: Mon Aug 21, 2006 8:43 am
by train
astions wrote:You can nest foreach loops if your dealing with arrays containing arrays..

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            echo "{$key}={$value}<br />\n";
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
Now I get

Code: Select all

1=1
2=1
3=1
4=1
5=1
first_name=Chevy
middle_name=
Which is a lot closer... the 2=1 means the second checkbox is the "custom_12" array is checked.

And so I guess if that field is "custom_12" then

"custom_12[2]" would be checked?

But when I try

Code: Select all

echo ($_POST[custom_12[2]]);
It doesnt work...


Thanks!

Posted: Mon Aug 21, 2006 8:56 am
by jayshields
You would need to refer to it as $_POST['custom_12'][2] I think, not sure though.

Referring to your original question:
Why not set the value of the checkboxes to the email address you want to send to. It would make things alot easier for you.

You could then take astions code and modify it like so.

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            mail($value, 'subject', 'message');
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
Instead of just knowing that the second checkbox was checked (meaning nothing in terms of application logic), you will know who to send the email to.

Posted: Mon Aug 21, 2006 9:03 am
by train
jayshields wrote:You would need to refer to it as $_POST['custom_12'][2] I think, not sure though.

Referring to your original question:
Why not set the value of the checkboxes to the email address you want to send to. It would make things alot easier for you.

You could then take astions code and modify it like so.

Code: Select all

foreach ($_POST as $key => $data)
{
    if (is_array($data))
    {
        foreach ($data as $key => $value) {
            mail($value, 'subject', 'message');
        }
    } else {
        echo "{$key}={$data}<br />\n";
    }
}
Instead of just knowing that the second checkbox was checked (meaning nothing in terms of application logic), you will know who to send the email to.
I cant change the checkboxes because they are dynamically generated.

That doesnt work... I still cant read $_POST[custom_12][2]

Thanks though. This is getting me closer.

Posted: Mon Aug 21, 2006 9:10 am
by Benjamin
To see where everything is..

Code: Select all

echo '<pre>' . print_r($_POST, true) . '</pre>';

Posted: Mon Aug 21, 2006 9:16 am
by train
astions wrote:To see where everything is..

Code: Select all

echo '<pre>' . print_r($_POST, true) . '</pre>';
Yeah, thats really cool. Thanks man.

But still, I need to evaluate the array... so if custom_12[2] is 1, a certain action happens.

I guess I can just

Code: Select all

if (custom_12[1] = 1)
    {
     (do this);
    }
if (custom_12[2] = 1)
    {
     (do this);
if (custom_12[3] = 1)
    {
     (do this);
    }
And so on...

You guys rock. Thanks!

Posted: Mon Aug 21, 2006 9:55 am
by jayshields
I still cant read $_POST[custom_12][2]
I'm not suprised. I said $_POST['custom_12'][2].

Don't use multiple if's. Use a loop.

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  //do this
}

Posted: Mon Aug 21, 2006 9:59 am
by train
jayshields wrote:
I still cant read $_POST[custom_12][2]
I'm not suprised. I said $_POST['custom_12'][2].

Don't use multiple if's. Use a loop.

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  //do this
}
The action will vary depending on which member of the array is true, so I dont think the loop will work well.

Thanks. I do appreciate it.

Posted: Mon Aug 21, 2006 10:09 am
by jayshields
You should still use a loop...

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  if($v == 1) {
    //do this
  }
}

Posted: Mon Aug 21, 2006 10:10 am
by train
jayshields wrote:You should still use a loop...

Code: Select all

foreach($_POST['custom_12'] as $k => $v) {
  if($v == 1) {
    //do this
  }
}
Will do. Thank you.