Källkodsbrowser

cart.php

91 lines UTF-8 Unix (LF)
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
<?php
session_start
();
require 
'check_login.php';
require 
'db_connect.php';

$user_id $_SESSION['user_id'];
$message '';

// Hantera borttagning av bok från kundvagnen
if (isset($_POST['remove_book'])) {
    
$book_id_to_remove intval($_POST['book_id']);
    if (isset(
$_SESSION['cart'][$book_id_to_remove])) {
        unset(
$_SESSION['cart'][$book_id_to_remove]);
        
$message "Boken har tagits bort från kundvagnen.";
    }
}

// Beräkna total summa och uppdatera kundvagnen
$total_sum 0;
$cart_items $_SESSION['cart'] ?? [];

foreach (
$cart_items as $book_id => $item) {
    
$total_sum += $item['price'] * $item['quantity'];
}

// Funktion för att visa meddelanden
function display_message($msg$type 'success') {
    if (
$msg) {
        return 
"<p class='message $type'>" htmlspecialchars($msg) . "</p>";
    }
    return 
'';
}

?>
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <title>Kundvagn</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header>
        <div class="store-header">
            <h1>Kundvagn</h1>
            <div class="header-buttons">
                <a href="index.php" class="btn">Tillbaka till butiken</a>
                <a href="user.php" class="btn">Min Sida</a>
                <a href="logout.php" class="logout">Logga ut</a>
            </div>
        </div>
    </header>

    <main>
        <?= display_message($message?>
        
        <div class="cart-container">
            <h2>Dina valda böcker</h2>
            <?php if (empty($cart_items)): ?>
                <p>Din kundvagn är tom.</p>
            <?php else: ?>
                <?php foreach ($cart_items as $item): ?>
                    <div class="cart-item">
                        <span><?= htmlspecialchars($item['book_name']) ?> (<?= $item['quantity'?> st)</span>
                        <span><?= number_format($item['price'] * $item['quantity'], 2','' '?> kr</span>
                        <form method="post" style="display:inline;">
                            <input type="hidden" name="book_id" value="<?= $item['book_id'?>">
                            <button type="submit" name="remove_book" class="btn delete-btn" style="padding: 5px 10px;">Ta bort</button>
                        </form>
                    </div>
                <?php endforeach; ?>

                <div class="cart-total">
                    Total summa: <?= number_format($total_sum2','' '?> kr
                </div>

                <div class="cart-actions">
                    <form action="process_purchase.php" method="post">
                        <button type="submit" class="btn">Slutför köp</button>
                    </form>
                </div>
            <?php endif; ?>
        </div>
    </main>

    <footer>
        <p>&copy; 2025 E-bookstore</p>
    </footer>
</body>
</html>