/
index.php
54 lines (40 loc) · 1.42 KB
/
index.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
<?php
include('config/db_connect.php');
// 1. Write query for all albums
$sql = 'SELECT id, artist, title, price FROM albums ORDER BY artist';
// 2. make query & get results
$result = mysqli_query($conn, $sql);
// 3. fetch the resulting rows as an array
$albums = mysqli_fetch_all($result, MYSQLI_ASSOC);
// 4. check the array
// print_r($albums);
// 5. free result from memory
mysqli_free_result($result);
// 6. close connection
mysqli_close($conn);
// explode
// print_r(explode(',', $albums[0]['songs']));
?>
<?php include('templates/header.php'); ?>
<div class="container p-4">
<table class="table table-striped mt-3">
<thead>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<!-- 7. cycle through the array -->
<?php foreach($albums as $album) : ?>
<tr>
<td><?php echo htmlspecialchars($album['artist']); ?></td>
<td><?php echo htmlspecialchars($album['title']); ?></td>
<td><a href="details.php?id=<?php echo $album['id']; ?>"><button class="btn btn-sm btn-light">More info</button></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php include('templates/footer.php');