CodeIgniter Forums
Inserting For each? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Inserting For each? (/thread-52437.html)

Pages: 1 2


Inserting For each? - El Forum - 06-11-2012

[eluser]Dizza[/eluser]
Hey there,

Is there a way to submit a form more times then one?
The problem i am having is i have multiple students and each student can be "aanwezig" or "niet aanwezig".

Now if i submit the form with the students it will only submit the first student and not the rest.

Is there a way to keep submitting until i had al the students inserted?

Here a screenshot, maybe it will make more sense!
http://cl.ly/2f3U1u1L2D033q3j2M1v

Hope someone can help me!


Inserting For each? - El Forum - 06-11-2012

[eluser]Dizza[/eluser]
updated the post with the new problem.


Inserting For each? - El Forum - 06-11-2012

[eluser]Glazz[/eluser]
You can name your form selects like
Code:
student[student_id_1]
student[student_id_2]
student[student_id_3]
...

In your foreach loop or while loop you change the "student_id_1" to that student id.

Hope i make it clear for you, if not, post your view here and i change it for you.


Inserting For each? - El Forum - 06-11-2012

[eluser]Dizza[/eluser]
Here is my view! Sorry for the mess, usually i clean it up after i fixed the entire function Tongue

The leerling = student!

Code:
<table id="aanwezigheid">
  &lt;?php echo form_open('aanwezigheid/update/');

$lesuur = array(
                  '1'   =>  '1',
                  '2'   =>  '2',
                  '3'   =>  '3',
                  '4'   =>  '4',
                  '5'   =>  '5',
                  '6'   =>  '6',                                
                  '7'   =>  '7',                                                  
                  '8'   =>  '8',                                                                    
                );
                
                echo form_dropdown('lesuur', $lesuur, '');
                ?&gt;
  <thead>
    <tr>
      <th>Naam</th>
      <th>Leerlingnummer</th>
      <th>Absent</th>
    </tr>
  </thead>
  <tbody>
    &lt;?php foreach ($leerling as $leerlingen) {
$options = array(
                  'Aanwezig'  =>   'Aanwezig',
                  'Geoorloofd afwezig'    => 'Geoorloofd afwezig',
                  'Ongeoorloofd afwezig'   =>  'Ongeoorloofd afwezig',
                  'Te laat' =>  'Te laat',
                );?&gt;
    
  <tr><td>&lt;?php echo form_hidden('ID', $leerlingen['ID']);
                echo $leerlingen['voornaam'];                            
                echo '&nbsp;';
       echo $leerlingen['tussenvoegsel'];  
       echo '&nbsp;';
       echo $leerlingen['achternaam']; ?&gt;</td>
      <td>&lt;?php echo $leerlingen['leerlingnummer']; ?&gt;</td>
      <td>&lt;?php echo form_dropdown('aanwezigheid', $options, ''); ?&gt;</td>
    </tr>
    &lt;?php } ?&gt;
    <tr>
  </tbody>
  &lt;?php echo form_submit('submit', 'Submit Post!'); ?&gt; &lt;?php echo form_close(); ?&gt;
</table>


Thanks!


Inserting For each? - El Forum - 06-11-2012

[eluser]Glazz[/eluser]
I think this should work
Code:
&lt;?php echo form_dropdown('aanwezigheid[' . $leerlingen['ID'] . ']', $options, ''); ?&gt;



Inserting For each? - El Forum - 06-11-2012

[eluser]Dizza[/eluser]
Ooow sorry, i dont think i explained myself well Tongue BUT THANKS Tongue

I meant that for each student i need to insert a query. The problem that i have is when i submit my form it will only update
the first student in my form and not the other students which i get from my foreach loop in my view.

So i wanted to know if there is a way to submit the form multiple times so each student is being update.

If i submit my form now it will only submit the first one like this
Code:
INSERT INTO `aanwezigheid` (`llID`, `lesuurID`, `aanwezig`) VALUES ('8', '2', 'niet aanwezig' )
but it should submit also the id of the other students with the aanwezig status Tongue

OR is it possible to use the form_dropdown you gave and submit it for each student?

BTW i will use your dropdown anyways because its way cleaner Tongue thanks!


Inserting For each? - El Forum - 06-11-2012

[eluser]Glazz[/eluser]
You submit the form only once of course, then with the data you get from $_POST you do a foreach.

Something like
Code:
foreach ( $this->input->post('aanwezigheid') as $studentID => $value )
{
  the $value is the value you selected on the dropdown for this student..
}

Don't know if it works properly, but i use it this way when i have tabular data and multiple dropdowns or inputs with the same name.


Inserting For each? - El Forum - 06-11-2012

[eluser]Dizza[/eluser]
Thanks im going to try it!

If i have any questions i will probably post again haha Tongue ThANKS!!


Inserting For each? - El Forum - 06-11-2012

[eluser]Dizza[/eluser]
[quote author="Glazz" date="1339437835"]You submit the form only once of course, then with the data you get from $_POST you do a foreach.

Something like
Code:
foreach ( $this->input->post('aanwezigheid') as $studentID => $value )
{
  the $value is the value you selected on the dropdown for this student..
}

Don't know if it works properly, but i use it this way when i have tabular data and multiple dropdowns or inputs with the same name.[/quote]
I have a question, probably an easy answer for you, but the how do i get the $value?
If i use the &lt;?php echo form_dropdown('aanwezigheid[' . $leerlingen['ID'] . ']', $options, ''); ?&gt;
i get an empty array.

And what do i post inside the for each loop?


Inserting For each? - El Forum - 06-11-2012

[eluser]Glazz[/eluser]
After you submit your form, try using this code to see what you are exactly getting from the form:
Code:
var_dump( $this->input->post() );
or
Code:
var_dump( $this->input->post('aanwezigheid') );

Inside the foreach, you place your mysql query, but you need change the values of course, so it will be automatically.