/
HW7_2.php
157 lines (147 loc) · 6.59 KB
/
HW7_2.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?
define ('ads', 'ads.txt');
$cities = array('-- Выберите город --', 'Донецк', 'Луганск', 'Харьков', 'Днепропетровск', 'Киев');
$categories = array('-- Выберите Категорию --', 'Работа', 'Услуги', 'Хобби', 'Недвижимость', 'Транспорт');
function showAd(){
echo '<h1>Объявления</h1>';
if(file_exists(ads) && filesize(ads) != 0){
$array_ads = file(ads);
foreach ($array_ads as $array){
$ad = unserialize($array);
foreach ($ad as $key=> $aa){
$ad = explode(' / ', $aa);
echo '<a href="HW7_2.php?id='.$key.'">'.$ad[7] .'</a>' . ' | '. $ad[2] . ' | <a href="HW7_2.php?delete='.$key.'">Удалить</a><br>';
}
}
} else {
echo 'Объявлений нет';
}
}
function cleaning($variable){
$var = htmlentities(trim($variable));
return $var;
}
function unSer($file_name, $key){
$remaining_ads = '';
$current_ads = file_get_contents($file_name);
$current_ads = explode("\n", $current_ads);
foreach ($current_ads as $current_ad){
$current_ad = unserialize($current_ad);
if(isset($current_ad[$key])){
unset($current_ad[$key]);
} elseif(!empty($current_ad)){
$current_ad = serialize($current_ad);
$remaining_ads .= $current_ad."\n";
}
}
file_put_contents(ads, $remaining_ads);
header("Location: HW7_2.php");
}
function selected($array, $field_name){
$select = $field_name;
$output = '';
foreach ($array as $val){
$output .= '<option value="' . $val . '"';
if($val==$select)
$output .= ' selected="selected"';
$output .= '>';
$output .= $val;
$output .= '</option>';
}
echo $output;
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$type = cleaning($_POST['type']);
$name = cleaning($_POST['name']);
$email = cleaning($_POST['email']);
if(isset($_POST['otvet'])){
$otvet = $_POST['otvet'];
} else {
$otvet = '';
}
$phone = $_POST['phone'];
$city = cleaning($_POST['city']);
$category = cleaning($_POST['category']);
$ad_title = cleaning($_POST['ad_title']);
$ad_description = cleaning($_POST['ad_description']);
$price = $_POST['price'];
$str[uniqid()] = $type . ' / ' . $name . ' / ' . $email . ' / ' . $otvet . ' / ' . $phone . ' / ' . $city . ' / ' . $category . ' / ' . $ad_title . ' / ' . $ad_description . ' / ' . $price;
$str = serialize($str);
file_put_contents(ads, $str . "\n", FILE_APPEND);
} elseif(isset($_GET['delete'])){
unSer(ads, $_GET['delete']);
} elseif(isset($_GET['id'])){
$arr = file_get_contents(ads);
$arr = explode("\n", $arr);
foreach ($arr as $aa){
$aa = unserialize($aa);
if(isset($aa[$_GET['id']])){
$bb = explode(' / ', $aa[$_GET['id']]);
$name = cleaning($bb[1]);
$email = cleaning($bb[2]);
$phone = $bb[4];
$ad_title = cleaning($bb[7]);
$ad_description = cleaning($bb[8]);
$price = $bb[9];
$type = cleaning($bb[0]);
$category = cleaning($bb[6]);
if(isset($bb[3])){
$otvet = cleaning($bb[3]);
} else {
$otvet = '';
}
$city = cleaning($bb[5]);
}
} ?>
<form method="POST">
<? if($type == 'private_person'){?>
<p><input type="radio" name="type" value="private_person" checked> Частное лицо
<input type="radio" name="type" value="company"> Компания</p>
<? } else { ?>
<p><input type="radio" name="type" value="private_person"> Частное лицо
<input type="radio" name="type" value="company" checked> Компания</p>
<? } ?>
<p>Ваше имя <input type="text" name="name" value="<?= $name; ?>"/></p>
<p>Электронная почта <input type="email" name="email" value="<?= $email; ?>"/></p>
<? if(empty($otvet)){?>
<p><input type="checkbox" name="otvet" /> Я не хочу получать ответы по объявлению по e-mail </p>
<? } else { ?>
<p><input type="checkbox" name="otvet" checked/> Я не хочу получать ответы по объявлению по e-mail </p>
<? }?>
<p>Номер телефона <input type="tel" name="phone" value="<?= $phone; ?>"/></p>
<p><select name="city">
<? selected($cities, $city); ?>
</select></p>
<p><select name="category">
<? selected($categories, $category); ?>
</select></p>
<p>Название объявления <input type="text" name="ad_title" value="<?= $ad_title; ?>"/></p>
<p>Описание объявления <textarea name="ad_description"><?= $ad_description; ?></textarea></p>
<p>Цена <input type="number" name="price" min="0" max="100000" value="<?= $price; ?>" /> руб</p>
<p><input type="submit" name="submit"/></p>
<a href="HW7_2.php">Главная страница</a>
</form>
<? exit; } ?>
<form method="POST">
<p><input type="radio" name="type" value="private_person" checked> Частное лицо
<input type="radio" name="type" value="company"> Компания</p>
<p>Ваше имя <input type="text" name="name"/></p>
<p>Электронная почта <input type="email" name="email" /></p>
<p><input type="checkbox" name="otvet" /> Я не хочу получать ответы по объявлению по e-mail </p>
<p>Номер телефона <input type="tel" name="phone" /></p>
<p><select name="city"><?
foreach ($cities as $city){ ?>
<option value="<?= $city; ?>"><?= $city; ?></option>
<?}
?></select></p>
<p><select name="category"><?
foreach ($categories as $category){ ?>
<option value="<?= $category; ?>"><?= $category; ?></option>
<?}
?></select></p>
<p>Название объявления <input type="text" name="ad_title"/></p>
<p>Описание объявления <textarea name="ad_description"></textarea></p>
<p>Цена <input type="number" name="price" min="0" max="100000" value="0" /> руб</p>
<input type="submit" name="submit">
</form>
<? showAd();