CodeIgniter Forums
code formatting help - 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: code formatting help (/thread-51472.html)



code formatting help - El Forum - 05-05-2012

[eluser]Samus[/eluser]
Hi guys I need help formatting this code to make use of insert_batch().

My code looks abit like this:

Code:
if ($handle) :
            while (($buffer = fgets($handle, 1024)) !== false) :
                $column = explode("|", $buffer);
                            $final = array(
                                'send_NID' => xss_clean($column[0]),
                                'send_ruler' => xss_clean($column[1]),
                                'send_nation' => xss_clean($column[2]),
                                'send_alliance' => xss_clean($column[3]),
                                'rec_NID' => xss_clean($column[5]),
                                'rec_ruler' => xss_clean($column[6]),
                                'rec_nation' => xss_clean($column[7]),
                                'rec_alliance' => xss_clean($column[8]),
                                'status' => xss_clean($column[10]),
                                'money' => xss_clean($column[11]),
                                'tech' => xss_clean($column[12]),
                                'soldiers' => xss_clean($column[13]),
                                'date_sent' => xss_clean($column[14]),
                                'id' => xss_clean($column[16])
                            );
                            $this->iron->insert('aid', $final);
            endwhile;
endif;

As you can see it is using insert() within the while loop to insert each line, which produces a sql query for each line, whereas I could probably do it all in one statement.

Could someone suggest how I would re arrange the array to incorporate the use of insert_batch()?

I have a feeling insert_batch would be appropriate outside the while loop, just not sure how to format the array?


code formatting help - El Forum - 05-05-2012

[eluser]CroNiX[/eluser]
It just needs to be an array of (insert) arrays. Right now you have a single (insert) array, $final. Change $final = array() to $final[] = array() and then outside of the while loop run the batch insert using $final.


code formatting help - El Forum - 05-05-2012

[eluser]Samus[/eluser]
[quote author="CroNiX" date="1336232031"]It just needs to be an array of (insert) arrays. Right now you have a single (insert) array, $final. Change $final = array() to $final[] = array() and then outside of the while loop run the batch insert using $final.[/quote]
Thanks mate!

All good. Smile