   <?php
date_default_timezone_set('America/Panama');

function logs($text)
{
    $hoynowf = getdate();
    
    $hora = str_pad($hoynowf["minutes"], 2, "0", STR_PAD_LEFT);
    
    $timeStampf = $hoynowf["year"] . "-" . $hoynowf["mon"] . "-" . $hoynowf["mday"] . " " . $hoynowf["hours"] . ":" . $hora . ":" . $hoynowf["seconds"];
    
    if (is_readable("logphp.txt")) {
        // echo "Tienes permiso de lectura!";
    } else {
        // echo "NO Tienes permiso de lectura!";
    }
    
    // chmod ("logphp.txt", 0777);
    
    // Entrada
    
    $fp = fopen("d:\logphp.txt", "a");
    fputs($fp, $timeStampf . " : " . $text . "\r\n");
    fclose($fp);
    
    // echo $salida;
}

// FECHA CREACION: JOHNN GUTIERREZ 2017-10-06
// ULTIMA MODIFICACION: JOHNN GUTIERREZ 2017-10-07
function gen_jsonmysql($data, $regs, $tab, $conn, $dbn, $swsavelog)
{
    if ($swsavelog == '1') {
        logs('--gen_jsonmysql--');
    }
    ;
    // echo 'son '.$regs."de la tabla".$tab."<br>";
    $ib = 0;
    
    while ($ib < $regs) {
        /*
         * el valor presentado sería
         * $i antes del incremento
         * (post-incremento)
         */
        
        // echo 'vuelta nro: '.$ib;
        
        $opfinal = $data[$ib];
        
        $str_fields = '';
        $str_values = '';
        $str_update = '';
        $i = 0;
        $valuekey = '';
        $fieldkey = '';
        foreach ($opfinal as $field => $value) {
            
            $fieldkey = $field;
            $valuekey = $value;
            // if($i<9999) {
            if ($i > 0) {
                $field = ',' . $field;
                $fieldkey = ',' . $fieldkey;
                
                if ($value == '') {
                    $value = $value . ',null';
                    $valuekey = 'null';
                } else {
                    $value = ',"' . $value . '"';
                    $valuekey = '"' . $valuekey . '"';
                }
                
                // $field=','.$field.$i;
                // $value=',"'.$value.$i.'"';
            } else {
                $value = '"' . $value . '"';
            }
            
            $str_update = $str_update . $fieldkey . '=' . $valuekey;
            $str_fields = $str_fields . $field;
            $str_values = $str_values . $value;
            $i = $i + 1;
            
            // }
        }
        
        // echo $str_update;
        
        $sql = "INSERT INTO " . $dbn . "." . $tab . " (" . $str_fields . ") VALUES (" . $str_values . ") ON DUPLICATE KEY UPDATE " . $str_update . "";
        
        if ($swsavelog == '1') {
            logs('SQL: ' . $sql);
        }
        ;
        
        $SQLupdate = $sql;
        
        if ($conn->query($SQLupdate) === TRUE) {
            
            $result = 'true';
        } else {
            
            $result = 'false';
        }
        
        $ib ++;
        
        // echo "inserta: ".$sql;
    }
    
    if ($swsavelog == '1') {
        logs('RESP: ' . $result);
    }
    ;
    
    return $result;
}

?>
