Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

50%
+0 −0
Q&A 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_vi...

1 answer  ·  posted 12mo ago by Collins ‭  ·  last activity 11mo ago by dagelf‭

Question php mysql
#2: Post edited by user avatar Alexei‭ · 2023-04-18T07:40:05Z (12 months ago)
replaced what seems to be a real e-mail with an example one
  • 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
  • <?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@gmail.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);
  • ?>
  • ```
  • 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
  • <?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: Initial revision by user avatar Collins ‭ · 2023-04-16T11:32:57Z (12 months ago)
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
<?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@gmail.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);
?>

```