<?php
//http://stackoverflow.com/questions/18382740/cors-not-working-php
if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

include 'conn.php';
//Select data from database

//$data = urldecode($_GET['database']);

$getData = 'select productos.pr_id,productos.pr_descripcion,round(productos.pr_venta1,2) as pr_venta1 from ' . $_GET['database'] . '.productos

left outer join ' . $_GET['database'] . '.productos_det on productos.pr_id=productos_det.pr_id where productos_det.pr_activo=1';

$qur = $connection->query($getData);
$jsonData = array();
$jsonTempData = array();

while ($r = mysqli_fetch_assoc($qur)) {

    $jsonTempData = array();
    $jsonTempData['id'] = $r['pr_id'];
    $jsonTempData['name'] = utf8_encode($r['pr_descripcion']);
    $jsonTempData['lastText'] = $r['pr_venta1'];
    $jsonTempData['face'] = 'http://54.244.208.51/rctech-cloud/public/usuarios/1031_inversionesac3/images/productos/pr_' . $r['pr_id'] . '.jpg';
    $jsonTempData['cantidad'] = '0';
    $jsonData[] = $jsonTempData;

}
;

/*
for($i=0;$i<4; $i++)
{
$jsonTempData = array();
$jsonTempData['id']         = $i;
$jsonTempData['name']       = "juan".$data.$i;
$jsonTempData['lastText']   = $data.$i;
$jsonTempData['face']   =   'http://54.244.208.51/rctech-cloud/api/v1/images/86';

$jsonData[] = $jsonTempData;
}*/

$json = $jsonData;

header('content-type: application/json');
print_r(json_encode($json));

$qur=null;
@mysqli_close($connection);
?>