PHP - Get different database data from the same button

Asked

Viewed 91 times

1

Well, here’s the problem: I have the following code HTML/PHP which allows me to retrieve the last four data entered in the database to show the last ones added:

$getLastFour = mysqli_query($dbc,"Select * From products Order By id_product DESC LIMIT 4") or die(mysqli_error($dbc));

while($row = mysqli_fetch_array($getLastFour)){
        $idProd = $row["id_product"];
        $name = $row["name_Product"];
        $price = $row["prod_price"];
        $description = $row["prod_description"];

?>
<div class="col-md-2">
            <div class="grid">
                <div class="portfolio app mix_all" data-cat="app" style="display: inline-block; opacity: 1;">
                    <div class="portfolio-wrapper">
                        <a data-toggle="modal" data-target=".bs-example-modal-md" href="#" class="b-link-stripe b-animate-go  thickbox">
                            <img src="images/cont1.jpg"/>
                            <div class="b-wrapper"><h2 class="b-animate b-from-left b-delay03 ">
                                <img src="images/link-ico.png" class="img-responsive" name="showDescr"  alt=""/></h2>
                            </div>
                        </a>
                    </div>
                </div>
                <p class="text-center image" style="color: #990000;"><b><u><?php echo $name ?></u></b></p>
                <h2 class="text-center image"><b><?php echo $price.'€' ?></b></h2>
                <p class="text-center button" ><a href="details.html">Comprar</a></p>
            </div>
        </div>
<?php
        }
        ?>

So far everything works perfectly. But as you can see in the code when clicking on the tag name image showDescr supposedly appears a modal that will show the product description.

The problem is that I cannot the following modal code within the cycle while otherwise it will show all the results at the same time (in supbreposição) and outside will show only one. The modal code is following:

<!-- modal start -->
        <a data-toggle="modal" data-target=".bs-example-modal-md" href="#"> </a>
        <div class="modal fade bs-example-modal-md light-box" tabindex="-1" role="dialog"
             aria-labelledby="mySmallModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-md">
                <div class="modal-content light-box-info">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><img
                            src="images/close.png" title="close"/></button>
                    <h3>Descrição do Produto</h3>
                    <p><?php echo $description ?></p>
                </div><!-- end modal content -->
            </div><!-- end modal dialog -->
        </div><!-- end modal fade -->

Will I have to give a different name to the image that when clicking will appear the modal in the example tag name:

name="<?php echo $idProd ?>"
  • How are you loading the modal ? Only with link or ajax ?

  • With the use of data target, bootstrap

2 answers

0

uses a form with input Hidden with value "id" on the button where you want to open the modal and on the modal to read the data use a PHP that reads the id with the variable Post with Mysql query using id to target the content you want. I have it working but in Lavarel.

check the insertion with: if(isset($_POST['minha_modal'])){} e no form coloca assim : "> #

0


I managed to solve the problem. So I did the following: On the button (which in this case is an image) to open the modal in the attribute data-targetin addition to the name you assign to modal I also gave him the id from the database search. And the modal created an attribute id and gave it its name so that the target works.

Altered code:

<div class="b-wrapper"><h2 class="b-animate b-from-left b-delay03 ">
                                <img src="images/link-ico.png" data-toggle="modal" data-target="#MyModal<?php echo $idImage; ?>" class="img-responsive"  alt=""/></h2>
                            </div>

<div class="modal fade bs-example-modal-md light-box" tabindex="-1" role="dialog"
             aria-labelledby="mySmallModalLabel" aria-hidden="true" id="MyModal<?php echo $idImage;?>">
            <div class="modal-dialog modal-md">
                <div class="modal-content light-box-info">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><img
                            src="images/close.png" title="close"/></button>
                    <h3>Descrição do Produto</h3>
                    <p><?php echo $description ?></p>
                </div><!-- end modal content -->
            </div><!-- end modal dialog -->
        </div><!-- end modal fade -->

P.S: The code of modal must be inside the while (presented in the question description above) to iterate on the various existing id’s

Browser other questions tagged

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