Viewing file: Main_Model.inc.php (3.83 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<? class Main_Model { public static $DB; public static function Connect() { self::$DB = new PDO('mysql:host=localhost;dbname=zt-rada;charset=utf8', 'morozov', 'mav1024'); self::$DB->exec("SET NAMES utf8");
} public static function ArrayFilter($array, $fields) { $newArray = array(); foreach($fields as $value) $newArray[$value] = $array[$value]; return $newArray; } public static function ExecuteQuery($sql, $params = array()) { $sth = self::$DB->prepare($sql); if (!empty($params)) { if ($params === array_values($params)) { $sth->execute($params); } else { foreach ($params as $key => $value) { $sth->bindValue(':' . $key, $value); } $sth->execute(); } } else $sth->execute(); return $sth; } public static function GetAllRecords($table) { $res = self::ExecuteQuery("SELECT * FROM $table"); return $res->fetchAll(); } public static function GetAllRecordsToAssocArray($table, $uniqueField) { $res = self::ExecuteQuery("SELECT * FROM $table"); $rows = $res->fetchAll(); $result = array(); for($i = 0; $i < count($rows); $i++) $result[$rows[$i][$uniqueField]] = $rows[$i]; return $result; } public static function DateExplode($dateString) { $date_components = explode(" ", $dateString); $date = explode("-", $date_components[0]); $time = explode(":", $date_components[1]); return array($date[0], $date[1], $date[2], $time[0], $time[1], $time[2]); } public static function GetUsefulDate($dateString, $showdate = true, $showtime = true) { global $ULANG; $html = ""; $parts = self::DateExplode($dateString); $month = array('','січня', 'лютого', 'березня', 'квітня', 'травня', 'червня', 'липня', 'серпня', 'вересня', 'жовтня', 'листопада', 'грудня'); if ($showdate) { $html .= '<span class="compdate">'.(int)$parts[2]." ".$month[((int)$parts[1])]." ".$parts[0]." р.</span>"; }
if ($showtime) { $html .= ' <span class="comptime"> о'." ".$parts[3].":".$parts[4].'</span>'; } return $html; } public static function GetNowDate() { return $date = date('Y-m-d H:i:s'); } public static function GetNowYear() { return date('Y'); } public static function NormalizePercent($value) { if ($value >= 100) return 100; else return $value; } public static function Insert($sql, $params = array()) { $sth = self::$DB->prepare($sql); if (!empty($params)) { if ($params===array_values($params)) { $sth->execute($params); } else { foreach ($params as $key => $value) { $sth->bindValue(':' . $key, $value); } $sth->execute(); } }
} public static function GetRowById($table, $fieldName, $id) { $sql = "SELECT * FROM {$table} WHERE {$fieldName} = :id"; $sth = self::ExecuteQuery($sql, array('id' => $id)); return $sth->fetch(); } }
|