Welcome Guest, Not a member yet? Register   Sign In
looping through form fields
#1

[eluser]jplanet[/eluser]
I have a VERY bizarre probem. When I use code to loop through form fields using the $_POST array in a foreach block, I get strange errors in other code later on the page. I am chalking this up to a bug in PHP5.

From what I can find, Codeigniter's input class only provides acces sto individual post variables using $this->input->post('fieldname'). Is there a way I can access the array of post variables using a Codeigniter function?

For those who are curious, this is the code that breaks my application:

Code:
foreach($_POST as $key => $data) {
        if(strstr($key,"wrap_")){
        $cart_id = stristr($key,"_");
        $cart_id = str_replace("_","",$cart_id);
        $this->cart_model->addWrap($cart_id);
        }
        }

Once I insert this code, I get this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: controllers/checkout.php

Line Number: 290

Line 290 refers to this very simple line of code, which does not generate an error when I delete the foreach statement above:

Code:
$data['cart'] = $this->cart_model->getCart($this->session->userdata('session_id'));

Even if I replace the body of the foreach block with a simple echo statement, it produces the same error. As long as I loop through POST variables using foreach, I get the error.
#2

[eluser]Seppo[/eluser]
Code:
foreach($_POST as $key => $data) { // <--- watch the $data here!
You are assigning to $data each value of the $_POST array, so after the loop it contains a string =)
#3

[eluser]jplanet[/eluser]
[quote author="Seppo" date="1202530828"]
Code:
foreach($_POST as $key => $data) { // <--- watch the $data here!
You are assigning to $data each value of the $_POST array, so after the loop it contains a string =)[/quote]

WOW! I didn't notice that $data in that context is conflicting with the $data array used in Codeigniter! That is genius!

Thank you, Seppo! I thought I was going insane!




Theme © iAndrew 2016 - Forum software by © MyBB