-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImages.php
138 lines (131 loc) · 3.35 KB
/
Images.php
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Class Images
* @package graphics
* @author Rytov Anton
* @copyright 2018
*/
class Images
{
/**
*
*/
const HOST = '127.0.0.1';
/**
*
*/
const DB = 'graphics';
/**
*
*/
const USER = 'root';
/**
*
*/
const PASS = '';
/**
*
*/
const CHARSET = 'utf8';
/**
* @var PDO
*/
private $db_conn;
/**
* @var array
*/
private $images;
/**
* индекс правильного отвнта на вопрос
* @var
*/
private $true_index;
/**
* количество вариантов ответа на вопрос
* @var int
*/
private $count_images;
/**
* @var
*/
private $count_result;
/**
* создает вопрос и $count_result например 4 варианта ответа
* Всего есть $this->count_images графиков функций
* Images constructor.
* @param $count_result
*/
public function __construct($count_result)
{
$this->count_result = $count_result;
$dsn = "mysql:host=".self::HOST.";dbname=".self::DB.";charset=".self::CHARSET;
$this->db_conn = new PDO($dsn, self::USER, self::PASS);
$query = 'SELECT * FROM question';
$sth = $this->db_conn->query($query);
$arr = [];
while ($row = $sth->fetch()) {
$arr[] = $row;
}
$this->images = $arr;
$this->count_images = count($arr);
}
/**
* формируем ответ на вопрос
*/
public function get_true_image()
{
$var = rand(0, $this->count_images);
$this->true_index = $var;
echo '<h5>'.$this->images[$var]['question'].'</h5>';
}
/**
* получаем индекс правильного ответа на вопрос
*/
public function getTrueIndex()
{
$index = $this->true_index;
$index++;
echo $index;
}
/**
* генерируем неправильные ответы на вопрос
* @return array
*/
public function get_indexes()
{
$rand_img = [];
$rand_img[] = $this->true_index;
while (count($rand_img) < $this->count_result) {
$var2 = rand(0, $this->count_images);
if (!in_array($var2, $rand_img)) {
$rand_img[] = $var2;
}
}
return $rand_img;
}
/**
* Используеим полученный массив случайных индексов картинок и выводим мх изображения
*/
public function get_all_image()
{
$arrIndexes = $this->get_indexes();
shuffle($arrIndexes);
$i = 1;
foreach ($arrIndexes as $index) {
echo ' <div class="radio">
<label>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="'.++$index.'">
Graphic '.$i++.'
</label>
</div>';
echo $index.' ';
}
echo '<h5>Выбираем график</h5>';
$counter = 1;
foreach ($arrIndexes as $index) {
echo '<img src="images/'.$this->images[$index]['image'].'">';
echo '<h4>График '.$counter++.'</h4>';
}
$arrIndexes = $this->get_indexes();
}
}