Ejemplo n.º 1
0
$data = array('users' => array(array('login' => 'user1', 'customerId' => 10, 'firstName' => 'John', 'lastName' => 'Doe', 'password' => $db->func('SHA1(?)', array("secretpassword+salt")), 'createdAt' => $db->now(), 'updatedAt' => $db->now(), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc()), array('login' => 'user2', 'customerId' => 10, 'firstName' => 'Mike', 'lastName' => NULL, 'password' => $db->func('SHA1(?)', array("secretpassword2+salt")), 'createdAt' => $db->now(), 'updatedAt' => $db->now(), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(2)), array('login' => 'user3', 'active' => true, 'customerId' => 11, 'firstName' => 'Pete', 'lastName' => 'D', 'password' => $db->func('SHA1(?)', array("secretpassword2+salt")), 'createdAt' => $db->now(), 'updatedAt' => $db->now(), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(3))), 'products' => array(array('customerId' => 1, 'userId' => 1, 'productName' => 'product1'), array('customerId' => 1, 'userId' => 1, 'productName' => 'product2'), array('customerId' => 1, 'userId' => 1, 'productName' => 'product3'), array('customerId' => 1, 'userId' => 2, 'productName' => 'product4'), array('customerId' => 1, 'userId' => 2, 'productName' => 'product5')));
function createTable($name, $data)
{
    global $db;
    //$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
    $db->rawQuery("DROP TABLE IF EXISTS {$name}");
    $q = "CREATE TABLE {$name} (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT";
    foreach ($data as $k => $v) {
        $q .= ", {$k} {$v}";
    }
    $q .= ")";
    $db->rawQuery($q);
}
// rawQuery test
foreach ($tables as $name => $fields) {
    $db->rawQuery("DROP TABLE " . $prefix . $name);
    createTable($prefix . $name, $fields);
}
if (!$db->ping()) {
    echo "db is not up";
    exit;
}
// insert test with autoincrement
foreach ($data as $name => $datas) {
    foreach ($datas as $d) {
        $id = $db->insert($name, $d);
        if ($id) {
            $d['id'] = $id;
        } else {
            echo "failed to insert: " . $db->getLastQuery() . "\n" . $db->getLastError();
            exit;
Ejemplo n.º 2
0
$tables = array('users' => array('login' => 'char(10) not null', 'active' => 'bool default 0', 'customerId' => 'int(10) not null', 'firstName' => 'char(10) not null', 'lastName' => 'char(10)', 'password' => 'text not null', 'createdAt' => 'datetime', 'updatedAt' => 'datetime', 'expires' => 'datetime', 'loginCount' => 'int(10) default 0'), 'products' => array('customerId' => 'int(10) not null', 'userId' => 'int(10) not null', 'productName' => 'char(50)'));
$data = array('user' => array(array('login' => 'user1', 'customerId' => 10, 'firstName' => 'John', 'lastName' => 'Doe', 'password' => $db->func('SHA1(?)', array("secretpassword+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc()), array('login' => 'user2', 'customerId' => 10, 'firstName' => 'Mike', 'lastName' => NULL, 'password' => $db->func('SHA1(?)', array("secretpassword2+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(2)), array('login' => 'user3', 'active' => true, 'customerId' => 11, 'firstName' => 'Pete', 'lastName' => 'D', 'password' => $db->func('SHA1(?)', array("secretpassword2+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(3))), 'product' => array(array('customerId' => 1, 'userId' => 1, 'productName' => 'product1'), array('customerId' => 1, 'userId' => 1, 'productName' => 'product2'), array('customerId' => 1, 'userId' => 1, 'productName' => 'product3'), array('customerId' => 1, 'userId' => 2, 'productName' => 'product4'), array('customerId' => 1, 'userId' => 2, 'productName' => 'product5')));
function createTable($name, $data)
{
    global $db;
    //$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
    $q = "CREATE TABLE {$name} (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT";
    foreach ($data as $k => $v) {
        $q .= ", {$k} {$v}";
    }
    $q .= ")";
    $db->rawQuery($q);
}
// rawQuery test
foreach ($tables as $name => $fields) {
    $db->rawQuery("DROP TABLE " . $name);
    createTable($name, $fields);
}
foreach ($data as $name => $datas) {
    foreach ($data[$name] as $userData) {
        $obj = new $name($userData);
        $id = $obj->save();
        if ($obj->errors) {
            echo "errors:";
            print_r($obj->errors);
            exit;
        }
    }
}
$products = product::ArrayBuilder()->get(2);
foreach ($products as $p) {
Ejemplo n.º 3
0
 }
 $cols = array("*", "( 6371 * acos( cos( radians(" . $lat . ") ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(" . $lon . ") ) + sin( radians(" . $lat . ") ) * sin(radians(lat)) ) ) AS distance");
 $db->where("( 6371 * acos( cos( radians(" . $lat . ") ) * cos( radians( lat ) ) * cos( radians( lon ) - radians(" . $lon . ") ) + sin( radians(" . $lat . ") ) * sin(radians(lat)) ) )", $rad, "<");
 $db->orderBy("distance", "asc");
 $pictures = $db->get("picture", null, $cols);
 $_output = array();
 foreach ($pictures as $picture) {
     $tmp = array();
     //$tmp['url2'] = 'https://htv.utfapp.com/index/index.php/thumb?path='.base64_encode($picture['path']);
     $tmp['url'] = 'https://htv.utfapp.com/index/index.php/thumb2?path=/&id=' . $picture['idPicture'];
     $tmp['distance'] = $picture['distance'];
     $tmp['address'] = $picture['address'];
     $tmp['city'] = $picture['city'];
     $tmp['country'] = $picture['country'];
     $tmp['date'] = $picture['date'];
     $tags = $db->rawQuery("SELECT tag, api FROM gallery WHERE idPicture = " . $picture['idPicture'] . " ORDER BY probs DESC");
     $t = array();
     foreach ($tags as $tag) {
         if (!isset($t[$tag['api']])) {
             $t[$tag['api']] = array();
         }
         array_push($t[$tag['api']], $tag['tag']);
     }
     $tmp['tags'] = $t['clarifai'];
     if (isset($t['imagga'])) {
         $tmp['tagsImagga'] = $t['imagga'];
     }
     $year = date("Y", strtotime($tmp['date']));
     if (!isset($_output[$year])) {
         $_output[$year] = array();
     }