PHP - Get different database data from the same button

2

Well the problem is this: I have the following code HTML/PHP which allows me to go fetch the last four data entered in the database in order 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 so good, it works perfectly. But as you can see in the code when loading the tag name showDescr image, a modal appears that will show the product description.

The problem is that I can not do the following modal code within the while cycle, otherwise it will show all results at once (overlapping) and out will only show one. The modal code is as follows:

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

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

name="<?php echo $idProd ?>"
    
asked by anonymous 27.09.2016 / 04:03

2 answers

0

I was able to solve the problem. Then I did the following: In the button (which in this case is an image) to open the modal in the data-target attribute, in addition to the name that assigns the modal , I also gave the id from the search in the database. And in the code of modal I created a id attribute and gave it its name so that the target works.

Code changed:

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

Q: The code of modal must be within the while (shown in the description of the question above) to iterate over the various existing id's

    
01.10.2016 / 03:34
1

read the data uses a function in PHP to read the id with the Post variable with MySQL query using the id to target the content you want. I have to run it but in Lavarel .

You verify the insertion with: form

    
27.09.2016 / 17:14