Ejemplo n.º 1
0
 /**
  * Because POST can be JSON, or other formats in the future, we cannot simply
  * use $_REQUEST.
  *
  * Another difference with $_REQUEST is this also counts $_COOKIE.
  */
 public function param($name = null, $type = null)
 {
     /*! Note @ 23 Apr, 2015
      *  POST validation should be simple, just match it with some hash key stored in sessions.
      */
     // TODO: Do form validation, take reference from form key of Magento.
     $result = $this->_param($type);
     if (is_array($result)) {
         // remove meta keys and sensitive values
         $result = array_filter_keys($result, funcAnd(notIn([ini_get('session.name')]), compose('not', startsWith($this->metaPrefix))));
     }
     if ($name === null) {
         return $result;
     } else {
         $fx = prop($name);
         return $fx($result);
     }
 }
Ejemplo n.º 2
0
 /**
  * Upsert function.
  *
  * @param $table Target table name.
  * @param $data Key-value pairs of field names and values.
  *
  * @returns True on update succeed, insertId on a row inserted, false on failure.
  */
 public static function upsert($table, array $data, $update = null)
 {
     $fields = static::escapeField(array_keys($data), $table);
     $values = array_values($data);
     $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', static::escapeField($table), implode(', ', $fields), implode(', ', array_fill(0, count($fields), '?')));
     // append "ON DUPLICATE KEY UPDATE ..."
     $keys = static::getFields($table, 'PRI');
     $fields = array_intersect($fields, static::escapeField($keys, $table));
     if ($fields) {
         // selective update
         if ($update !== null) {
             $data = array_select($data, (array) $update);
         }
         foreach ($data as $field => $value) {
             $data["`{$field}` = ?"] = $value;
             unset($data[$field]);
         }
         // full dataset appended with non-key fields
         $values = array_merge($values, array_values(array_filter_keys($data, notIn($keys))));
         $query .= ' ON DUPLICATE KEY UPDATE ';
         if ($data) {
             $query .= implode(', ', array_keys($data));
         } else {
             // note: key1 = key1; We do not use INSERT IGNORE because it'll ignore other errors.
             $value = reset($fields);
             $query .= "{$value} = {$value}";
             unset($value);
         }
     }
     unset($keys, $fields);
     $res = static::query($query, $values);
     unset($query, $values);
     if ($res !== false) {
         $res->closeCursor();
         // Inserted, return the new ID.
         if ($res->rowCount() == 1) {
             // Note: mysql_insert_id() doesn't do UNSIGNED ZEROFILL!
             $res = (int) static::getConnection()->lastInsertId();
             //$res = static::fetchField("SELECT MAX(ID) FROM `$table`;");
         } else {
             $res = true;
         }
     }
     return $res;
 }
Ejemplo n.º 3
0
        if ($index[0] == $myValue[0] && $index[1] == $myValue[1]) {
            return 0;
        }
    }
    return 1;
}
$myClassesTest = array();
if ($result = $mysqli->query("SELECT class FROM classes")) {
    while ($obj = $result->fetch_object()) {
        $thisClass = $obj->class;
        $newQuery = "SELECT class, professor FROM notes WHERE class='" . $thisClass . "'";
        if ($newResult = $mysqli->query($newQuery)) {
            while ($newObj = $newResult->fetch_object()) {
                $thisProf = $newObj->professor;
                $newArray = array($thisClass, $thisProf);
                if (notIn($myClassesTest, $newArray)) {
                    array_push($myClassesTest, $newArray);
                }
            }
        }
    }
}
$myClasses = array();
if ($result = $mysqli->query("SELECT class FROM classes")) {
    while ($obj = $result->fetch_object()) {
        array_push($myClasses, $obj->class);
    }
}
$mysqli->close();
?>