0

I have a page that loads content from a mysql db and I need the iframes to load after the main page as there are over 20 iframes and it slows down the page significantly.

<?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);

?>

I have tried many of the scripts online to no avail.

1 Answers1

1

PHP is processed server-side, which means you can't tell portions of it to wait until the some of the page has loaded in the browser. For that, you could use Javascript (I'm using jQuery in this example):

<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>

Also - mysql_* functions (such as mysql_connect()) are deprecated - you should definitely switch to PDO or mysqli. (Why shouldn't I use mysql_* functions in PHP? )

lookdad
  • 98
  • 7