Creating Dynamic Modal popups with information from job - php

I have a PHP loop that renders results from a Database:
<?php
foreach ($results as $result){
$job_numb = $result['job_numb'];
$job_name = $result['job_name'];
$comments = $result['comments'];
?>
<tr>
<td><?php echo "$job_numb";?> </td>
<td><?php echo "$job_name";?></td>
<td> <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#Comments-<?php echo $job_numb;?>">Comments</button>
<!-- Modal -->
<div id="Comments-<?php echo $job_numb;?>" 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>
<h4 class="modal-title">Comments for <?php echo "$job_name" . "$job_numb";?></h4>
</div>
<div class="modal-body">
<?php echo "$job_name";?>
<?php echo "$comments";?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</td>
<td>Edit Record</td>
</tr>
<?php
}
?>
The Data target for the modal cannot be a STATIC ID because then it's repeating IDs on the same page - so you will just repeat the same information as the first instance of the ID.
What I am after is to make sure the ID is always different for as many records as there are. I have seen the code before, but I cannot find it now. What am I missing?

Create a unique ID using <?php echo and a unique ID:
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#Comments-<?php echo $job_numb;?>">Comments</button>
<div id="Comments-<?php echo $job_numb;?>" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Comments for <?php echo "$job_name" . " | " . "$job_numb";?></h4>
</div>
<div class="modal-body">
<?php echo "$comments";?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
Using this, I was able to pass all of that dynamic row's specific data to the modal without ID conflicts. I hope this is useful to others seeking out the answer.

Related

bootstrap modal inside a php while loop to display different infornmation

