Modal Bootstrap > While Loop > Open Modal Item

Asked

Viewed 2,013 times

1

I believe it’s a matter of putting the code in the right place, but I’m racking my brain and I can’t...

I made a test with the standard Bootstrap code, where it works on a button. I even put the data calls inside it and it worked.

The question now is, does my index return database values by while:

  while ($posts = mysql_fetch_array($busca)){
                extract($posts);
                ?>

                <div class="col-md-2"  >
                    <div class="item-title">
                        <h6 class="title">

                            <strong>
                                <a href="single.php?id=<?php echo $posts['id']?>" title="<?php echo $posts['titulo']; ?>" >
                                <?php echo $posts['titulo']; ?></a>
                            <strong>
                        </h6>
                    </div><!--Fecha titulo do posts/item-->


                    <div class="item-img" data-toggle="modal" data-target=".bs-example-modal-lg">
                        <a href="single.php?id=<?php echo $posts['id']?>" title="<?php echo $posts['titulo']; ?>" class="thumbnail">
                            <img src="uploads/<?php echo $posts['path'] ?>" title="<?php echo $posts['titulo']; ?>" alt=""  id="blc-img" />
                        </a>
                    </div><!--Fecha Imagem do posts/item-->

                    <div class="item-vist">
                        <h6 class="vist"><small>Visitas <b><?php echo $posts['visitas'] ?></small></b></h6>
                    </div><!--Fecha Visitas do posts/item-->

                </div>
                <?php
                    };
                ?>

How can I adapt this code so that when clicking on one of the items, it opens in Modal, and not in single.php? like this at the moment.

Modal code:`

<button type="button" class="btn btn-primary" data-toggle="modal"    data-target=".bs-example-modal-lg">Large modal</button>

    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      ...
    </div>
  </div>
</div>`

I will not modify the standard Modal code (I believe there is no need). I would like to click on one of the returned items, open in Modal.

Modal with working button: inserir a descrição da imagem aqui

My idea is when you click on the post (the one below Catalogo), it opens the same way as the button.

Thank you in advance!

  • Place the modal and the button that will call the modal inside the while and the respective bank retornor in each position.

  • @Rafaelacioly sorry, could you be a little more specific? in case I do not want to use the button to call the modal, and yes the own post as call to the modal...

  • you want when the bank query is performed the modal already appears?

  • @Rafaelacioly put a print of the modal with button. The ideal is to work when you click on the post (the cover that is below the word Catalogo)

  • @Henriquesilva, the Boostrap modal does not allow you to run multiple modals, so it is not loading the content you want, but you can create separate blocks and call them within a single modal, take a look at my answer: http://answall.com/questions/81050/como-usar-multiplas-modals-no-bootstrap/81095#81095

  • That picture in the catalog reminded me of Sephiroth in Final Fantasy VII

  • @Ivanferrer sorry, I don’t think I understand...in the logic of php, this would not be a single modal?

  • 1

    Yes, it is correct true, I had not noticed, but you have to send an id to each element of your loop to be loaded inside your modal, otherwise it will always carry only the first external div element of your modal. Look at this example: http://jsfiddle.net/Lyp615jw/5/

  • @Ivanferrer ! Thank you for your help. I was able to adapt your reference perfectly... I’m only with a small problem, when I click off the Modal it closes and the index page shrinks a little the right side, and if I keep entering other Modals and clicking outside it will shrink more... You know what it can be?

Show 4 more comments

2 answers

2


Create a function in js that you call it by passing the id of the movie, then this function calls its single.php for ajax passing the id and then loading the html from single.php within the modal and then opens the modal.

 function carregarItem(id)
    {
        $( ".modal-content" ).load( "single.php", { id: id }, function() {
            ('.bs-example-modal-lg').modal('show');  
        });
    }

Called HTML:

<strong>
   <a onclick='carregarItem(<?php echo $posts['id']?>)' title="<?php echo $posts['titulo']; ?>" >
   <?php echo $posts['titulo']; ?></a>
<strong>
  • Srs, in this case the only change would be using the call HTML and Function? Without the use of bootsstrap code? I didn’t understand

  • You should keep the html of your modal too, because js will update the content of the existing modal and after that, show on the screen.

  • exactly this part I get lost... the html of the modal bootstrap should be included in which location? I already put Function inside the script in the head, and already modified the a line with onclick

  • Where is the film listing, outside the loop.

  • Nothing happens... and with the html code for a onclick, it loses the "link" and I can’t click.

  • Tries to debug, in the first line of the type function console.log(id), see if the id arrives, and also for the .load work, you need the jquery.

Show 2 more comments

1

I imagine by what I understood that when clicking on the links you should make an ajax request to the url in case for the single.php from there take the return that should be a preferred html and play inside the modal div then open the modal. Now there is the problem of the parameter I think I would have to implement like this: Example: Your link:

<strong>
   <a class='linkmodal' id='<?php echo $posts['id']?>' title="<?php echo $posts['titulo']; ?>" >
   <?php echo $posts['titulo']; ?></a>
<strong>

//vinculei a solicitação ajax ao click do a e pego o id na propriedade do elemento

$('a.linkmodal').click(function(){
        $.ajax({
            url: 'single.php',
            data: {id: $(this).attr('id')}
        }).done(function(data) {
            $('div.modal-content').html(data);
        });

//aqui chamar abertura da modal codigo depende do plugin

});
  • Oops! Thanks for the comment. This part: //here call opening the modal code depends on the plugin ... vc refers to the code I placed from the bootstrap?

  • This comment refers to the opening of the modal for example "modal.show()" I don’t know which plugin you’re using. But the function of @henriquedpereira down there became more elegant.

  • Actually as I never worked with JS or Jquery, I don’t know which one to use... I’m trying to adapt what I find on http://getbootstrap.com/javascript/#modals-methods

  • I edited his answer to suit you well, I think it’s more elegant, but I think the two ways will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.