html head head body form method POST action _SERVER PHP_SELF сортировк

 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
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER['PHP_SELF']?>">
<p>1 - сортировка `age` - по возрастанию, `gender` - по убыванию ,
2 `age` - по убыванию, `gender` - по возрастанию</p>
<input type="text" name="a">
<input type="submit">
</form>
</body>
</html>
<?php
$row = array(
'a1' => array('id'=>'1', 'age'=>'16', 'gender'=>'m', 'login'=>'Вася'),
'a2' => array('id'=>'2', 'age'=>'18', 'gender'=>'m', 'login'=>'Петя'),
'a3' => array('id'=>'3', 'age'=>'20', 'gender'=>'g', 'login'=>'Катя'),
'a4' => array('id'=>'4', 'age'=>'20', 'gender'=>'m', 'login'=>'Стас'),
'a5' => array('id'=>'5', 'age'=>'12', 'gender'=>'g', 'login'=>'Маша'),
'a6' => array('id'=>'6', 'age'=>'44', 'gender'=>'g', 'login'=>'Галя'),
'a7' => array('id'=>'7', 'age'=>'45', 'gender'=>'m', 'login'=>'Макс'),
'a8' => array('id'=>'8', 'age'=>'20', 'gender'=>'m', 'login'=>'Илья'),
'a9' => array('id'=>'9', 'age'=>'20', 'gender'=>'g', 'login'=>'Даша'),
);
$sort =$_POST["a"];
if (!empty($sort)) {
If ($sort == 1) {
usort($row, function ($a, $b) {
return ($a['age'] - $b['age']);
});
foreach ($row as $array) {
echo $array['age'];
}
echo '<br>';
function cmp($a, $b)
{
if ($a['gender'] == $b['gender']) {
return 0;
}
return ($a['gender'] < $b['gender']) ? -1 : 1;
}
usort($row, "cmp");
foreach ($row as $array) {
echo $array['gender'];
}
} elseif ($sort == 2) {
usort($row, function ($a, $b) {
return ($b['age'] - $a['age']);
});
foreach ($row as $array) {
echo $array['age'];
}
echo '<br>';
function cmp($a, $b)
{
if ($a['gender'] == $b['gender']) {
return 0;
}
return ($a['gender'] > $b['gender']) ? -1 : 1;
}
usort($row, "cmp");
foreach ($row as $array) {
echo $array['gender'];
}
} else {
echo "Введены некорректные данные";
}
}