php - Bootstrap Modal with mysql data -
i have html table list of product name , id, upon clicking product name link wanted open modal , show item respect id.
i wanted pass $code model , retrieve data. how should do?
my code below..
<a href="#mymodal" data-toggle="modal" data-target="#mymodal" data-code="@<? echo $code; ?>">product 1</a> <a href="#mymodal" data-toggle="modal" data-target="#mymodal" data-code="@<? echo $code; ?>">product 2</a> <div class="modal fade" id="mymodal" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="mymodallabel">modal title</h4> </div> <div class="modal-body"> <? $code_id = isset($_get['code']) ? $_get['code'] : false; $result = $db->prepare("select * $tbl_name id=:code limit 1"); $result->bindvalue(':code', $code_id, pdo::param_str); $result->execute(); $row = $result->fetch(pdo::fetch_assoc); $unit = $row['unit']; $name = $row['name']; $price = $row['price']; ?> <table class="table"> <tr> <th style="text-align: center;">#</th> <th style="text-align: center;">unit</th> <th style="text-align: center;">product name</th> <th style="text-align: center;">price</th> </tr> <tr> <td><? echo $i; ?></td> <td><? echo $unit; ?></td> <td><? echo $name; ?></td> <td><? echo $price; ?></td> </tr> </table> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">close</button> </div> </div> </div>
i realized logan's answer wouldn't work because targeting classes instead of ids. data-target
each link modal should unique id. created variable $uid (unique identifier), , initialised 1 (alternatively use primary key). each modal have id of mymodal+$uid
, , each link point specific modal's id.
<?php $code_id = isset($_get['code']) ? $_get['code'] : false; $result = $db->prepare("select * $tbl_name id=:code limit 1"); $result->bindvalue(':code', $code_id, pdo::param_str); $result->execute(); //checks if there results before sending while loop if ($result->num_rows > 0) { $uid = 1;//alternatively can use primary key table while($row = $result->fetch_assoc()){ ?> <a href="#mymodal" data-toggle="modal" data-target="#mymodal<?php echo $uid; ?>" data-code="@<? echo $code; ?>">product <?echo $uid?></a> <!-- start modal, realise id of each modal --> <div class="modal fade" id="mymodal<?php echo $uid; ?>" tabindex="-1" role="dialog" aria-labelledby="mymodallabel" aria-hidden="true"> <div class="modal fade"> <!--the rest of code--> </div> <?php $uid = $uid +1;//increase uid }// end while }//end if statement ?>
Comments
Post a Comment