• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
going from json_decode to insert_batch

#1
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.


Attached Files
.pdf   localhost _ 127.0.0.1 _ substantiator _ activity_log _ phpMyAdmin 4.5.1.pdf (Size: 97.03 KB / Downloads: 19)
Reply

#2
Not sure if these will help but give them a try on arrays and objects.

PHP Code:
// -----------------------------------------------------------------------

/**
 * objectToArray ()
 */
if ( ! function_exists('objectToArray'))
{
    
/**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return array
     */
    
function objectToArray($data)
    {
        if (
is_object($data))
        {
            
/**
             * Gets the properties of the given object
             * with get_object_vars function
             */
            
$data get_object_vars($data);
        }

        
/**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        
return (is_array($data))
            ? 
array_map(__FUNCTION__$data)
            : 
$data;
    }
}

// -----------------------------------------------------------------------

/**
 * arrayToObject ()
 */
if ( ! function_exists('arrayToObject'))
{
    
/**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return object
     */
    
function arrayToObject($data)
    {
        
/**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        
return (is_array($data))
            ? (object) 
array_map(__FUNCTION__$data)
            : 
$data;
    }


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:
// -----------------------------------------------------------------------

/**
 * varDebug () - Add this method to a CodeIgniter Helper.
 * -----------------------------------------------------------------------
 *
 * Formatted output of var_dump() or print_r() see comment below etc;
 */
if ( ! function_exists('varDebug'))
{
    
/**
     * Debug Helper
     * -------------------------------------------------------------------
     * Outputs the given variable(s) with color formatting and location
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.$callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument)
        {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';

            
// You can also change this to print_r() if needed.
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";
        exit;
    }


You use it just like var_debug.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#3
Thanks. I am actually not sure how to try this. Place what in a Helper? And what helper? Sorry, I am clueless.
Reply

#4
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);

$object arrayToObject($your_array_data);

varDebug($array or $object);
exit(); 

Hope that helps.

Place this in ./application/helpers/utility_helper.php

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------
 * Editor   : PhpStorm
 * Date     : 4/16/2018
 * Time     : 1:07 PM
 * Authors  : Raymond L King Sr.
 * -----------------------------------------------------------------------
 *
 * Class        Name
 *
 * @project     ci3blog
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2018 Custom Software Designers, LLC.
 * @license     http://www.procoversfx.com/license
 * -----------------------------------------------------------------------
 */

// -----------------------------------------------------------------------

/**
 * objectToArray ()
 *
 * Usage:
 *
 * $array = objectToArray($your_object_data)
 */
if ( ! function_exists('objectToArray'))
{
    
/**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return array
     */
    
function objectToArray($data)
    {
        if (
is_object($data))
        {
            
/**
             * Gets the properties of the given object
             * with get_object_vars function
             */
            
$data get_object_vars($data);
        }

        
/**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        
return (is_array($data))
            ? 
array_map(__FUNCTION__$data)
            : 
$data;
    }
}

// -----------------------------------------------------------------------

/**
 * arrayToObject ()
 *
 * Usage:
 *
 * $object = arrayToObject($your_array_data);
 */
if ( ! function_exists('arrayToObject'))
{
    
/**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return object
     */
    
function arrayToObject($data)
    {
        
/**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        
return (is_array($data))
            ? (object) 
array_map(__FUNCTION__$data)
            : 
$data;
    }
}

// -----------------------------------------------------------------------

/**
 * varDebug () - Add this method to a CodeIgniter Helper.
 * -----------------------------------------------------------------------
 *
 * Formatted output of var_dump() or print_r() see comment below etc;
 *
 * Usage:
 *
 * varDebug($array or $object);
 * exit();
 *
 */
if ( ! function_exists('varDebug'))
{
    
/**
     * Debug Helper
     * -------------------------------------------------------------------
     * Outputs the given variable(s) with color formatting and location
     *
     * @param    mixed    - variables to be output
     */
    
function varDebug()
    {
        list(
$callee) = debug_backtrace();

        
$arguments func_get_args();

        
$total_arguments func_num_args();

        echo 
'<div><fieldset style="background: #fefefe !important; border:1px red solid; padding:15px">';
        echo 
'<legend style="background:lightgrey; padding:5px;">'.$callee['file'].' @line: '.$callee['line'].'</legend><pre><code>';

        
$i 0;
        foreach (
$arguments as $argument)
        {
            echo 
'<strong>Debug #'.++$i.' of '.$total_arguments.'</strong>: '.'<br>';

            
// You can also change this to print_r() if needed.
            
var_dump($argument);
        }

        echo 
"</code></pre></fieldset><div><br>";
        exit;
    }
}

/**
 * -----------------------------------------------------------------------
 * Filename: utility_helper.php
 * Location: ./application/helpers/utility_helper.php
 * -----------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#5
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.
Reply

#6
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 the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply

#7
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"
Reply

#8
You should be able to access them the index name.
What did you Try? What did you Get? What did you Expect?

Joined the CodeIgniter Community in 2009.          ( Skype: insitfx )
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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