Exemplo n.º 1
0
 /**
  * Составляет запрос для фильтрации
  * @param string $table имя таблицы, для которой строится запрос
  * @param array $fields key -> фильтруемое поле, value -> значение
  * @param array $returned_fields имена полей, которые необходимо отобрать
  * @return string SQL-запрос
  */
 private function _getFilterQuery($table, $fields, $returned_fields)
 {
     $query = "SELECT ";
     foreach ($returned_fields as $returned_field) {
         $query .= "`{$returned_field}`, ";
     }
     $query = substr($query, 0, strlen($query) - 2);
     $query .= " FROM `metotech_metalls`.`{$table}` WHERE ";
     foreach ($fields as $field => $values) {
         $s = getCommaSeparatedList($values);
         $query .= "`{$field}` IN ({$s}) AND ";
     }
     $query = substr($query, 0, strlen($query) - 5);
     //print "<br><br>query_2: $query<br><br>";
     return $query;
 }
Exemplo n.º 2
0
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()));
    }
}