-
Notifications
You must be signed in to change notification settings - Fork 0
/
song.php
65 lines (55 loc) · 2.04 KB
/
song.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
55
56
57
58
59
60
61
62
63
64
65
<?php
// Include database connection
include 'db_connection.php'; // Ensure correct path to db_connection.php
// Initialize variables
$song = null;
$comments = [];
function fetchSongDetails($conn, $songID) {
$sql = "SELECT s.*, a.artist_name
FROM Songs s
JOIN artist a ON s.artist_id = a.artist_id
WHERE s.id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $songID);
$stmt->execute();
$result = $stmt->get_result();
$song = $result->fetch_assoc();
$stmt->close();
return $song;
}
// Function to get profile image path
function getProfileImagePath($profile_image) {
if (!empty($profile_image)) {
if (strpos($profile_image, 'uploads/') === 0) {
$image_path = $profile_image;
} elseif (strpos($profile_image, 'uploads/') === 0) {
$image_path = substr($profile_image, 3);
} else {
$image_path = 'uploads/profile/' . $profile_image;
}
} else {
$image_path = 'assets/pic/default.jpg'; // Default image path
}
return $image_path;
}
// Function to fetch comments for a song
function fetchComments($conn, $songID) {
$comments = [];
$commentsQuery = "SELECT c.*, u.name, u.profile_image FROM Comments c JOIN users u ON c.user_id = u.user_id WHERE song_id = $songID ORDER BY created_at DESC";
$commentsResult = mysqli_query($conn, $commentsQuery);
if ($commentsResult) {
while ($row = mysqli_fetch_assoc($commentsResult)) {
// Process the profile image path
$row['profile_image'] = getProfileImagePath($row['profile_image']);
$comments[] = $row;
}
}
return $comments;
}
// Handle new comment submission
function addComment($conn, $songID, $userID, $commentText) {
$commentText = mysqli_real_escape_string($conn, $commentText);
$insertCommentQuery = "INSERT INTO Comments (song_id, user_id, comment_text) VALUES ($songID, $userID, '$commentText')";
$result = mysqli_query($conn, $insertCommentQuery);
return $result;
}