0

In on of my views i have the following form:

<table align="center" border="1">
<tr>
   <td>Identificação</td>
   <td>Conc, %</td>
   <td>Classificação 67/548/CEE</td>
   <td>Classificação 1272/2008 (CLP)</td>
</tr>
<tr>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][identificacao]"></textarea>
   </td>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][conc]"></textarea>
   </td>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][classificacao_cee]"></textarea>
   </td>
   <td>
       <textarea rows="4" cols="30" name="componentes[0][classificacao_clp]"></textarea>
   </td>
</tr>
</table>

<div id="outro_curso"></div>
<p class="submit">
    <button id="novo_curso">Add Curso</button>
</p>

As you can see, I'm using multidimensional arrays. I'm doing that, because when I click the "Add Curso" button I call a jquery function that generates another table like the previous one. The names of the text areas will be componentes[1][identificacao], componentes[1][conc], etc...

NOTE: I can use the button to generate tables the number of times I want.

Now, my problem is how to get this data treated in my CodeIgniter model. I tried saving the data to the $componentes array (to later insert into the database) But I guess there is something wrong with my code:

$componentes;
    foreach ($this->input->post('componentes') as $key => $value){
        $componentes[$key]['identificacao']=$value[$key]['identificacao'];
        $componentes[$key]['conc']=$value[$key]['conc'];
        $componentes[$key]['classificacao_cee']=$value[$key]['classificacao_cee'];
        $componentes[$key]['classificacao_clp']=$value[$key]['classificacao_clp'];
    }

Can someone give me a little help please?

EDIT:

I forgot to mention, I'm getting the error:

Invalid argument supplied for foreach().

So I don't know if my foreach ($this->input->post('componentes') as $key => $value){ is correct or if is some problem in the lines inside.

2
  • From a quick look, I'm guessing you'd need this: $componentes[$key]['identificacao'] = $value['identificacao']; Commented Jan 22, 2013 at 11:34
  • I edited my post Mudshark. Right now i can't test your advice because i'm getting an error on my foreach. Commented Jan 22, 2013 at 11:40

1 Answer 1

1
$componentes_post = $this->input->post('componentes');
if(is_array($componentes_post)&&!empty($componentes_post))
{
    foreach ($componentes_post as $key => $value)
    {
        $temp_componentes['identificacao']=$value['identificacao'];
        $temp_componentes['conc']=$value['conc'];
        $temp_componentes['classificacao_cee']=$value['classificacao_cee'];
        $temp_componentes['classificacao_clp']=$value['classificacao_clp'];
        $componentes[] = $temp_componentes;
    }
}

Now see,

print_r($componentes);

You should get what you need. Next, you are getting error Invalid argument supplied for foreach() since the array you have iterated on foreach is empty, make sure value is coming on that element by using print_r($this->input->post('componentes'))

Sign up to request clarification or add additional context in comments.

1 Comment

Couldn't ask for better. Thanks a lot hsuk, everything is working perfectly now.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.