include config php function initConnect global config db_connect mysql

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?
include 'config.php';
function initConnect()
{
global $config;
$db_connect = mysql_connect($config['db_server'], $config['db_login'], $config['db_pass']) or die("Could not connect: " . mysql_error());
mysql_select_db($config['db_name']);
return $db_connect;
}
function closeConnect($db_connect)
{
mysql_close($db_connect);
}
function getList($table, $field, $w_field = '', $w_value = '')
{
$sql = 'SELECT id,' . $field . ' FROM expert_' . $table . '';
if ($w_field && $w_value) {
$sql .= " WHERE " . $w_field . " = '" . $w_value . "'";
}
return mysql_query($sql);
}
function getRecords($table, $w_field = '', $w_value = '')
{
$sql = 'SELECT * FROM expert_' . $table . '';
if ($w_field && $w_value) {
$sql .= " WHERE " . $w_field . " = '" . $w_value . "'";
}
return mysql_query($sql);
}
function initIndex()
{
$db_connect = initConnect();
$params = array();
$list = array();
$question = array();
$question_id = 0;
$end = false;
if (isset($_GET['users'])) {
if ($user_id = intval($_GET['users'])) {
$get_params = $_GET;
$sqlResult = getList('question', 'text', 'user_id', $user_id);
while ($item = mysql_fetch_array($sqlResult, MYSQL_ASSOC)) {
$question[$item['id']] = $item['text'];
}
$name = 'question';
if ($count = count($get_params['question'])) {
if ($count < count($question)) {
end($get_params['question']);
$question_id = key($get_params['question']);
foreach ($question as $key => $item) {
if ($key == $question_id) {
$params['caption'] = current($question);
break;
}
}
} else {
$end = true;
$params['caption'] = 'Экспертная система завершила анализ.';
$params['condition'] = array();
$hashArray = array();
foreach ($get_params['question'] as $question_id => $answer_id) {
$sqlResult = getRecords('answer', 'id', $answer_id);
$sqlResult = mysql_fetch_array($sqlResult, MYSQL_ASSOC);
$params['condition'][$question[$question_id]] = $sqlResult['text'];
$hashArray[] = $sqlResult['hash'];
}
$sqlResult = getRecords('product', 'hash', serialize($hashArray));
$params['product'] = mysql_fetch_array($sqlResult, MYSQL_ASSOC);
}
} else {
$params['caption'] = reset($question);
}
if (!$end) {
$question_id = intval(key($question));
$sqlResult = getList('answer', 'text', 'question_id', $question_id);
while ($item = mysql_fetch_array($sqlResult, MYSQL_ASSOC)) {
$list[$item['id']] = $item['text'];
}
}
$params['question_id'] = $question_id;
$params['params'] = $get_params;
}
} else {
$name = 'users';
$params['caption'] = 'Пожалуйста, укажите Ваш статус';
$sqlResult = getList($name, 'name');
while ($item = mysql_fetch_array($sqlResult, MYSQL_ASSOC)) {
$list[$item['id']] = $item['name'];
}
}
$params['name'] = $name;
$params['list'] = $list;
include 'index.tpl';
closeConnect($db_connect);
}
initIndex();
?>