so i have a while loop and i am fethcing the name and the description of them from my db
so when ever i click on one of the parts i want the modal to display the name of the item that i clicked on in the modal i hope this picture would describe it better
below is the code i have so far
at the moment when i click on the modal i get displayed the name of the item which is first on the list no matter where i click
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<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>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
You are not doing things the way you have to do. You are re-creating your mymodal for every iteration of your while loop which is not a better way to achieve what you want.
Follow these steps:
Create your mymodal outside of your while loop.
Pass the ID of current row to a javascript function and populate the data of that id using javascript.
I have set the things which needs to be done. Try the following code
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!--- display table header row-->
<table style="width:100%">
<tr>
<th>name</th>
<th>description</th>
<th>Action</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description'];
$id = $row['id']; // I am assuming that your table has auto incremented primary key column by the name of id, if not then replace that by $row['your_id_column']
?>
<!--- desplay the data in a table row -->
<tr>
<td id="<?php echo "name_".$id; ?>"><?php echo $name; ?></td>
<td id="<?php echo "description_".$id; ?>"><?php echo $description ; ?></td>
<td><div href="#" data-toggle="modal" data-target="#mymodal" onClick="showModal(<?php echo $id ; ?>)">Edit</div></td>
</tr>
<?php } ?>
</table>
<!--- Your 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>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p id="name"></p> <!--- name will be shown here-->
<p id="description"></p><!--- description will be shown here-->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
function showModal(id)
{
// setting the values of clicked item in the bootstrap Modal
$("#name").text($("#name_"+id).text());
$("#description").text($("#description_"+id).text());
}
</script>
Looks like you are not serializing the modals... If you wanted to do things that way your code should be something like;
<!-- language-all: lang-html -->
while($row = mysqli_fetch_assoc($result)) {
$procuct_id = $row['ID'];
$name = $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#mymodal_<?php echo $procuct_id;?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="mymodal_<?php echo $procuct_id;?>" 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>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }
<--
A Better way to do it would be to write a javascript function to show the generic modal window, then run an ajax call to load your info into the "modal-body" div. You would have to pass a unique id to the call in order to have your ajax script hit the db and get the info to display.
It's been almost 4 years but I'm writing for everyone; change data-target(id) as row id.
edited code:
while($row = mysqli_fetch_assoc($result))
{
$name= $row['name_of_product'];
$description = $row['description']
?>
<a href="#" data-toggle="modal" data-target="#<?php echo $row['id']; ?>">
<div class="col-sm-4">
<?php echo name; ?>
<?php echo description ; ?>
</div>
</a>
<div id="<?php echo $row['id']; ?>" 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>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p><?php echo $name; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>

How to get 'id' from table to modal to delete it with php

Hi I'm a super begginer in web developing. I need to take my table ID to a modal in order to delete the mySQL record. This is my code:
<?php while($llenartabla = $getsibella->fetch_assoc()) { ?>
<tr>
<th scope="row"><?=$llenartabla['id']?></th>
<td><?=$llenartabla['date']?></td>
<td><?=$llenartabla['location']?></td>
<td><?=$llenartabla['city']?></td>
<td><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal">Delete</button></td>
</tr>
<?php }?>
And here is my code for the modal:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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">Delete</h4>
</div>
<div class="modal-body">
Are you sure you want to delete the event?
</div>
<div class="modal-footer">
Delete
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I know it's kind of an easy question, but I'm learning bootstrap and php haha. So any answers or comments are welcome :p
Here's a newbie solution for you:
<?php while($llenartabla = $getsibella->fetch_assoc()) { ?>
<tr>
<th scope="row"><?=$llenartabla['id']?></th>
<td><?=$llenartabla['date']?></td>
<td><?=$llenartabla['location']?></td>
<td><?=$llenartabla['city']?></td>
<td><button type="button" class="btn btn-danger" data-toggle="modal" data-target="#myModal<?=$llenartabla['id']?>">Delete</button></td>
</tr>
<?php }?>
just changed the following part: data-target="#myModal<?=$llenartabla['id']?>"
As for modal: again wrapped around the same loop with changed id of modal id="myModal<?=$llenartabla['id']?>" and your delete url modified delete.php?id=$llenartabla['id']
<?php while($llenartabla = $getsibella->fetch_assoc()) { ?>
<div class="modal fade" id="myModal<?=$llenartabla['id']?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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">Delete</h4>
</div>
<div class="modal-body">
Are you sure you want to delete the event?
</div>
<div class="modal-footer">
Delete
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php }?>
Now if you want the advance solution you need javascript.

pass multiple php variables to modal

i want to pass multiple php variables to my modal. this how I am passing my variables but i also want to pass $idd and $gift variable to the modal
$gift ="plasma";
$idd = 4;
$track = "ty67890iuoj":
<li><a data-toggle="modal" data-target="#mySing<?php echo $track; ?>">View</a></li>
//modal
<div id="mySing<?php echo $track; ?>" 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>
<h4 class="text-center">Order Details</h4>
</div>
<div class="modal-body">
<h3 class="text-center">Print Details</h3>
<div class="text-center">
<?php $notan = DataDB::getInstance()->select_from_where('order_printd','trackingnumber',$track);
foreach($notan as $ron):?>
<span style="padding-right: 5px;font-weight: 600;"><?php echo DataDB::getInstance()->get_name_from_id('name','print_type','print_type_id',$ron['print_type_id']);?></span>
<?php endforeach; ?>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

error when passing id in bootstrap modal

i am new in codeigniter
<?php foreach ($rows as $row) { ?>
<tr>
<td><?php echo $i++;?></td>
<td><?php echo $row->orderid;?></td>
<td><?php echo $row->order_date;?></td>
<td><?php echo $row->name;?></td>
<td><?php echo $row->price;?></td>
<td>
<button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#myModal-<?= $row->customerid?>">View Details</button>
</td>
</tr>
<div class="modal fade" id="myModal-<?= $row->customerid?>" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Order Details</h4>
</div>
<div class="modal-body">
<p><strong>Product Name</strong>: <?php echo $row->name;?></p>
<p><strong>Quantity</strong>: <?php echo $row->quantity;?></p>
<p><strong>Price</strong>: <?php echo $row->price;?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<?php } ?>
only details of the first row is displayed in all modals
i need to get the details of each from each modal
I can help you with bootbox modal plugin(http://bootboxjs.com/). Modify your html as follows
<div id="myModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<!-- dialog body -->
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal">×</button>
<div></div>
</div>
</div>
</div>
</div>
then delete your button code and replace with this code
<a class="btn btn-primary your_defined_class_name_for_jquery_use" data-id="<?php echo $row['whatever_id']?>" > What ever name</a>
add the below script on the bottom of the page
<script>$(className).on('click','your_defined_class_name_for_jquery_use', function (e) {
var id=$(this).data('id');
var dialog = bootbox.dialog({
title: title,
message: $('<div></div>').load(url_to_your_controller_which_shows_the_data ),
});
});
Make sure that jquery is previously loaded on the page. I think this will help you.
Try to bellow code:
function get_orders(){
$this->db->select('*');
$this->db->from('orders o');
$this->db->join('order_detail od', 'o.serial = od.orderid', 'left');
$this->db->join('tbl_products p', 'p.prod_id = od.productid', 'left');
$query = $this->db->get();
return $query->result();
}
use this line $this->db->join('order_detail od', 'o.serial = od.orderid', 'left');
not $this->db->join('order_detail od', 'od.orderid = o.serial','left');

Bootstrap modal delete confirmation

Good day! I've got a problem in my bootstrap modal where I want to delete a post using a modal.
Here is my PHP code where it toggles the delete_modal.php
<?php
if ($row['member_id'] == $_SESSION['member_id']){
echo "
<div style='float:right;'>
<a href='#<?php echo $id; ?>' data-toggle='modal' data-target='#myModal'>Delete</a>
</div>";}
else{
echo "
<div style='float:right;'>
<a href='#'>Delete</a>
</div>";
}
?>
Next this is my delete_modal.php
<div class="modal fade" id="myModal" 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>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button id="<?php echo $id; ?>" href="deleteposthome.php?id='.$row['post_id'].'" type="button" class="btn btn-danger" data-dismiss="modal">Delete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Now this is my deleteposthome.php
<?php
require ("connection.php");
?>
<?php
$id =$_GET['id'];
mysql_query("DELETE FROM posts WHERE post_id = '$id'")
or die(mysql_error());
?>
I don't know how to get ID so that it can be deleted using modal ! please help me
This is my answer, for how many months I've asked this question.
<div class='modal-footer' style='background-color:#1F4E78;'>
<a class='btn btn-danger' href='deleteposthome.php?id=".$row['post_id']."'> Delete</a>
</div>

Categories