-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
121 lines (102 loc) · 4.22 KB
/
Copy pathsetup.php
File metadata and controls
121 lines (102 loc) · 4.22 KB
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
<?php
// Use centralized DB/bootstrap and seeders from /database
require_once __DIR__ . '/database/config.php';
require_once __DIR__ . '/database/database-seed.php';
function createDatabase() {
// DB and schema are ensured by database/config.php; just return global $pdo
global $pdo, $database;
return $pdo;
}
function createTables($pdo) {
// Tables are created by createTablesDalamDatabase in database/config.php
return true;
}
function populateData($pdo) {
// Delegate to runDatabaseSeed from database/database-seed.php
runDatabaseSeed($pdo);
return "Seed completed successfully.";
}
function setupDatabase() {
$messages = [];
$pdo = createDatabase();
$messages[] = "Database connection ready.";
$result = createTables($pdo);
if ($result === true) {
$messages[] = "Schema ensured.";
} else {
$messages[] = $result;
}
$result = populateData($pdo);
$messages[] = $result;
return $messages;
}
// Alternative: pass $pdo explicitly (no use of global inside these functions)
// function populateDataAlt(PDO $pdo) {
// runDatabaseSeed($pdo);
// return "Seed completed successfully (alt).";
// }
// function setupDatabaseAlt(PDO $pdo) {
// $messages = [];
// // In this alt flow, we assume $pdo is already constructed by config.php
// $messages[] = "Database connection ready (alt).";
// // Schema already ensured by config.php include
// $messages[] = "Schema ensured (alt).";
// $result = populateDataAlt($pdo);
// $messages[] = $result;
// return $messages;
// }
if (isset($_GET['run'])) {
if (isset($_GET['alt'])) {
// Demonstrate alternative parameter-based flow
$results = setupDatabaseAlt($pdo);
} else {
$results = setupDatabase();
}
}
?>
<html>
<head>
<title>Database Setup</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<body class="bg-light">
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow-sm border-0">
<div class="card-header bg-primary text-white text-center">
<h4 class="mb-0">Database Setup</h4>
</div>
<div class="card-body">
<?php if (isset($results)): ?>
<div class="alert alert-info">
<h5>Setup Results:</h5>
<ul class="mb-0">
<?php foreach ($results as $message): ?>
<li><?php echo $message; ?></li>
<?php endforeach; ?>
</ul>
</div>
<div class="d-grid gap-2 mt-3">
<a href="view.php" class="btn btn-success">View Products</a>
<a href="add.php" class="btn btn-primary">Add Product</a>
</div>
<?php else: ?>
<div class="text-center">
<h5>Initialize Database</h5>
<p class="text-muted">Click the button below to create the database, tables, and sample data.</p>
<div class="alert alert-warning">
<strong>Note:</strong> Make sure MySQL server is running and the connection settings in config.php are correct.
</div>
<a href="?run=1" class="btn btn-primary btn-lg">Setup Database</a>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>