I have few thumbs on the page which are loaded from mysql database. Now I want to make button view on them and when I click to open modal windows with more info.
The problem is that always is opening first item on each thumb(different id). This is what I've using. I guess the problem is in the button but can't figured out how to fix it.
foreach($pdo->query("SELECT * FROM projects ORDER BY project_id") as $row)
{
echo '<div class="portfolio-item '.$row['project_category'].'">';
echo '<div class="portfolio">';
echo '<a href="#" data-lightbox-gallery="portfolio">';
echo '<img src="'.$row['project_image'].'" alt="Portfolio 1">';
echo '<div class="portfolio-overlay hvr-rectangle-out">';
echo '<h2 class="margin-bottom-small">
<strong class="white-color bold-text"></strong>
</h2>';
echo '<div class="button" data-toggle="modal" data-target="#portfolio-4">View More</div>';
echo '</div><!-- END PORTFOLIO OVERLAY -->
</a>
</div>
<div class="modal fade contact-form" id="portfolio-4" tabindex="-1" role="dialog" aria-labelledby="portfolio-4" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<div class="modal-body member-info">
<div class="row">
<div class="col-md-5 col-sm-5">
<figure>
<img src="'.$row['project_image'].'" alt="">
</figure>
</div>
<div class="col-md-7 col-sm-7">
<div class="description">
<h3><strong class="bold-text">'.$row['project_overlay'].'</strong></h3>
<div class="light-text">'.$row['project_title'].'</div>
<div class="about margin-top-medium">
<p>'.$row['project_details'].'</p>
</div>
</div> <!-- *** end description *** -->
</div> <!-- *** end col-md-7 *** -->
</div> <!-- *** end row *** -->
</div> <!-- *** end modal-body *** -->
</div> <!-- *** end modal-content *** -->
</div> <!-- *** end modal-dialog *** -->
</div> <!-- *** end Contact Form modal *** -->
</div> <!-- *** end portfolio-item *** -->';
}
At a simple glance (I can't tell what plugin you are using) I would check this line : ... div class="modal fade contact-form" id="portfolio-4"
the button triggers this modal data-target="#portfolio-4">
So I would get the index of every row and append it to the id of the modal, then in the button data-target I would also append that index... (so IDs are different) Hope it helps, I did the same but using bootstrap.
Related
Good day. In my Laravel App, I have a foreach loop where I display all products. I also have a quickview modal where users can view the products without loading a new page. Furthermore, I am using owlcarousel.js to display the products. Unfortunately, owl.carousel breaks the bootstrap modal unless the modal is placed outside the owl.carousel. But, since the products are loaded on the page dynamically, I can't access the ids of the modals outside the owl carousel div which encompasses the loop.
The $modal_id variable is only returning the last Id in the loop. So, please what is the way out?
How can I access the individual ids within the loop from on click from outside
The code is shown below
<div class="product owl-carousel">
<?php $modal_id = 0; ?>
#foreach($new_arrivals as $new_arrival)
<div class="pro">
<div class="pro-img">
<?php $modal_id = $new_arrival->id?>
<span class="sticker-new">new</span>
<div class="quick-view-pro">
<a data-toggle="modal" data-target="#{{$modal_id}}" class="quick-view modal-view"
href="#{{$modal_id}}" rel="{{$new_arrival->id}}"></a>
</div>
</div>
</div>
#endforeach
</div>
<!-- Modal is shown below !-->
<div class="product-view">
<div class="container">
<div class="modal fade" id="{{$modal_id}}">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="row">
</div>
</div>
<!-- Modal footer -->
</div>
</div>
</div>
</div>
</div>
Try this code:
<div class="product owl-carousel">
<?php $modal_id = 0; ?>
<?php $modal_ids = []; ?>
#foreach($new_arrivals as $new_arrival)
<div class="pro">
<div class="pro-img">
<?php $modal_id = $new_arrival->id?>
<?php $modal_ids[] = $new_arrival->id ?>
<span class="sticker-new">new</span>
<div class="quick-view-pro">
<a data-toggle="modal" data-target="#{{$modal_id}}" class="quick-view modal-view"
href="#{{$modal_id}}" rel="{{$new_arrival->id}}"></a>
</div>
</div>
</div>
#endforeach
</div>
<!-- Modal is shown below !-->
#foreach($modal_ids as $item)
<div class="product-view">
<div class="container">
<div class="modal fade" id="{{$item}}">
<div class="modal-dialog modal-lg modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<div class="row">
</div>
</div>
<!-- Modal footer -->
</div>
</div>
</div>
</div>
</div>
#endforeach
Hope help you.
A PHP function loops through an array of objects and lists down their titles. I have a link that opens a modal. How can I dynamically load the modal header and body with regard to the object $r from the $results when I press details?
<?php
foreach ($results as $r) {
?>
<div class="row justify-content-center">
<div class="alert alert-success">
<strong>
<?php echo $r['_source']['title']; ?></strong> <span id="showSearchTerm"></span></br>
<a data-target="#myModal" data-toggle="modal" class="MainNavText" id="MainNavHelp" href="#myModal">Details</a>
</div>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<!-- <button type="button" class="close" data-dismiss="modal">×</button> -->
<h2 class="modal-title">
<?php echo $r['_source']['title']; ?>
</h2>
</div>
<div class="modal-body">
<p>
<?php echo $r['_source']['body']; ?>
</p>
<p>
<?php echo $r['_source']['path']['real']; ?>
</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php
}
Take the information you need to fill up the modal from an Ajax call. After that you can add that content with:
$(".classname").append(information_from_Ajax_call_stored_in_variable);
I have searched for this answer, I tried a lot of things but it is still not working...
Here below I'll give you the code. Please, I will be so grateful to the person that will help me. Thank You.
I will try to explain to you what I did. Basically, I linked the Post ID to the modal window to display the specific post content on a specific modal window. Seems like it might work, but nope. I realized that the modal id link does not change while the Link it is changing... I don't what to do...
Here Below I Will Give you a screenshot.
Sorry If I made a mistake explaining you all this..:)
PHP Code:
<div class="col-lg-4 col-md-4 col-sm-12 col-xs-12 align-self-start">
<div class="client-opinion-box text-center">
<p class="client-opinion-box-txt text-center"><?php the_excerpt(); ?></p>
*** <a class="btn btn-primary" id="clickme" data-toggle="modal" data-id="<?php the_ID(); ?>" href="#myModal-<?php the_ID(); ?>">читать полностью>>
</a> ***
<h4 class="client-opinion-box-sub_header"><?php the_title(); ?></h4>
</div>
</div>
<?php
}
wp_reset_postdata(); // сброс
?>
</div>
<div class="row justify-content-center">
<button id="opinion-btn" class="main-button opinion-btn btn btn-primary" type="button" data-toggle="collapse" data-target="#collapsed-opinion-leave" aria-expanded="false" aria-controls="collapsed-opinion-leave">Добавить отзыв</button>
</div>
<div class="row opinion-leave-box collapse" id="collapsed-opinion-leave">
<div class="col-lg-6 col-md-12 col-sm-12 col-xs-12">
<h3 class="opinion-leave-box-header">Оставьте свой отзыв</h3>
<?php
echo do_shortcode('[contact-form-7 id="144" title="Контактная форма (ОТЗЫВЫ)"]');
?>
</div>
<div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 opinion-alignment">
<img src="/wp-content/themes/Rodoslov-theme/assets/img/Opinions/family-tree-opinion.png" class="img-responsive opinion-leave-img" alt="Family Tree">
</div>
</div>
</div>
</section>
<!-- Modal Bootstrap -->
<!-- Modal -->
*** <div class="modal fade" id="myModal-<?php the_ID(); ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> ***
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle"><?php the_title(); ?></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
JS code:
$(document).ready(function(){
$("#clickme").on
("click", function(){
$("#myModal-" + $(this).attr('data-id')).modal();
});
});
Picture:
enter image description here
I am trying to open bootstrap pop for every row in table .It works if I keep the
onclick event in loop and pop up window in loop for getting different id ie photoModal.But for some reason i have to keep the pop up window out of loop.
<!-- Image toggle box start-->
<div class="bs-example">
<div id="photoModal" class="modal fade">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">rudra pop 2 </h4>
</div>
<div class="modal-body"> <img src="<?php echo URL;?>images/decor/Int-Design2.jpg" style="max-width:100%; " /> </div>
</div>
</div>
</div>
</div>
<!-- End Image toggle box start-->
<a href="#" data-toggle="modal" data-target="#photoModal" data-title="Feedback"
style="border:none !important; text-decoration:none !important;">
<div class="tz-image-item">
<img alt="" src="images/decor/Int-Design1.jpg">
</div>
</a>
Use Javascript to open your modal dialog. That should help you.
$(document).ready(function() {
$('tr').on('click', function(ev) {
$('#my-modal').modal('show');
});
});
Briefly speaking, my site has a Bootstrap modal, which is used for display a Youtube video, like following
<!-- Video dialog-->
<div class="modal fade" id="vid-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<div class="modal-title">
<h4 id="modal-title">Modal title</h4>
</div>
</div>
<div class="modal-body">
<div class="modal-video">
<iframe src=""></iframe> <!-- Code for video here -->`
</div>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- End video dialog-->
This modal is displayed each time user clicks on a link. Those links are loaded from database using PHP with this code:
<div class="panel-group" id="accordion">
<?php
$v_units = $courses->get_distinct_unit($id);
foreach ($v_units as $v_unit) { ?>
<div class="panel-info">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" href="#collpase<?php echo $v_unit['unit_id'];?>"><?php echo $v_unit['unit_id'] . ' - ' . $v_unit['unit_name']; ?></a>
</h4>
</div> <!-- End of .panel-heading-->
<div id="collpase<?php echo $v_unit['unit_id'];?>" class="panel-collapse collapse in">
<div class="panel-body">
<?php $v_vids = $courses->get_unit_video($v_unit['unit_id']); ?>
<?php foreach ($v_vids as $v_vid): ?>
<a class="show-vid-modal" href="<?php echo $v_vid['vid_link']; ?>"><?php echo $v_vid['vid_title'] ?></a>
<?php endforeach ?>
</div> <!-- End of .panel-body -->
</div> <!-- End of .panel-collapse -->
</div> <!-- End of .panel-default-->
<?php }?>
</div> <!-- End of #accordion-->
The a class="show-vid-modal" ... triggers the modal.Modal's title and video link is fetched through PHP for each item.
I have added the following javascript code.
<script>
$('.show-vid-modal').click(function(e) {
e.preventDefault();
$("#vid-modal").on('show.bs.modal', function(e) {
$("h4 #modal-title").text("<?php echo $v_vid['vid_title'];"?>);
$("div .modal-video").innerHTML = "<iframe src="<?php echo $v_vid['vid_link'];?>" "
});
$("#vid-modal").modal();
});
</script>
However, it only shows the title and video of the last item for every item (as the foreach loop ends). If I put the javascript inside foreach loop, then it will not work.
Can anyone give me an idea on how I should do that.
In your modal, put an id for the iframe, so it'll be:
<iframe id="video-frame" src=""></iframe>
In your link, create a custom attribute, let's say video-src, so it'll be:
<a class="show-vid-modal" href="#" video-src="<?php echo $v_vid['vid_link']; ?>"><?php echo $v_vid['vid_title'] ?></a>
Once you have it, put in your button's click function:
$('.show-vid-modal').click(function(e) {
//here the attribute video-src is helpfull
var vid-src = $(this).attr('video-src');
var title = $(this).html();
e.preventDefault();
$("#vid-modal").on('show.bs.modal', function(e) {
$("h4 #modal-title").text(title);
//here the id for the iframe is helpfull
$('#video-frame').attr('src', vid-src);
});
$("#vid-modal").modal();
});
It might work.