going from json_decode to insert_batch |
I am getting back a buffer from a chrome extension via xhr. I take the buffer and use json_decode. Here is an example of the buffer called $json after json_decode:
[{"email":"[email protected]"},{"Date_0":"","BusinessC_0":"choose","Project_0":"choose","Activity_0":"choose","RecordId_0":"0","Date_1":"","BusinessC_1":"choose","Project_1":"choose","Activity_1":"choose","RecordId_1":"0","Date_2":"","BusinessC_2":"choose","Project_2":"choose","Activity_2":"choose","RecordId_2":"0","Date_3":"","BusinessC_3":"choose","Project_3":"choose","Activity_3":"choose","RecordId_3":"0","Date_4":"","BusinessC_4":"choose","Project_4":"choose","Activity_4":"choose","RecordId_4":"0","Date_5":"","BusinessC_5":"choose","Project_5":"choose","Activity_5":"choose","RecordId_5":"0","Date_6":"","BusinessC_6":"choose","Project_6":"choose","Activity_6":"choose","RecordId_6":"0","Date_7":"","BusinessC_7":"choose","Project_7":"choose","Activity_7":"choose","RecordId_7":"0","Date_8":"","BusinessC_8":"choose","Project_8":"choose","Activity_8":"choose","RecordId_8":"0","Date_9":"","BusinessC_9":"choose","Project_9":"choose","Activity_9":"choose","RecordId_9":"0","Date_10":"","BusinessC_10":"choose","Project_10":"choose","Activity_10":"choose","RecordId_10":"0","Date_11":"","BusinessC_11":"choose","Project_11":"choose","Activity_11":"choose","RecordId_11":"0","Date_12":"","BusinessC_12":"choose","Project_12":"choose","Activity_12":"choose","RecordId_12":"0","Date_13":"","BusinessC_13":"choose","Project_13":"choose","Activity_13":"choose","RecordId_13":"0","_RowCount":14}] The first array element is the email address of the user that I am using as the key, and I can get that fine. The problem is that I want to log all this information to a table with $this->db->insert_batch('activity_log', $json,14); I actually want to append 14 separate records to the table, all with that same email address. But as you can see, the second element of the array is an object, that I can't really break up. I though about using a for loop but I don't think that will work. I have attached the structure of the table where I am trying to log this info.
proof that an old dog can learn new tricks
Not sure if these will help but give them a try on arrays and objects.
PHP Code: // ----------------------------------------------------------------------- Just place those in a CodeIgniter helper. They will also handle multi-objects and multi-arrays And you can add this for nice output of dumps. PHP Code: // ----------------------------------------------------------------------- You use it just like var_debug. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Just create your own helper under ./application/helpers
EXAMPLE: uitlity_helper.php Add all of the above into that file. To use: PHP Code: $array = objectToArray($your_object_data); Hope that helps. Place this in ./application/helpers/utility_helper.php PHP Code: <?php What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks. I just tried casting:
$Arr=(array)$json[1]; This seems to make a long 1-d array of the values. Then to decant it I use for ($i = 0;$i <= 13;$i++) { $date=$Arr[$i*5+0]; $busc=$Arr[$i*5+1]; $proj=$Arr[$i*5+2]; $act=$Arr[$i*5+3]; $email=$email_key; $logdata[0]=$date; $logdata[1]=$busc; $logdata[2]=$proj; $logdata[3]=$act; $logdata[4]=$email; //here is where I will // this->db->insert('activity_log',$logdata); } The strange thing is busc which should be "choose" seems to be returning null. I am using that same buffer as above. Unless there is some more direct way to get this data into table? seems like it will be slow.
proof that an old dog can learn new tricks
If your displaying a dropdown then choose would be null because it is the 0 index.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Yes, $Arr[0]=null which is correct. But $Arr[1]
(which in my C way of thinking) is analagous to $Arr['BusinessC_0'] should be the word "choose". Can I access those array indexes either numberically or by the index name? I don't get why $Arr[1]=null? $Arr = {array} [71] Date_0 = "" BusinessC_0 = "choose" Project_0 = "choose" Activity_0 = "choose" RecordId_0 = "0" Date_1 = "" BusinessC_1 = "choose" Project_1 = "choose" Activity_1 = "choose" RecordId_1 = "0" Date_2 = "" BusinessC_2 = "choose"
proof that an old dog can learn new tricks
You should be able to access them the index name.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |