• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert array into database

#11
[eluser]Randy Casburn[/eluser]
Maybe I've misunderstood your intent with this:

Code:
$sql = array(
                   $this->reminder_id = $reminder_id,
                   $this->quantity = $_POST['quantity'][$i][1],
                   $this->bulb_type_id = $_POST['bulb_type_id'][$i][2],
                   //$this->shatterproof = $_POST['shatterproof'][$i][3]
                    );

When I look back at this, it looks like your intent really is to say I want $reminder_id to be assigned to the Class variable named reminder_id and I want the POST'd value from array index 'quanity'[$i][1] to be assigned to the Class variable quantity etc.

So I led you astray before. If that is your intent. But if that is true, you are not ready to pass that to the db->insert method yet. First, you must build the array properly. And that is your current problem.

So do this:

1) change the => operators back to = operators
2) change $sql array stuff to this:

Code:
$this->reminder_id = $reminder_id,
     $this->quantity = $_POST['quantity'][$i][1],
     $this->bulb_type_id = $_POST['bulb_type_id'][$i][2],
     $this->shatterproof = $_POST['shatterproof'][$i][3]

3) now set up the array for the insert function:

Code:
$data = array(
         "REMINDER COLUMN NAME HERE" => $this->reminder_id,
         "QUANTITY COLUMN NAME HERE" => $this->quantity,
         "BULB_TYPE COLUMN NAME HERE" => $this->bulb_type_id,
         "SHATTERPROOF COLUMN NAME HERE" => $this->shatterproof,
                    );

4) change your insert statement to say: $this->db->insert('line_items', $data);

============

Let's try this. let me know what your questions are .

Randy

#12
[eluser]bohara[/eluser]
Ah, Now that makes sense. I forgot to tell insert what columns the data belongs into. I will try that.

Beau

#13
[eluser]bohara[/eluser]
Thanks a ton. Every thing is working. I now have some security and tweaking of the true/false. Also, I think I only need the part outlined in step three.

Code:
$data = array(
         "REMINDER COLUMN NAME HERE" => $this->reminder_id,
         "QUANTITY COLUMN NAME HERE" => $this->quantity,
         "BULB_TYPE COLUMN NAME HERE" => $this->bulb_type_id,
         "SHATTERPROOF COLUMN NAME HERE" => $this->shatterproof,
                    );

The stuff I was doing before becomes irrelevant I think?

Beau

#14
[eluser]Randy Casburn[/eluser]
Pretty much.

#15
[eluser]newsun[/eluser]
Hi, not sure if you figured a way around this, but you were not passing a multi dimensional array, just a bunch of post fields which looked like one in name.

Code:
$_POST['bulb_type_id[0][2]']
for example was one of your variables you passed

You could iterate through them doing something like this:

Code:
for ($i=0;$i<$_POST['a_number_you_pass'];$i++) {
            
    $sql = array(
    reminder_id = $reminder_id,
    quantity = $_POST["quantity[$i][1]"],
    bulb_type_id = $_POST["bulb_type_id[$i][2]"],
    shatterproof = $_POST["shatterproof[$i][3]"],
    );
    $this->db->insert('line_items', $sql);    
}

I think this is more in the lines of what you re trying to do here, where you need to pass in the number of rows you have total from your ajax via a post variable which I called 'a_number_you_pass' I included the insert statement in the loop as I don't know if CI can take a multi-dimentional array and run multiple inserts like that, if so you can move it out and change $sql to $sql[] or $sql[$i]

Good luck.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.