我有一个从mysql数据库加载内容的页面,我需要在主页面后加载iframe,因为有超过20个iframe并且它显着减慢了页面速度。
<?php
$con = mysql_connect("localhost","####","####");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("daniel_login", $con);
$result = mysql_query("SELECT * FROM replays ORDER BY id DESC");
while($row = mysql_fetch_array($result))
{
echo "<div class=\"col-sm-6 col-xs-12 masonry__item\" data-masonry-filter=\"" . $row['category'] . "\">";
echo "<div class=\"portfolio-item portfolio-item-2 video-cover\" data-scrim-bottom=\"9\">";
echo "<div class=\"background-image-holder\"><img src=\"../img/" . $row['image'] . "\" /></div>";
echo "<div class=\"portfolio-item__title\"><h5>" . $row['h1'] . "</h5><span><em>" . $row['h2'] . "</em></span></div><div class=\"video-play-icon video-play-icon--sm\"></div>";
echo "<div id=\"dna_video\"></div>";
echo "<iframe src=\"" . $row['video'] . "?autoplay=0\" allowfullscreen=\"allowfullscreen\"></iframe></div></div>";
}
mysql_close($con);
?>
我在线尝试了很多脚本无济于事。
答案 0 :(得分:0)
PHP在服务器端处理,这意味着你不能告诉它的某些部分要等到浏览器中加载了一些页面。为此,你可以使用Javascript(我在这个例子中使用jQuery):
<script type="text/javascript">
var links = [];
<?php
//Create DB connection $con - I use PDO
$result = $con->query("SELECT * FROM replays ORDER BY id DESC");
while($row = $result->fetch()){
echo 'links.push("'.$row['video'].'");'; //get all urls
//links.push("video-link");
}
?>
$(function(){ //wait until page loads
for(var n=0;n<links.length;n++){
$('body').append($('<iframe>').attr('src',links[n])); //loop through links and add to page as iframe
}
});
</script>
另外 - mysql_*
函数(例如mysql_connect())已弃用 - 您绝对应该切换到PDO或mysqli。 (Why shouldn't I use mysql_* functions in PHP?
)