Welcome Guest, Not a member yet? Register   Sign In
Inserting For each?
#11

[eluser]Dizza[/eluser]
[quote author="Glazz" date="1339442077"]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.[/quote]

Haha thanks! Im almost there now Tongue

My model looks like this now
Code:
function updateAanwezigheid($data)
{
  foreach ( $this->input->post('aanwezigheid') as $ID => $aanwezigheid )
  {
   $this->db->insert('aanwezigheid', $data);
  }
   echo "Added";
  return;  
}

And my controller
Code:
function insert(){
$this->is_logged_in();

if($this->input->post('submit')){
    $this->load->model('aanwezigheid_model');
    
     $ID = $this->input->post('ID');
   $lesuur   = $this->input->post('lesuur');
   $aanwezigheid   = $this->input->post('aanwezigheid');
    
     $this->aanwezigheid_model->updateAanwezigheid($data);
  }
  
  $this->load->view('aanwezigheidklas_view' );    
}

When i used the var_dump all data was there but now i get this error, that while im not even updating Tongue
Code:
You must use the "set" method to update an entry.

Filename: /Users/jimmytenbrink/Sites/afstuderen/models/aanwezigheid_model.php

Line Number: 9
#12

[eluser]Glazz[/eluser]
Well like i said, you need to loop the $aanwezigheid array and insert or update your desired records.


Code:
$this->aanwezigheid_model->updateAanwezigheid($data);
You are not passing any variable there " $data " is not declared anywhere.


My sugestion, in your controller you do the loop and call the model function inside the loop, something like:

Code:
foreach ( $aanwezigheid as $ID => $value )
{
    $data = array(
        'id' => $ID,
        'value' => $value
        .... paste here all the entries you want to insert or update in your database..
    );
    $this->aanwezigheid_model->updateAanwezigheid($data);
}

And in your model function you just need to do the db->insert thing..


Since i don't understand those functions names and those variables names either, i can't help you much with this.
#13

[eluser]Dizza[/eluser]
I fixed it! Thanks to you!

Lots of thanks, my model now looks like this, just for the people who have the samen problem in the future
Code:
function updateAanwezigheid()
{
  foreach ( $this->input->post('aanwezigheid') as $ID => $aanwezigheid )
  {
$data = array(
   'llID' => $ID,
   'lesuurID'   => $this->input->post('lesuur'),
   'aanwezig'   => $aanwezigheid);
  $this->db->insert('aanwezigheid', $data);
  }
   echo "Added";
  return;  
}


#14

[eluser]Glazz[/eluser]
No problem, you can make it "more cleaner"

Controller:
Code:
function insert()
{
$this->is_logged_in();

if ( $this->input->post('submit') )
{
  $this->load->model('aanwezigheid_model');
    
  //$ID = $this->input->post('ID'); // you dont need this, at least for now...
  $lesuur       = $this->input->post('lesuur');
  $aanwezigheid = $this->input->post('aanwezigheid');

  foreach ( $aanwezigheid as $llID => $aanwezig )
  {
   $data = array(
    'llID'     => $llID,
    'lesuurID' => $lesuur,
    'aanwezig' => $aanwezigheid
   );
   $this->aanwezigheid_model->updateAanwezigheid($data);
  }
}

$this->load->view('aanwezigheidklas_view' );    
}


Model:
Code:
function updateAanwezigheid()
{
$this->db->insert('aanwezigheid', $data);

echo "Added";

return true;  
}
#15

[eluser]Unknown[/eluser]
--
#16

[eluser]CroNiX[/eluser]
look into a batch insert. Inserting in a loop is very resource consuming compared to a single query.

Code:
foreach ( $aanwezigheid as $llID => $aanwezig )
{
   $data[] = array(
    'llID'     => $llID,
    'lesuurID' => $lesuur,
    'aanwezig' => $aanwezigheid
   );
}
$this->aanwezigheid_model->updateAanwezigheid($data);
Code:
aanwezigheid_model::updateAanwezigheid($data)
{
  return $this->db->insert_batch($data);
}
#17

[eluser]Dizza[/eluser]
CroNIX thank you! I will use that one! Way faster Tongue
Glazz thank you for helping me out this night!

Bye!




Theme © iAndrew 2016 - Forum software by © MyBB