Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on PHP code is not excluding the watched videos.
Post
PHP code is not excluding the watched videos.
Can someone explain why this PHP code is not excluding the watched videos?
The email_id
parameter is used to specify the location of the watched videos array. The code should remove the watched_videos
array from the user table, but it's not working as expected. Since the watched_videos
array is stored as a string, it needs to be converted to an array. Additionally, I want the code to exclude watched videos when the video_id
is sent back in an AJAX request.
<?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 = "clins@example.com";
// 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);
?>
1 comment thread