javascript/ php/ html/ magnific-popup

I am trying to do a dynamic content magnific popup.

This is my current code.

if ($queryResult > 0) {
while ($row = mysqli_fetch_assoc($result)) {
  echo "<div class='hvrbox'>
     <img src='image/".$row['mImage']."' alt='' class='hvrbox-layer_bottom' />
     <div class='hvrbox-layer_top'>
       <a href='#posterVid1' class='posterbtn1' id='posterLink1'>Play Trailer</a>
       <div id='posterVid1' class='mfp-hide' style='max-width: 75%; margin: 0 auto;'>
           <iframe width='853' height='480' src='".$row['mLink']."' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>
       </div>
       <a class='posterbtn2' href='movie-detail.php?name=".$row['mName']."'>Movie Details</a>
       <div class='hvrbox-text'>Me Before You</div>
     </div>
   </div>";
      }
    }

This is the javascript

$('#posterLink1, #posterLink2, #posterLink3')
.magnificPopup({
      type:'inline',
      midClick: true // Allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source in href.
  }) 

Since the magnific popup I'm using only opens content based on the id given in the id tag and in the JavaScript, if I have different content, the id still stays the same and the button only opens the same content over and over again because the id is the same. Because the content I'm getting is from a database, I can't replicate the code over and over again and only change the id like in a static page.

What am I doing wrong?

The issue is that you don't have a counter in your loop. Also you don't need the id for calling the magnificPopup , instead use class for dynamic.

PHP:

if ($queryResult > 0) {
    $ctr = 1;
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<div class='hvrbox'>
            <img src='image/".$row['mImage']."' alt='' class='hvrbox-layer_bottom' />
            <div class='hvrbox-layer_top'>
                <a href='#posterVid".$ctr."' class='posterbtn1'>Play Trailer</a>
                <div id='posterVid".$ctr."' class='mfp-hide' style='max-width: 75%; margin: 0 auto;'>
                    <iframe width='853' height='480' src='".$row['mLink']."' frameborder='0' allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>
                </div>
                <a class='posterbtn2' href='movie-detail.php?name=".$row['mName']."'>Movie Details</a>
                <div class='hvrbox-text'>Me Before You</div>
            </div>
        </div>";
        $ctr++;
    }
}

Javascript:

$('.posterbtn1')
    .magnificPopup({
        type:'inline',
        midClick: true // Allow opening popup on middle mouse click. Always set it to true if you don't provide alternative source in href.
    }) 

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Magnific popup Dynamic content Arrows not displaying in Magnific Popup gallery Mixed content type in magnific popup Magnific Popup with Wordpress Image (php content) Magnific popup closes on content click after replacing content Magnific popup close when i click on ajax content Hide nav content when Magnific-popup is shown Magnific Popup iframe auto resize on content change (cross domain) Magnific popup - How can I fit inline content vertically? Owl Carousel with Magnific Popup - Popup not even displaying, not sure what I'm doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM