/
task4.php
85 lines (67 loc) · 1.42 KB
/
task4.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
<?php
require_once 'functions.php';
$numbers = getValue($_POST, 'numbers');
function validateForm(&$errors) {
global $numbers;
if (!validateRequiredField($numbers)) {
$errors['numbers'][] = 'Enter first number';
}
return !empty($errors);
}
$validationErrors = [];
if ($_POST) {
validateForm($validationErrors);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Task4</title>
<style>
#numbers {
width: 300px;
margin-bottom: 10px;
}
.error {
color: red;
}
.error input {
border-color: red;
}
</style>
</head>
<body>
<form action="" method="post">
<div class = "<?= getErrorClass($validationErrors, 'numbers') ?>">
<label for="numbers">Enter 10 numbers separated with commas:</label>
<input type="text" id="numbers" name="numbers" />
<?= getFieldErrors($validationErrors, 'numbers')?>
</div>
<input type="submit" id="submit" />
</form>
<br />
<?php
if ($_POST) {
$nums = explode(',', $numbers);
sort($nums);
$min = $nums[0];
$max = $nums[0];
for ($i = 0; $i < 10; $i++) {
if ($min > $nums[$i]) {
$min = $nums[$i];
}
if ($max < $nums[$i]) {
$max = $nums[$i];
}
}
echo 'Sorted numbers: ';
foreach ($nums as $value) {
echo $value, ' ';
}
echo '<p></p>';
echo 'Min number: ', $min, ' Max number: ', $max;
}
?>
</body>
</html>