嗨,我大部分时间都花在了这一天,并转向你,希望有人可以提供帮助。我需要在页面上生成一个随机显示5-10个嵌入式youtube vids而不会重复。当我刷新它时,我可以显示视频(但是所有相同的艺术家),它会显示一组新的视频,但同样是所有相同的艺术家。我知道答案可能很简单,但我看不到它。
非常感谢任何帮助或指示。
最初的PHP代码是:
$result = mysql_query("SELECT Youtube FROM artist ORDER BY RAND() LIMIT 5");
$test = mysql_fetch_array($result);
if (!$result)
{
die("Error: Data not found..");
}
$Youtube=$test['Youtube'];
输出为:
<div class="ws_images"><ul>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<? php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
<iframe width="300" height="300" src="https://www.youtube.com/embed/<?php echo $Youtube ?>" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
</ul></div>
答案 0 :(得分:0)
这应该有效。我改变了你的代码,并通过while循环减少了它。
<?php
$mysqli = new mysqli("127.0.0.1", "root", "password", "database name");
$mysqli->set_charset('utf8mb4'); //optional for when you use utf8mb4 charset (recommended)
$getYouTubeLinks = "SELECT DISTINCT Youtube FROM artist ORDER BY RAND() LIMIT 5";
$getRows = $mysqli->query($getYouTubeLinks);
if($getRows->num_rows > 0) {
while($row = $getRows->fetch_assoc()) {
$iframes .= '<iframe width="300" height="300" src="https://www.youtube.com/embed/'.$row['Youtube'].'" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe><br>';
}
}
?>
<div class="ws_images"><ul>
<?=$iframes;?>
</ul></div>
答案 1 :(得分:0)
$ result = mysql_query(&#34; SELECT DISTINCT Youtube FROM artist ORDER BY RAND()LIMIT 5&#34;);