Home > other >  If / else statement in PHP to show multi-format video in HTML markup
If / else statement in PHP to show multi-format video in HTML markup

Time:12-22

I want to show multi-format videos on my index.php or index.html

I set videos source/src as a variable on my index.php like below

<?php 
$video_1080p = "../videos/abcd-1080p.mp4";
$video_720p = "../videos/abcd-720p.mp4";
$video_480p = "../videos/abcd-480p.mp4";
$video_360p = "../videos/abcd-360p.mp4";
$video_240p = "../videos/abcd-240p.mp4";

// also add HD filter variable
$hd = "data-fluid-hd";
?>

This is my HTML markup and i want to show videos, according to the video source, and HD filter. And also remove <source> tag if any video variables empty.

<html>
<body>
<video id="123" controls style="width: 100%; height: auto;">

<source data-fluid-hd src='../videos/abcd-1080p.mp4' title='1080p' type='video/mp4' />

<source data-fluid-hd src='../videos/abcd-720p.mp4' title='720p' type='video/mp4' />

<source src='../videos/abcd-480p.mp4' title='480p' type='video/mp4' />

<source src='../videos/abcd-360p.mp4' title='360p' type='video/mp4' />

<source src='../videos/abcd-240p.mp4' title='240p' type='video/mp4' />

</video>
</body>
</html>

How to do it by if else statement or any other ways?
All videos are raw files (converted with FFmpeg).

CodePudding user response:

You can add to Array and loop it instead of writing multiple if statements.

$videos = array(
  array('src' => $video_1080p, 'title' => '1080p', 'hd' => $hd),
  array('src' => $video_720p, 'title' => '720p', 'hd' => $hd),
  array('src' => $video_480p, 'title' => '480p'),
  array('src' => $video_360p, 'title' => '360p'),
  array('src' => $video_240p, 'title' => '240p')
);

foreach ($videos as $video) {
  if (!empty($video['src'])) {
    echo "<source";
    if (!empty($video['hd'])) echo " " . $video['hd'];
    echo " src='" . $video['src'] . "' title='" . $video['title'] . "' type='video/mp4' />";
  }
}
  • Related