• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
logging a structure

#1
I have an array of two structures that have "arrived" from a remote application. Looking at one of the two structures I have:

$data = array(
   'task1' => $Arr[0].Task_0,
   'task1_percent' => $Arr[0].Perc_time_0,
   'task2' => $Arr[0].Task_1,
   'task2_percent' => $Arr[0].Perc_time_1,
   'task3' => $Arr[0].Task_2,
   'task3_percent' => $Arr[0].Perc_time_2,
   'task4' => $Arr[0].Task_3,
   'task4_percent' => $Arr[0].Perc_time_3,
   'task5' => $Arr[0].Task_4,
   'task5_percent' => $Arr[0].Perc_time_4,
   'task6' => $Arr[0].Task_5,
   'task6_percent' => $Arr[0].Perc_time_5,
   );



This is not working correctly. When I look at $data I see that it task1 has the string "Task_0" instead of the value of Task_0. I am looking to log all the values into a table. My table has the structure attached. What is the easy way to do this? I realize that once I get it into the array I need to use $this->db->insert('activity_log',$data);


Attached Files Thumbnail(s)
   
proof that an old dog can learn new tricks
Reply

#2
Looks like syntax error, in PHP you'd use -> instead of . to access object attributes.

PHP Code:
$Arr[0]->Task_0
// $Arr[0].Task_0 
Reply

#3
changed it to:
$data = array(
'task1' => $Arr[0].Task_0,
'task1_percent' => $Arr[0]->Perc_time_0,
'task2' => $Arr[0].Task_1,
'task2_percent' => $Arr[0]->Perc_time_1,
'task3' => $Arr[0].Task_2,
'task3_percent' => $Arr[0]->Perc_time_2,
'task4' => $Arr[0].Task_3,
'task4_percent' => $Arr[0]->.Perc_time_3,
'task5' => $Arr[0].Task_4,
'task5_percent' => $Arr[0]->Perc_time_4,
'task6' => $Arr[0].Task_5,
'task6_percent' => $Arr[0]->.Perc_time_5,
);

This causes the application to not even load.
proof that an old dog can learn new tricks
Reply

#4
So I am trying to get data that came over from another app into my CI based program so I can log it to the database.
$json has two structures. The first ($json[0]) is the email address of this user. I can get that one fine. $json[1] is an array of two structures.
$Arr=(array)$json[1];
$Arr=array_values($Arr); //this is an array of two structures. The first is from the tasks array and the 2nd is from the Bus comp array.
Attached is the $Arr structure. As you can see, it is an array of two different sized structures. $Arr[0] is the tasks structure and $Arr[1] is the BusComps structure. My database tables have slightly different structures since I include a few other fields such as a timestamp. So how do I get these two structures into a form that I can db->insert() into one of those two tables?

Previously I had the data coming in from the other app in a string and I just used picked off the fields I needed. On this one, the data from the other app is coming in as a structure (actually two structures) but I can't seem to get the values. So foe example, $Arr[0].Task_0 should be the value of the Task_0 element of the structure in array[0]. It should be the string "{choose}". This is exactly what it is when I look at it with the debugger. But when I run $task0=$Arr[0].Task_0; $task0 is assigned "Task_0". How do I get the value of the element instead of the index name?
proof that an old dog can learn new tricks
Reply

#5
Ah, $Arr[0] is an array?

I, for some stupid reason, assumed it was an object.

In that case $Arr[0]['Task_0'] should work, but probably best if you check what format and structure your array is with var_dump function.

Also noticed you have few extra . in the code right after -> that's probably why the code doesn't run. And you would need to use same method for all elements, not just for percent.
Reply

#6
You were right and thanks for your help. This was what finally worked.

$data = array(
'survey_compiler' => $email_key,
'task1' => $Arr[0]->Task_0,
'task1_percent' => $Arr[0]->Perc_time_0,
'task2' => $Arr[0]->Task_1,
'task2_percent' => $Arr[0]->Perc_time_1,
'task3' => $Arr[0]->Task_2,
'task3_percent' => $Arr[0]->Perc_time_2,
'task4' => $Arr[0]->Task_3,
'task4_percent' => $Arr[0]->Perc_time_3,
'task5' => $Arr[0]->Task_4,
'task5_percent' => $Arr[0]->Perc_time_4,
'task6' => $Arr[0]->Task_5,
'task6_percent' => $Arr[0]->Perc_time_5
);

$data2 = array(
'BC1' => $Arr[1]->BusComp_0,
'BC1_percent' => $Arr[1]->Perc_time2_0,
'BC2' => $Arr[1]->BusComp_1,
'BC2_percent' => $Arr[1]->Perc_time2_1,
'BC3' => $Arr[1]->BusComp_2,
'BC3_percent' => $Arr[1]->Perc_time2_2,
'BC4' => $Arr[1]->BusComp_3,
'BC4_percent' => $Arr[1]->Perc_time2_3,
'BC5' => $Arr[1]->BusComp_4,
'BC5_percent' => $Arr[1]->Perc_time2_4,
'BC6' => $Arr[1]->BusComp_5,
'BC6_percent' => $Arr[1]->Perc_time2_5
);
$this->db->insert('survey_tasks', $data);
$this->db->insert('survey_bc', $data2);
proof that an old dog can learn new tricks
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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