File size: 9.12Kb
<?php
require_once 'db.php';
$view_id = isset($_GET['user_id']) ? (int)$_GET['user_id'] : 0;
if ($view_id <= 0) {
header('Location: browse.php');
exit;
}
$current_id = !empty($_SESSION['user_id']) ? (int)$_SESSION['user_id'] : 0;
// если смотрим сами на себя – перекидываем на свой профиль
if ($current_id && $current_id === $view_id) {
header('Location: profile.php');
exit;
}
// грузим пользователя
$stmt = $mysqli->prepare("
SELECT id, name, gender, birthday, city, about, photo,
TIMESTAMPDIFF(YEAR, birthday, CURDATE()) AS age,
is_banned
FROM users
WHERE id = ?
");
$stmt->bind_param('i', $view_id);
$stmt->execute();
$user = $stmt->get_result()->fetch_assoc();
$stmt->close();
if (!$user) {
include 'header.php';
echo '<p>Пользователь не найден.</p>';
include 'footer.php';
exit;
}
if (!empty($user['is_banned'])) {
include 'header.php';
echo '<p>Этот профиль недоступен.</p>';
include 'footer.php';
exit;
}
// интересы
$interests = [];
$stmt = $mysqli->prepare("SELECT interest FROM interests WHERE user_id = ?");
$stmt->bind_param('i', $view_id);
$stmt->execute();
$res = $stmt->get_result();
while ($row = $res->fetch_assoc()) {
$interests[] = $row['interest'];
}
$stmt->close();
// альбом (12 фото)
$stmt = $mysqli->prepare("
SELECT filename
FROM user_photos
WHERE user_id = ?
ORDER BY created_at DESC
LIMIT 12
");
$stmt->bind_param('i', $view_id);
$stmt->execute();
$album = $stmt->get_result();
$stmt->close();
// статус дружбы
$friendStatus = 'none'; // none | incoming | outgoing | friends
if ($current_id) {
$stmt = $mysqli->prepare("
SELECT requester_id, addressee_id, status
FROM friendships
WHERE (requester_id = ? AND addressee_id = ?)
OR (requester_id = ? AND addressee_id = ?)
LIMIT 1
");
$stmt->bind_param('iiii', $current_id, $view_id, $view_id, $current_id);
$stmt->execute();
$rel = $stmt->get_result()->fetch_assoc();
$stmt->close();
if ($rel) {
if ($rel['status'] === 'accepted') {
$friendStatus = 'friends';
} elseif ($rel['status'] === 'pending') {
if ((int)$rel['requester_id'] === $current_id) {
$friendStatus = 'outgoing';
} else {
$friendStatus = 'incoming';
}
}
}
}
include 'header.php';
?>
<style>
.view-profile-page { margin-top: 18px; }
.view-layout {
display: grid;
grid-template-columns: 280px minmax(0, 1fr);
gap: 20px;
}
.view-main-card {
text-align: center;
padding: 20px;
border-radius: 24px;
background: #ffffff;
box-shadow: 0 10px 30px rgba(0,0,0,0.06);
}
.view-avatar {
width: 140px;
height: 140px;
border-radius: 999px;
object-fit: cover;
margin-bottom: 10px;
}
.view-avatar-placeholder {
width: 140px;
height: 140px;
border-radius: 999px;
background: linear-gradient(135deg,#ffe5f0,#f5ebff);
display:flex;align-items:center;justify-content:center;
font-size:40px;margin:0 auto 10px;
}
.view-name { font-size:22px;margin-bottom:4px; }
.view-age { font-size:14px;color:#ff6b9c; }
.view-city { font-size:14px;color:#7b7287;margin-top:4px; }
.view-actions { margin-top:16px;display:flex;flex-direction:column;gap:8px; }
.view-card {
padding:18px 20px;
border-radius:24px;
background:#ffffff;
box-shadow:0 10px 30px rgba(0,0,0,0.06);
margin-bottom:14px;
}
.view-card h2 { margin:0 0 6px;font-size:18px; }
.view-about { font-size:14px;color:#4a4257;white-space:pre-wrap; }
.view-interests{margin-top:8px;}
.view-chip{
display:inline-block;
padding:4px 10px;
border-radius:999px;
background:#ffe6f1;
color:#74415a;
font-size:12px;
margin:0 4px 4px 0;
}
/* альбом */
.view-album-grid{
display:grid;
grid-template-columns:repeat(auto-fit,minmax(82px,1fr));
gap:8px;
}
.view-album-photo{
width:100%;aspect-ratio:1/1;border-radius:16px;object-fit:cover;
}
.view-album-empty{font-size:14px;color:#7b7287;}
/* дружба */
.friend-status-badge {
font-size:12px;
color:#7b7287;
}
@media (max-width:768px){
.view-layout{grid-template-columns:1fr;}
.view-main-card{margin:0 -4px;border-radius:18px;}
.view-card{margin:10px -4px 0;border-radius:18px;}
}
</style>
<div class="view-profile-page">
<h1>Профиль пользователя</h1>
<div class="view-layout">
<section class="view-main-card">
<?php if ($user['photo']): ?>
<img src="/assets/img/<?= htmlspecialchars($user['photo']) ?>" class="view-avatar" alt="">
<?php else: ?>
<div class="view-avatar-placeholder">🙂</div>
<?php endif; ?>
<div class="view-name"><?= htmlspecialchars($user['name'] ?: 'Без имени') ?></div>
<?php if (!empty($user['age'])): ?>
<div class="view-age"><?= (int)$user['age'] ?> лет</div>
<?php endif; ?>
<?php if (!empty($user['city'])): ?>
<div class="view-city"><?= htmlspecialchars($user['city']) ?></div>
<?php endif; ?>
<?php if ($current_id): ?>
<div class="view-actions">
<a href="messages.php?user_id=<?= $view_id ?>" class="btn-primary">Написать сообщение</a>
<a href="send_gift.php?user_id=<?= $view_id ?>" class="btn-outline">Подарок 🎁</a>
<!-- дружба -->
<?php if ($friendStatus === 'none'): ?>
<form method="post" action="friends_action.php">
<input type="hidden" name="action" value="add">
<input type="hidden" name="target_id" value="<?= $view_id ?>">
<button type="submit" class="btn-outline">Добавить в друзья</button>
</form>
<?php elseif ($friendStatus === 'outgoing'): ?>
<div class="friend-status-badge">Заявка в друзья отправлена</div>
<?php elseif ($friendStatus === 'incoming'): ?>
<form method="post" action="friends_action.php" style="display:flex;gap:6px;justify-content:center;">
<input type="hidden" name="target_id" value="<?= $view_id ?>">
<button type="submit" name="action" value="accept" class="btn-primary btn-small">
Принять в друзья
</button>
<button type="submit" name="action" value="decline" class="btn-outline btn-small">
Отклонить
</button>
</form>
<?php elseif ($friendStatus === 'friends'): ?>
<div class="friend-status-badge">Вы в друзьях</div>
<form method="post" action="friends_action.php">
<input type="hidden" name="target_id" value="<?= $view_id ?>">
<button type="submit" name="action" value="remove" class="btn-outline btn-small">
Удалить из друзей
</button>
</form>
<?php endif; ?>
</div>
<?php else: ?>
<div style="margin-top:12px;font-size:13px;color:#7b7287;">
Чтобы написать сообщение или отправить подарок, <a href="login.php">войдите</a>.
</div>
<?php endif; ?>
</section>
<section>
<div class="view-card">
<h2>О себе</h2>
<div class="view-about">
<?= nl2br(htmlspecialchars($user['about'] ?: 'Пока здесь пусто.')) ?>
</div>
<?php if ($interests): ?>
<div class="view-interests">
<?php foreach ($interests as $i): ?>
<span class="view-chip"><?= htmlspecialchars($i) ?></span>
<?php endforeach; ?>
</div>
<?php endif; ?>
</div>
<div class="view-card">
<h2>Фотоальбом</h2>
<?php if ($album->num_rows === 0): ?>
<p class="view-album-empty">В альбоме пока нет фотографий.</p>
<?php else: ?>
<div class="view-album-grid">
<?php while ($p = $album->fetch_assoc()): ?>
<img src="/assets/img/<?= htmlspecialchars($p['filename']) ?>" class="view-album-photo" alt="">
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</section>
</div>
</div>
<?php include 'footer.php'; ?>