subversion://[vpntest]/release/2.0/WWW/lib/load.php Log in
ViewVC logotype

Annotate of /release/2.0/WWW/lib/load.php

Parent Directory Parent Directory | Revision Log Revision Log

Revision 9 - (show annotations) (download) (as text)
Sun Feb 17 15:07:41 2008 MSK (12 years, 9 months ago) by vitalif
File MIME type: text/x-php; charset=windows-1251
File size: 3514 byte(s)
Release 2.0

1 <?php
2
3 // Загрузка списка вопросов-ответов а-ля "Кто хочет спать с миллионером"
4 // Copyright (c) 2006, Виталий Филиппов [VMX]
5
6 require_once 'config.php';
7
8 // Результат в виде ассоциативного массива с
9 // соответствиями [id][имя_колонки] => значение_колонки
10 function load_questions ($filename, $all_variant = 1)
11 {
12 $fl = @file ($filename);
13 $fc = count ($fl);
14 for ($qi = 1, $fi = 0; $fi < $fc; $qi++)
15 {
16 while (strlen(trim($fl[$fi])) == 0) $fi++;
17 $quest [$qi]['id'] = NULL;
18 $fl [$fi] = trim ($fl [$fi]); $pp = strpos ($fl [$fi], '. ');
19 $quest [$qi]['num'] = 1 + substr ($fl [$fi], 0, $pp) - 1;
20 $quest [$qi]['varid'] = $all_variant;
21 $quest [$qi]['question'] = substr ($fl [$fi++], $pp+2);
22 $quest [$qi]['rightanswer'] = 1;
23 $quest [$qi]['answer1'] = trim ($fl [$fi++]);
24 if ($quest [$qi]['answer1']{0} == '*')
25 {
26 $quest [$qi]['rightanswer'] = 1;
27 $quest [$qi]['answer1'] = ltrim ($quest [$qi]['answer1'], "* \t");
28 }
29 $quest [$qi]['answer2'] = trim ($fl [$fi++]);
30 if ($quest [$qi]['answer2']{0} == '*')
31 {
32 $quest [$qi]['rightanswer'] = 2;
33 $quest [$qi]['answer2'] = ltrim ($quest [$qi]['answer2'], "* \t");
34 }
35 $quest [$qi]['answer3'] = trim ($fl [$fi++]);
36 if ($quest [$qi]['answer3']{0} == '*')
37 {
38 $quest [$qi]['rightanswer'] = 3;
39 $quest [$qi]['answer3'] = ltrim ($quest [$qi]['answer3'], "* \t");
40 }
41 $quest [$qi]['answer4'] = trim ($fl [$fi++]);
42 if ($quest [$qi]['answer4']{0} == '*')
43 {
44 $quest [$qi]['rightanswer'] = 4;
45 $quest [$qi]['answer4'] = ltrim ($quest [$qi]['answer4'], "* \t");
46 }
47 }
48 return $quest;
49 }
50
51 // Загрузить ассоциативный массив $data в таблицу $table
52 function load_into_database ($table, $data)
53 {
54 global $result, $QuestionsTable;
55 foreach ($data as $datai => $cd)
56 {
57 $fields = array_keys ($cd);
58 $sql = "INSERT INTO `$table` SET ";
59 foreach ($fields as $cfi => $cf)
60 {
61 if (gettype($cd[$cf]) == 'integer')
62 $sql .= "`$cf`=$cd[$cf]";
63 else if ($cd[$cf] == NULL)
64 $sql .= "`$cf`=DEFAULT";
65 else
66 {
67 $ccc = mysql_escape_string ($cd[$cf]);
68 $sql .= "`$cf`='$ccc'";
69 }
70 if ($cfi < count ($fields)-1)
71 $sql .= ', ';
72 }
73 $result = amysql_query ($sql);
74 if (!$result)
75 {
76 echo mysql_error ();
77 exit;
78 return false;
79 }
80 }
81 return true;
82 }
83
84 /*
85 if (!isset ($req_file))
86 {
87 echo 'Please call script as this: /load.php?file=FILE_WITH_QUESTIONS.TXT';
88 exit;
89 }
90
91 // Соединяемся с MySQL
92 $link = mysql_connect ($dbhost, $dbuser, $dbpwd);
93 if (!$link)
94 {
95 echo ('Could not connect to the database: ' . mysql_errno() . ": " . mysql_error());
96 exit;
97 }
98 if (!mysql_select_db($db))
99 {
100 echo ('Could not select database.');
101 exit;
102 }
103
104 amysql_query ("SET NAMES cp1251");
105 //$result = amysql_query ("DROP TABLE IF EXISTS `$QuestionsTable`"); if (!$result) return false;
106 //$result = amysql_query ("CREATE TABLE IF NOT EXISTS `$QuestionsTable` (`id` int(11) NOT NULL auto_increment, `varid` int(11) default NULL, `question` text NOT NULL, `answer1` text NOT NULL, `answer2` text NOT NULL, `answer3` text NOT NULL, `answer4` text NOT NULL, `rightanswer` int(11) default NULL, PRIMARY KEY `id` (`id`)) AUTO_INCREMENT=1"); if (!$result) return false;
107 import_request_variables("gp", "req_");
108 if (load_into_database($QuestionsTable, load_questions($req_file)))
109 echo 'Success';
110 else echo 'Error ' . mysql_errno() . ': ' . mysql_error();
111
112 // Очищаем MySQL-соединение
113 @mysql_free_result($result);
114 mysql_close($link);
115 */
116
117 ?>