简体   繁体   English

如何在 php 中进行链接共享

[英]How to make link sharing in php

Videos come from a folder and are automatically displayed on my website.视频来自一个文件夹,并自动显示在我的网站上。 But I am having a problem at implementing share feature.但是我在实现共享功能时遇到了问题。 All I want to do is when share button is pressed, video's whole path like https:\\example.com\vid.mp4 should show up.我想要做的就是按下共享按钮时,应该显示视频的整个路径,如https:\\example.com\vid.mp4 I tried but it just shows the location of very last video on my page.我试过了,但它只显示了我页面上最后一个视频的位置。

My php:我的 php:

    $exten = '.mp4';
     $dir = __DIR__ . DIRECTORY_SEPARATOR . "gallery" . DIRECTORY_SEPARATOR;
     $videos = glob("$dir*.{mp4}", GLOB_BRACE);
   $alt = glob("$dir*.{webm,mp4,ogg}", GLOB_BRACE);
     if (count($videos) > 0) { foreach ($videos as $vid) {
        $base = basename($vid,'.mp4');
       printf("<div class='vi'><video src='gallery/%s' id='basename($vid,'.mp4')' loop='loop'></video>", rawurlencode(basename($vid)));
       echo '<div class="sh"><button class="share" onclick="phprun()">SHARE</button></div>' ;
     
     echo "<div class='title'>".basename($vid,'.mp4')."</div></div>";
}}
  ?>

My js:我的js:

var result ="<?php echo('http://victure.freecluster.eu/gallery/'.basename($vid)); ?>"
const copy = document.getElementById("copy");
 const h1 = document.getElementById("h1");
 const h2 = document.getElementById("h2");
 
function phprun(){
 if (copy.style.display === "none") {
    copy.style.display = "block";
  } else {
    copy.style.display = "none";
  }
  h1.innerHTML="SHARE:  "+"<?php echo(basename($vid,'.mp4')); ?>";
  h2.innerHTML=result;
//  alert(result)
}

It makes a div appear with video's path but it is showing the path of only one video.它使 div 与视频的路径一起出现,但它只显示一个视频的路径。

Note: Keep in mind it is automated.注意:请记住,它是自动化的。

In short: How to display path of each video?简而言之:如何显示每个视频的路径?

You can see working problem here: Problem你可以在这里看到工作问题:问题

Thanks in advance:)提前致谢:)

What you need is to get the onclick target (like explained here ) in order to make your content dynamic .您需要的是获得 onclick 目标(如此所述)以使您的内容动态化

1. 'onclick' event: pass the 'this' parameter to your function phprun() . 1. 'onclick' 事件:将 'this' 参数传递给您的 function phprun() For an event, 'this' as a parameter refers to the target of the event (the div on which the event has been fired).对于事件,“this”作为参数是指事件的目标(触发事件的 div)。

<div class="vi">
   <video src="..." id="..." loop="..."></video>
   <div class="sh">
   <button class="share" onclick="phprun(this)"> // <-----(GET THE EVENT TARGET)
      <svg class="..."></svg>
   </button></div>
   <div class="title">Demo from ShazamBolt8</div>
</div>

2. In your function: inject the event target ( phprun(target){...} ) then use it in your logic. 2. 在您的 function 中:注入事件目标( phprun(target){...} )然后在您的逻辑中使用它。 Example of code for your function: function 的代码示例:

function phprun(target) { // <-----( INJECT THE EVENT TARGET)

    // get the video element from the target
    let videoEl = target.parentNode.parentNode.childNodes[0];

    // retrieve the data you want (eg. the video url and title)
    let videoUrl = videoEl.getAttribute('src');
    let videoTitle = videoEl.documentQuerySelector('.title');

    // inject it into the desired containers
    h1.innerHTML = 'Share:' + videoTitle;
    h2.innerHTML = videoUrl;

    // do more stuff...
    if (copy.style.display === "none") {
        copy.style.display = "block";
    } else {
        copy.style.display = "none";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM