Bootstrap Modal with mysql data - php

I have a html table with list of Product Name and ID, upon clicking the Product Name Link I wanted to open a Modal and show the Item with respect to ID.
I wanted to pass $code to the Model and retrieve data. How should I do?
My code below..
Product 1
Product 2
<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 * FROM $tbl_name WHERE 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 he was targeting classes instead of Ids. data-target for each link to modal should be a unique id. I created a variable $uid (unique identifier), and initialised it to one (alternatively you could use your primary key). Each modal will have an id of myModal+$uid, and each link will point to a specific modal's id.
<?php
$code_id = isset($_GET['code']) ? $_GET['code'] : false;
$result = $db->prepare("SELECT * FROM $tbl_name WHERE id=:code LIMIT 1");
$result->bindValue(':code', $code_id, PDO::PARAM_STR);
$result->execute();
//checks if there are results before sending it to the while loop
if ($result->num_rows > 0) {
$uid = 1;//alternatively you can use your primary key from the table
while($row = $result->fetch_assoc()){
?>
Product <?echo $uid?>
<!-- start modal, realise the 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 your code-->
</div>
<?php
$uid = $uid +1;//increase uid
}// end while
}//end if statement ?>

The data-target of your link would determine what modal to open. So you have to link the attribute of data-target by having the class attribute of your modal the same with the data-target attribute of your link.
Try this:
while(/* YOUR CONDITION FOR FETCHING DATA FROM YOUR DB */){
?>
Product 1
<?php
<!-- START OF MODAL -->
<div class="modal fade myModal<?php echo $uniqueid; ?>" .....>
<!-- REST OF MODAL CODE -->
</div>
} /* END OF LOOP */
Just replace the $uniqueid with your primary key.

Related

Updating the status of Read messages [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
i am trying to display and update read/unread messages. I have a column in database msg_read, that is by default has a value of 0. 0 means read, and 1 means unread. Need guidance to update the status when the message is read. With the guidance of Mr. Andre, the unread messages are showing Bold now, but when read, messages become normal (not bold).
<!-- Display User Messages Details Starts Here -->
<div class="tab-pane userprof-tab" id="tab-10">
<div class="table-responsive border-top">
<table class="table table-bordered table-hover mb-0 text-nowrap">
<thead>
<tr>
<th>Name</th>
<th>Jetski</th>
<th>Sender Email</th>
<th>Phone</th>
<th>Post Code</th>
<th>Message Date</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<?php
$SelectBoat = mysqli_query($con, "SELECT * FROM seller_contact WHERE seller_id='$user_id'");
while($row=mysqli_fetch_assoc($SelectBoat)){
$full_name = $row['full_name'];
$boatname = $row['boatname'];
$sender_email = $row['sender_email'];
$phone = $row['phone'];
$post_code = $row['post_code'];
$message = $row['message'];
$msg_date = $row['msg_date'];
$seller_id = $row['seller_id'];
$msg_read = $row['msg_read'];
?>
<?php
if(msg_read=="0"){
echo '<tr style="font-weight:900">
<td><?php echo $row['full_name'];?></td>
<td><?php echo $row['boat_name'];?></td>
<td><?php echo $row['sender_email'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['post_code'];?></td>
<td><?php echo $row['msg_date'];?></td>
<td>
<!-- View Message -->
<i class="fa fa-eye"></i>
<!-- View Message -->
<!--****** View Message Modal ******-->
<div class="modal fade" id="view<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><strong>From <?php echo $row['sender_email'];?></strong> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message'];?></p>
</div>
</div>
</div>
</div>
<!--****** End View Message Modal ******-->
</td>
</tr>'
} ?>else {
echo '<tr>
<td><?php echo $row['full_name'];?></td>
<td><?php echo $row['boat_name'];?></td>
<td><?php echo $row['sender_email'];?></td>
<td><?php echo $row['phone'];?></td>
<td><?php echo $row['post_code'];?></td>
<td><?php echo $row['msg_date'];?></td>
<td>
<!-- View Message -->
<i class="fa fa-eye"></i>
<!-- View Message -->
<!--****** View Message Modal ******-->
<div class="modal fade" id="view<?php echo $row['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><strong>From <?php echo $row['sender_email'];?></strong> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message'];?></p>
</div>
</div>
</div>
</div>
<!--****** End View Message Modal ******-->
</td>
</tr>'
} ?>
<?php } ?>
</tbody>
</table>
</div>
</div>
I found some errors in your code and some structural failures.
if you are working with PHP in a HTML page, use alternative syntax
see:
https://www.php.net/manual/en/control-structures.alternative-syntax.php
you don't need to rewrite the complete row again for the read message, only put a condicional on the style attribute.
you was calling read_msg instead of $read_msg
new code:
<div class="tab-pane userprof-tab" id="tab-10">
<div class="table-responsive border-top">
<table class="table table-bordered table-hover mb-0 text-nowrap">
<thead>
<tr>
<th>Name</th>
<th>Jetski</th>
<th>Sender Email</th>
<th>Phone</th>
<th>Post Code</th>
<th>Message Date</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($SelectBoat)) : ?>
<?php
$full_name = $row['full_name'];
$boatname = $row['boatname'];
$sender_email = $row['sender_email'];
$phone = $row['phone'];
$post_code = $row['post_code'];
$message = $row['message'];
$msg_date = $row['msg_date'];
$seller_id = $row['seller_id'];
$msg_read = $row['msg_read'];
?>
<tr <?= ($msg_read == "0") ? 'style="font-weight:900"' : '' ?>>
<td><?php echo $row['full_name']; ?></td>
<td><?php echo $row['boat_name']; ?></td>
<td><?php echo $row['sender_email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td><?php echo $row['post_code']; ?></td>
<td><?php echo $row['msg_date']; ?></td>
<td>
<!-- View Message -->
<i class="fa fa-eye"></i>
<!-- View Message -->
<!--****** View Message Modal ******-->
<div class="modal fade" id="view<?php echo $row['id']; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"><strong>From <?php echo $row['sender_email']; ?></strong> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p style="word-wrap: break-word; white-space: initial;"><?php echo $row['message']; ?></p>
</div>
</div>
</div>
</div>
<!--****** End View Message Modal ******-->
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
UPDATE:
to update the status of the message just like i told you in the commnent. add this to your code:
add this sttribute to your view button:
onclick="readMSG(this, '<?php echo $row['id']; ?>')"
add this script and modify to work in your case:
<script>
function readMSG(attribute, id) {
$(attribute).parent().parent().css('font-weight', 'normal')
$.ajax({
type: "GET",
url: "readmsg.php",
data: {
id: id
},
success: function (){
$(attribute).parent().parent().css('font-weight', 'normal')
}
});
}
</script>
the php controller file readmsg.php:
$update = mysqli_query($con, "UPDATE `seller_contact` SET `read_msg`=1 WHERE `id` =" . $_GET['id']);
if ($update) {
return ['success' => 'success'];
} else {
return ['error' => 'error'];
}
try to understand the code and adapt to work in your case. if work, don't forgot to accept the answer ;)

Codeigniter: Bootstrap Modal with table data

I have a table of data populated using a foreach loop like below:
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Keyboard Language</th>
<th scope="col">PC Spec</th>
<th scope="col">Desk Position</th>
<th scope="col">Price Per Hour</th>
<th scope="col">Hours</th>
<th scope="col">Total Price</th>
<th scope="col">Confirm</th>
</tr>
</thead>
<tbody>
<?php $increment =1; ?>
<!--print each item in a new row on the table -->
<?php foreach($bookingData as $booking_item) { ?>
<tr>
<td><?php echo $increment ?></td>
<td><?php echo $booking_item['item']; ?></td>
<td><?php echo $booking_item['item1']; ?></td>
<td><?php echo $booking_item['item2']; ?></td>
<td><?php echo '£ '. $booking_item['item3']; ?></td>
<td><?php echo $userEntered['item4']; ?></td>
<td><?php echo '£ '.$userEntered['item5'] * $booking_item['item6']; ?></td>
<?php $totalPrice = $userEntered['item7'] * $booking_item['item7'];?>
<td><button class="btn btn-success" data-toggle="modal" data-target="#exampleModalCenter<?php echo $increment; ?>"><i class="fa fa-calendar-check" style="color: white;"></i>Book now!</button></td>
</tr>
<?php $increment++; } ?>
</tbody>
</table>
I have button in each table row, I want to create a modal that pops up to ask the user to confirm the action using the modal code below:
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<?php echo $booking_item['item1']?>
<?php echo $booking_item['item2']?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Confirm Booking!</button>
</div>
</div>
</div>
</div>
However, I'm stumped as to how to load each row of data into the modal for each table row, I have done this before in another project but I can't for the life remember how to replicate it
For the modal to be loaded there needs to be seperate model content for each triggers. So you need model contents in foreach also.
<?php foreach
<td><button class="btn btn-success" data-toggle="modal" data-target="#exampleModalCenter<?php echo $increment; ?>"><i class="fa fa-calendar-check" style="color: white;"></i>Book now!</button></td>
foreach end
?>
<?php foreach
$increment =0;
<div class="modal fade" id="exampleModalCenter#exampleModalCenter<?php echo $increment; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
model content
</div>
</div>
$increment++;
foreach end
?>
I hope you understand what I am trying to tell you. You need to loop modal also and make the id dynamic.
I managed to accomplish this by using some simple JQuery and data attributes. I pass the data attributes into the button during the foreach loop so my button now looks like this:
<button class="btn btn-success" data-toggle="modal" data-target="#exampleModalCenter"
data-specs="PC Specs: <?php echo $booking_item['specs'];?>"
data-date="Date: <?php echo $userEntered['date'];?>"
data-start="Start: <?php echo $userEntered['start_time'];?>"
data-end="End : <?php echo $userEntered['end_time'];?>"
data-totalprice="Total Price : <?php echo $totalPrice;?>"
data-hours="Hours : <?php echo $userEntered['hours_booked'];?>">
<i class="fa fa-calendar-check" style="color: white;"></i>Book now!
</button>
Now In my Jquery, I fetch these values and set the text of the classes I have established within the modal
$('#exampleModalCenter').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) //button that triggers modal
var spec = button.data('specs') // Extract info from data-* attributes
var date = button.data('date')
var start = button.data('start')
var end = button.data('end')
var hours = button.data('hours')
var totalPrice = button.data('totalprice')
var modal = $(this)
modal.find('.specs').text(spec)
modal.find('.date').text(date)
modal.find('.start').text(start)
modal.find('.end').text(end)
modal.find('.hours').text(hours)
modal.find('.totalprice').text(totalPrice)
})
I found this at: https://getbootstrap.com/docs/4.0/components/modal/
Firstly add id attribute to the confirm button and store all your data in data attribute as I did in the below code.
In your modal add form to submit your data stored in the hidden input fields.
Edit
As per your answer, I've updated my code
Your button
<button class="btn btn-success" id="confirmBtn"
data-specs="<?php echo $booking_item['specs'];?>"
data-date="<?php echo $userEntered['date'];?>"
data-start="<?php echo $userEntered['start_time'];?>"
data-end="<?php echo $userEntered['end_time'];?>"
data-totalprice="<?php echo $totalPrice;?>"
data-hours="<?php echo $userEntered['hours_booked'];?>">
<i class="fa fa-calendar-check" style="color: white;"></i>Book now!
</button>
Your Modal code
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="<?php echo base_url(); ?>your_controller_name/confirm_book" method="post">
<div class="modal-body">
<input type="hidden" name="id" id="bookId" />
<input type="hidden" name="specs" id="specs" />
<input type="hidden" name="date" id="date" />
<input type="hidden" name="start_time" id="start_time" />
<input type="hidden" name="end_time" id="end_time" />
<input type="hidden" name="totalPrice" id="totalPrice" />
<input type="hidden" name="hours_booked" id="hours_booked" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Confirm Booking!</button>
</div>
</form>
</div>
</div>
</div>
This is your jquery code which does all your work.
Jquery Code
$('#confirmBtn').on('click', function (e) {
e.preventDefault();
var id = $(this).data('id'); //takes the `id` from your button
var specs = $(this).data('specs');
var date = $(this).data('date');
var start_time = $(this).data('start_time');
var end_time = $(this).data('end_time');
var totalPrice = $(this).data('totalPrice');
var hours_booked = $(this).data('hours_booked');
$("#exampleModalCenter #bookId").val(id); //stores to modal hidden field
$("#exampleModalCenter #specs").val(specs);
$("#exampleModalCenter #date").val(date);
$("#exampleModalCenter #start_time").val(start_time);
$("#exampleModalCenter #end_time").val(end_time);
$("#exampleModalCenter #totalPrice").val(totalPrice);
$("#exampleModalCenter #hours_booked").val(hours_booked);
$('#exampleModalCenter').modal();
});
Here is your controller code
public function confirm_book(){
$id = $this->input->post('id');
$specs= $this->input->post('specs');
$date= $this->input->post('date');
$start_date= $this->input->post('start_date');
$end_date= $this->input->post('end_date');
$totalPrice= $this->input->post('totalPrice');
$hours_booked= $this->input->post('hours_booked');
//do whatever you want to do with these data
}
Hope this will help you.

Value of the php variable fetched from mysql database cannot display in bootstrap modal

I would to fetch the mysql database table rows and display it into the html tables after that when user click on the value in table, bootstrap modal trigger and display all the column values of that clicked row in modal.
Here is php code
<tbody>
<?php
include "connection.php";
$sql="SELECT email,date_t from users";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result))
{
$mail=$row['email'];
$getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT fname,email from users where email='$mail'"));
$fname = $getID['fname'];
$email = $getID['email'];
echo '<tr>
<td>'.$fname.'</td>
<td>'.$row['email'].'</td>
<td>'. "$email" .'</td>
<td>'.$row['date_t'].'</td>
</tr>';
}
?>
</tbody>
Here is bootstrap modal
<div id="mymodel" 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">Email Reply</h4>
</div>
<div class="modal-body">
<p><?php echo $email; echo $fname; ?></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
It will display value of $fname in modal but not display the value of $email
Any help will be appreciated!

Modal Popup with dynamic IDs

Here is my code
$sql = "select * from billbook where stcode =? and sem =?";
$query = $mysqli->prepare($sql);
$query->bind_param('si',$stcode, $sem);
$query->execute();
$query->store_result();
$query->bind_result($billno,$billdate,$stcode,$sem,$amount);
$x = 1;
while($query->fetch())
{
?>
<tr>
<td align='center'><?php echo $x ?></td>
<td align='center' id="bno"><?php echo $billno ?></td>
<td align='center'><?php echo $billdate?></td>
<td align='right'><?php echo $amount?>.00</td>
</tr>
<?php
}
?>
My aim is to show the details of the Bill Number in a Modal Dialog Popup in Bootstrap, when the user click the "bno" ID (Second Column), after processing the details in an external PHP file.
Please Help.
<td align="center" id="bno"><?= $billno ?></td>
<?php
// generate a modal per bill
$modals[] = array(
'billno' => $billno,
'modal_id' => 'billno-'.$billno,
'title' => 'Bill No. '.$billno,
'message' => 'Modal body here',
'dismiss_button_text' => 'Close'
);
?>
...
<?php foreach ($modals as $modal) : ?>
<!-- Modal -->
<div class="modal fade" id="billno-<?= $modal['billno'] ?>" 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"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title"><?= $modal['title'] ?></h4>
</div>
<div class="modal-body">
<p><?= $modal['message'] ?></p>
</div>
<div class="modal-footer">
<div class="form-group">
<button type="button" class="btn btn-success form-control" data-dismiss="modal"><?= $modal['dismiss_button_text'] ?></button>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>

Bootstrap: Trouble using modal for showing dynamic data

I have a list created dinamically using php
while($row = $result->fetch_assoc()){ //$row is a row fetched from my database
<tr>
<td><?php echo $row['id']?></td>
<td><?php echo $row['name']?></td>
<td><?php echo $row['surname']?></td>
<td><a href="#Modal" <?php echo "id= " . $row['username'] ?> class="btn btn-small btn-info" data-toggle="modal" > Vedi</a></td>
</tr>
}
Modal is something like this:
<div id="Modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<a class="close" data-dismiss="modal" aria-hidden="true">×</a>
<h3 id="myModalLabel">user information</h3>
</div>
<div class="modal-body" id="loadInfo">
<div class="row-fluid">
<div class="span12">
<!-- my php code here -->
</div>
</div>
</div>
</div>
My php code must be referred to a precise user. So I need that the Modal can receive this the id of the in order to execute the proper php code.
How can I obtain this result?
assign the values of the user info to javascript variables. Add a data attribute to the modal toggle buttons like data-user_id=<?php echo $row['id']; ?> and make a jquery click handler to update the modal's content to match the button you clicked.

Categories