Can anybody explain why this php code is not excluding the watched videos?
the email_id is for specifying where to find the watched videos array.
i want the code to remove watched_videos array from the user table. the watched_videos array is stored as a string so it need to be converted to an array. is there a way i can make this code work.
i want the code to exclude watched videos when the video_id are being sent back to an ajax request.
here is the code:
<?php
// Database credentials
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "refwebsite";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the start and limit parameters
$start = 1;
$limit = 12;
$email_id = "[email protected]";
// Get the watched_videos array for the user with the given email_id
$sql_watched = "SELECT watched_videos FROM users WHERE email_id='$email_id'";
$result_watched = mysqli_query($conn, $sql_watched);
$row_watched = mysqli_fetch_assoc($result_watched);
$watched_videos = explode(",", $row_watched['watched_videos']);
// Generate a comma-separated list of unique_id values to exclude
$exclude_list = implode(",", $watched_videos);
// Select two random videos from the "less_videos" table, excluding watched videos
$sql_less = "SELECT video_id, description, points FROM less_videos WHERE unique_id NOT IN ('" . implode("', '", $watched_videos) . "') ORDER BY RAND() LIMIT 2";
$result_less = mysqli_query($conn, $sql_less);
// Select ten random videos from the "more_videos" table, excluding watched videos
$sql_more = "SELECT video_id, description, points FROM more_videos WHERE unique_id NOT IN ('" . implode("', '", $watched_videos) . "') ORDER BY RAND() LIMIT 10";
$result_more = mysqli_query($conn, $sql_more);
// Combine the selected videos into a single array
$data = array();
while ($row = mysqli_fetch_assoc($result_less)) {
$data[] = $row;
}
while ($row = mysqli_fetch_assoc($result_more)) {
$data[] = $row;
}
// Shuffle the array to randomize the order of videos
shuffle($data);
// Extract the required subset of videos
$data_subset = array_slice($data, $start, $limit);
// Return the data as JSON
echo json_encode($data_subset);
// Close the connection
mysqli_close($conn);
i tried finding the problem but i did not.