/** * entry point */ function init() { try { $statuses = $_POST["statuses"]; $sort = ""; $filterArr = array(); $filter = ""; $paging = ""; $pagingStatus = null; $query = ""; $html = ""; //error_log(urldecode($_POST["statuses"])); if (isset($statuses)) { //statuses => array $statuses = json_decode(urldecode($statuses)); foreach ($statuses as $key => $value) { switch ($value->action) { case "paging": $pagingStatus = $value; break; case "filter": $filter .= getFilterQuery($value, $filter); break; case "sort": $sort = getSortQuery($value); break; } } //connect to SQLite database $db = new PDO("sqlite:jplist.db"); //increases sqlite performance by turning syncing off $db->exec("pragma synchronous = off;"); //count database items for pagination $query = "SELECT count(*) FROM Item " . $filter . " " . $sort; $count = $db->query($query)->fetchColumn(); //init pagination query if ($pagingStatus) { $paging = getPagingQuery($pagingStatus, $count); } //init query with sort and filter $query = "SELECT title, desc, img, likes, keyword1, keyword2 FROM Item " . $filter . " " . $sort . " " . $paging; //select items $items = $db->query($query); foreach ($items as $item) { $html .= getHTML($item); } //init array for json data //$json_arr = array("html" => utf8_encode($html), "data" => array("count" => $count), "query"=> $query); //print html echo getHTMLWrapper($html, $count); //close the database connection $db = NULL; } } catch (PDOException $ex) { print 'Exception: ' . $ex->getMessage(); } }
/** * entry point */ function init() { try { //connect to database $db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD); $preparedParams = array(); $statuses = $_POST["statuses"]; $json = ""; $pagingStatus = null; $filter = ""; $sort = ""; $query = ""; $counter = 0; if (isset($statuses)) { //statuses => array $statuses = json_decode(urldecode($statuses)); foreach ($statuses as $key => $value) { switch ($value->action) { case "paging": $pagingStatus = $value; break; case "filter": $filter .= getFilterQuery($value, $filter, $preparedParams); break; case "sort": $sort = getSortQuery($value, $preparedParams); break; } } } //count database items for pagination $query = "SELECT count(*) FROM Item " . $filter . " " . $sort; if (count($preparedParams) > 0) { $stmt = $db->prepare($query); //error_log(print_r($preparedParams, true)); $stmt->execute($preparedParams); $count = $stmt->fetchColumn(); } else { $count = $db->query($query)->fetchColumn(); } //init pagination query if ($pagingStatus) { $paging = getPagingQuery($pagingStatus, $count, $preparedParams); } //init query with sort and filter $query = "SELECT title, description, image, likes, keyword1, keyword2 FROM Item " . $filter . " " . $sort . " " . $paging; if (count($preparedParams) > 0) { $stmt = $db->prepare($query); $stmt->execute($preparedParams); $items = $stmt->fetchAll(); } else { $items = $db->query($query); } $json .= "["; foreach ($items as $item) { if ($counter > 0) { $json .= ","; } $json .= json_encode($item); $counter++; } $json .= "]"; //print json echo getWrapper($json, $count); //close the database connection $db = NULL; } catch (PDOException $ex) { print 'Exception: ' . $ex->getMessage(); } }
function get_heater_form_content($param) { /*header("HTTP/1.0 500 Internal Server Error", true, 500); print json_encode(array("status"=>"error", "message"=>"it's very bad!"));*/ if ($param == "material") { $table_name = 'alloys'; $class_name = 'Alloy'; $conds = array('heater' => array(1)); $selected_fields = array('id'); } elseif ($param == "placement") { $table_name = 'rad_eff_coef'; $class_name = 'RadEffCoef'; $conds = null; $selected_fields = array('id'); } try { $db = MySQLDBase::instance(); } catch (Exception $e) { //print "Запрос к БД не может быть выполнен. ".$e->getMessage().". Повторите попытку расчета позже"; print json_encode(array("status" => "db_error", "error_header" => "Ошибка БД", "error_message" => $e->getMessage())); // обработка исключения } $query = getFilterQuery($table_name, $selected_fields, $conds); try { $rows = $db->select($query); foreach ($rows as $row) { $ids[] = $row[id]; } //echo "<br>ids: "; print_r($ids); echo "<br>"; $options = array(); fillGenericArray($table_name, $class_name, $options, $ids); // что будет в случае неудачи? if ($param == "placement") { $compare_funс = function ($first, $second) { if ($first->order == $second->order) { return 0; } return $first->order < $second->order ? -1 : 1; }; uasort($options, $compare_funс); } //echo "<br>options: "; print_r($options); echo "<br>"; foreach ($options as $option) { if ($param == "material") { // получаем максимальную рабочую температуру нагревателя (наибольшее значение) $query = "SELECT MAX(`max_temp`) FROM `max_heater_temp` WHERE `alloy_id` = " . mysql_real_escape_string($option->id); $res = $db->select($query); //echo "res: "; print_r($res); echo "\n"; if (empty($res[0]['MAX(`max_temp`)'])) { // если в БД нет максимальной температуры для данного сплава, то не включаем его в список доступных сплавов continue; } $max_heater_temp = $res[0]['MAX(`max_temp`)']; // получаем допустимые значения температуры нагревателя $query = "SELECT DISTINCT `temp_heater` FROM `metotech_metalls`.`heater_surface_power` WHERE `temp_heater` <= " . mysql_real_escape_string($max_heater_temp); $temps = $db->select($query); foreach ($temps as $temp) { $arr[] = $temp['temp_heater']; } $t_h = getCommaSeparatedList($arr); // получаем допустимые значения температуры изделия $query = "SELECT DISTINCT `temp_solid` FROM `metotech_metalls`.`heater_surface_power` WHERE `temp_solid` < " . mysql_real_escape_string($max_heater_temp); $temps = $db->select($query); unset($arr); foreach ($temps as $temp) { $arr[] = $temp['temp_solid']; } $t_s = getCommaSeparatedList($arr); //echo "<br>t_s: ".$t_s."<br>"; $html_code .= "<option value=\"" . $option->id . "\" data-resistivity=\"" . $option->resistivity . "\" data-htemps=\"" . $t_h . "\" data-stemps=\"" . $t_s . "\" data-density=\"" . $option->density . "\">"; $html_code .= $option->__toString(); $html_code .= "</option>\n"; unset($arr); } elseif ($param == "placement") { $av_coef = ($option->min_coef + $option->max_coef) / 2; $html_code .= "<option value=\"" . $av_coef . "\">"; $html_code .= $option->__toString(); $html_code .= "</option>\n"; } } print $html_code; } catch (Exception $e) { print json_encode(array("status" => "db_error", "error_header" => "Ошибка БД", "error_message" => $e->getMessage())); } }