64 lines
2.2 KiB
PHP
64 lines
2.2 KiB
PHP
<?php
|
|
require_once 'includes/header.php';
|
|
require_once 'db/config.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['service_id'])) {
|
|
$service_id = $_POST['service_id'];
|
|
|
|
$p = db()->prepare('SELECT * FROM user_services WHERE user_id = ? AND service_id = ?');
|
|
$p->execute([$user_id, $service_id]);
|
|
$existing_subscription = $p->fetch();
|
|
|
|
if (!$existing_subscription) {
|
|
$p = db()->prepare('INSERT INTO user_services (user_id, service_id) VALUES (?, ?)');
|
|
$p->execute([$user_id, $service_id]);
|
|
}
|
|
|
|
header('Location: subscribe.php');
|
|
exit();
|
|
}
|
|
|
|
$p = db()->prepare('SELECT * FROM services');
|
|
$p->execute();
|
|
$services = $p->fetchAll();
|
|
|
|
$p = db()->prepare('SELECT service_id FROM user_services WHERE user_id = ?');
|
|
$p->execute([$user_id]);
|
|
$subscribed_services = $p->fetchAll(PDO::FETCH_COLUMN);
|
|
|
|
?>
|
|
|
|
<div class="container">
|
|
<h1 class="mt-5">Subscribe to Services</h1>
|
|
<p class="lead">Browse our available services and subscribe to the ones you need.</p>
|
|
|
|
<div class="row">
|
|
<?php foreach ($services as $service): ?>
|
|
<div class="col-md-4">
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<h5 class="card-title"><?php echo htmlspecialchars($service['name']); ?></h5>
|
|
<p class="card-text"><?php echo htmlspecialchars($service['description']); ?></p>
|
|
<?php if (in_array($service['id'], $subscribed_services)): ?>
|
|
<button class="btn btn-secondary" disabled>Subscribed</button>
|
|
<?php else: ?>
|
|
<form action="subscribe.php" method="POST">
|
|
<input type="hidden" name="service_id" value="<?php echo $service['id']; ?>">
|
|
<button type="submit" class="btn btn-primary">Subscribe</button>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once 'includes/footer.php'; ?>
|