Skip to content
PHP Logo

How can JavaScript be integrated with PHP for controlling a video gallery?

To integrate JavaScript with PHP for controlling a video gallery, you can use PHP to dynamically generate the HTML structure of the video gallery and then use JavaScript to add interactivity such as play, pause, and navigate between videos.

php
<?php
// PHP code to generate video gallery
$videos = array("video1.mp4", "video2.mp4", "video3.mp4");

echo '<div id="video-gallery">';
foreach ($videos as $video) {
    echo '<video src="' . $video . '" controls></video>';
}
echo '</div>';
?>

<script>
// JavaScript code to control video gallery
const videos = document.querySelectorAll('#video-gallery video');

videos.forEach(video => {
    video.addEventListener('click', () => {
        if (video.paused) {
            video.play();
        } else {
            video.pause();
        }
    });
});
</script>