Bootstrap Modal & MySQL Query using PHP - javascript

I am trying to have mysql data displayed on a bootstrap modal and for some reason it is only pulling the ID of the first link that is clicked and not the ID for the row specified on the link clicked to display said modal. (hope that makes sense).
The link to be clicked:
<a data-toggle="modal" data-target="#leadModal" href="?id=<?php echo $id; ?>"><?php echo $f; echo ' '; echo $l; ?></a>
Here is the code that surrounds the link:
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Primary Phone</th>
<th>Date Created</th>
<th>Arrival Date</th>
<th>Departure Date</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>E-Mail Address</th>
<th>Primary Phone</th>
<th>Date Created</th>
<th>Arrival Date</th>
<th>Departure Date</th>
</tr>
</tfoot>
<tbody>
<?php
$agent = $userRow['userName'];
$query = "SELECT * FROM leads WHERE status='Follow-Up' && agent='$agent'";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
$f = $line['first'];
$l = $line['last'];
$e = $line['email'];
$p1 = $line['PrimePhone1'];
$p2 = $line['PrimePhone2'];
$p3 = $line['PrimePhone3'];
$a = $line['arrival'];
$d = $line['departure'];
$ag = $line['agent'];
$id = $line['id'];
$dc = $line['datecreated'];
$es = $line['emailsent'];
?>
<tr>
<td><a data-toggle="modal" data-target="#leadModal" href="?id=<?php echo $id; ?>"><?php echo $f; echo ' '; echo $l; ?></a></td>
<td><?php echo $e; ?></td>
<td><?php echo $p1; echo '-'; echo $p2; echo'-'; echo $p3; ?></td>
<td><?php echo $dc; ?></td>
<td><?php echo $a; ?></td>
<td><?php echo $d; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
And here is what I have for code to display the data in the modal:
<!-- Lead Modal-->
<div class="modal fade" id="leadModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header"><?php
$id = $_GET["id"];
$query = "SELECT * FROM leads WHERE id=$id";
$result = mysql_query($query);
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { ?>
<h5 class="modal-title" id="exampleModalLabel"><?php echo $line['first']; ?> <?php echo $line['last']; ?>'s Existing Lead</h5><?php } ?>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body"><?php
$id = $_GET["id"];
$query = "SELECT * FROM leads WHERE id=$id";
$result = mysql_query($query) or die(mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr align="center">
<td>
<table width="750" border="2" align="center" cellpadding="1" cellspacing="0" class="table">
<tr>
<td>Original Agent:</td>
<td><?php echo $line['agent']; ?></td>
</tr>
<?php if ($line['upagent'] == ''){ } else { ?> <tr>
<td>Last Updated By:</td>
<td><?php echo $line['upagent']; ?></td>
</tr><?php } ?>
<tr>
<td width="186">First Name:</td>
<td width="552">
<?php echo $line['first']; ?></td>
</tr>
<?php if ($line['last'] == ''){ } else { ?> <tr>
<td>Last Name:</td>
<td><?php echo $line['last']; ?></td>
</tr><?php } ?>
<?php if ($line['email'] == ''){ } else { ?><tr>
<td>E-Mail Address:</td>
<td><?php echo $line['email']; ?></td>
</tr><?php } ?>
<tr>
<td>Primary Phone:</td>
<td><?php echo $line['PrimePhone1']; echo '-'; echo $line['PrimePhone2']; echo '-'; echo $line['PrimePhone3']; ?></td>
</tr>
<?php if ($line['AltPhone1'] == '' || $line['AltPhone2'] == '' || $line['AltPhone3'] == '') { } else { ?>
<tr>
<td>Alternate Phone:</td>
<td><?php echo $line['AltPhone1']; echo '-'; echo $line['AltPhone2']; echo '-'; echo $line['AltPhone3']; ?></td>
</tr>
<?php } ?>
<tr>
<td>Lead Created:</td>
<td><?php echo $line['datecreated']; ?></td>
</tr>
<?php if($line['emailsent'] == 1){ ?> <tr>
<td>Follow-Up Email Sent:</td>
<td><?php echo $line['followupsent']; ?></td>
</tr>
<?php } ?>
<tr>
<td>Arrival Date:</td>
<td><?php echo $line['arrival']; ?></td>
</tr>
<tr>
<td>Departure Date:</td>
<td><?php echo $line['departure']; ?></td>
</tr>
<?php if ($line['adults'] == ''){ } else { ?>
<tr>
<td>Adults:</td>
<td><?php echo $line['adults']; ?></td>
</tr><?php } ?>
<?php if ($line['child'] == ''){ } else { ?>
<tr>
<td>Children:</td>
<td><?php echo $line['child']; ?></td>
</tr>
<?php } ?>
<?php if ($line['area'] == ''){ } else { ?>
<tr>
<td>Requested Area:</td>
<td><?php echo $line['area']; ?></td>
</tr>
<?php } ?>
<?php if ($line['budget'] == ''){ } else { ?>
<tr>
<td>Budget:</td>
<td><?php echo $line['budget']; ?></td>
</tr>
<?php } ?>
<?php if ($line['suggested'] == ''){ } else { ?>
<tr>
<td>Suggested Properties:</td>
<td><?php echo $line['suggested']; ?></td>
</tr>
<?php } ?>
<?php if ($line['discount'] == ''){ } else { ?>
<tr>
<td>Discount Offered:</td>
<td><?php echo $line['discount']; ?></td>
</tr>
<?php } ?>
<?php if ($line['promo'] == ''){ } else { ?>
<tr>
<td>Promo Code:</td>
<td><?php echo $line['promo']; ?></td>
</tr>
<?php } ?>
<tr>
<td>Lead Status:</td>
<td><?php echo $line['status']; ?></td>
</tr>
<?php if ($line['notes'] == ''){ } else { ?>
<tr>
<td height="85">Additional Notes:</td>
<td><?php echo $line['notes']; ?></td>
</tr>
<?php } ?>
<?php if ($line['resnum'] == ''){ } else { ?>
<tr>
<td>Reservation Number:</td>
<td><?php echo $line['resnum']; ?></td>
</tr><?php } } ?>
</table>
</td>
</tr>
</table>
</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Close</button>
<a class="btn btn-primary" href="#">Button</a>
</div>
</div>
</div>
</div>
Any help is greatly appreciated.
Thanks,
Scott

What you're asking cannot be done in the current design. Your answer will be in looping your db results to create several modals (messy) or using AJAX to retrieve the modal data based on the link ID. A good example is in the answer here: Using Jquery Ajax to retrieve data from Mysql

Related

How to add the existing rows and remove the previous row

I want to add the payment. if the payment is for the same contractor, add the payment as grand total and remove the previous row containing payment. I want to compare that is more than one payment exists against the contractor add that payment as grand total remove the previous row regarding payment. And if the contractor payment is a single entity(only one payment against the contractor) fetch from the database no addition will be made and the fetched row is displayed as the grandtotal .Here is my code
//code to compare that that payment is for thr same contractor whos row is fetched before
<?php
$contractor =0 ;
$payment_con=0;
while($row = mysqli_fetch_array($result))
{
$price = $row["price"];
$total = $row["CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))"];
$grandtotal = $total * $price;
$grandtotal = $grandtotal *0.05;
if($contractor == $row['cnic']) //false
{
$payment_con = $payment_con + $grandtotal ;
?>
<script type="text/javascript">
function run(){
document.getElementById('123').style.display='none';
}
<?php
echo "run();";
?>
</script>
<tr>
<td id="serviceId_<?php echo $row["mazdoorId"];?>"><?php echo $row["mazdoorId"]; ?></td>
<td><?php echo $row["serviceName"]; ?></td>
<td><?php echo $row["price"]; ?></td>
<td><?php echo $row["CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))"]; ?></td>
<td><?php echo $row["contractorName"]; ?></td>
<td><?php echo $row["cnic"]; ?></td>
<td><?php echo $grandtotal. "PKR" ?></td>
<th> GRAND TOTAL : <?php echo $payment_con. "PKR" ?></th> <td> <input type="button" name="edit" value="PAID" id="<?php echo $row["customerId"]; ?>" class="btn btn-danger btn-xs deactivate_accounts" /></td>
</td>
</tr>
<?php
}
else
{
?>
<tr id= "123">
<td id="serviceId_<?php echo $row["mazdoorId"];?>"><?php echo $row["mazdoorId"]; ?></td>
<td><?php echo $row["serviceName"]; ?></td>
<td><?php echo $row["price"]; ?></td>
<td><?php echo $row["CONCAT(Extract(HOUR From timeStamp) ,'.', Extract(MINUTE From timeStamp))"]; ?></td>
<td><?php echo $row["contractorName"]; ?></td>
<td><?php echo $row["cnic"]; ?></td>
<td><?php echo $grandtotal. "PKR" ?></td>
<th> GRAND TOTAL : <?php echo $grandtotal. "PKR" ?></th> <td> <input type="button" name="edit" value="PAID" id="<?php echo $row["customerId"]; ?>" class="btn btn-danger btn-xs deactivate_accounts" /></td>
</td>
</tr>
<?php
$contractor = $row['cnic'];
$payment_con =$grandtotal ;
}
}
?>
</thead>
</table>

datatables default column sorting

I have this table:
<table id="datatable" class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Agent</th>
<th>TWWID</th>
<th>Start Date</th>
<th>Issue</th>
<th>Comment</th>
<th>Overview</th>
<th>Action</th>
<th>TL</th>
<th>Qfinity ID</th>
<th>MSISDN</th>
</tr>
</thead>
<tbody>
<?php
//get records from database
$sql_list = "SELECT * FROM `coaching`";
$sql_list_result = $mysqli->query($sql_list);
if($sql_list_result->num_rows > 0){
while($row = $sql_list_result->fetch_assoc()){ ?>
<tr>
<td><?php echo $row['ID']; ?></td>
<td><?php echo $row['Agent']; ?></td>
<td><?php echo $row['TWWID']; ?></td>
<td><?php echo $row['Start_Date']; ?></td>
<td><?php echo $row['Issue']; ?></td>
<td><?php echo $row['Comment']; ?></td>
<td><?php echo $row['Overview']; ?></td>
<td><?php echo $row['Action']; ?></td>
<td><?php echo $row['TL']; ?></td>
<td><?php echo $row['Qfinity_ID_1']; ?></td>
<td><?php echo $row['MSISDN']; ?></td>
</tr>
<?php } }else{ ?>
<tr><td colspan="5">Nema brojeva u bazi.....</td></tr>
<?php } ?>
</tbody>
</table>
In this table i have sortable heders and first column is defoult sorted ASC, but i want the 3'rd column to be sorted by default DESC.
I include in my .php this js (//cdn.datatables.net/1.10.18/js/jquery.dataTables.min.js)
then I add in .php this script
<script>
$(document).ready(function() {
$('#datatable').DataTable( {
"aaSorting": [[ 3, "desc" ]]
} );
} );
</script>
My table still have sort by 1'st column [ID]
how can i change default sorting?
I faced the same problem and I solved it with the following code. Please try if it can help:
<script>
$(document).ready(function() {
$('#datatable').DataTable( {
"order": [[ 3, "desc" ]]
});
});
</script>

Auto Refresh Table PHP JavaScript/jQuery

I want to refresh my table automatically when i add new row in database. I don't have any idea to create it.
<div class="row">
<div class="col-md-6">
<legend>Typy Darmowe</legend><hr>
<table class="table" id="types-free">
<thread>
<tr>
<th>ID</th>
<th>Mecz</th>
<th>Typ</th>
<th>Data Dodania</th>
<th>URL</th>
</tr>
</thread>
<tbody>
<?php foreach($types as $type) { ?>
<tr>
<td><?php echo $type->t_id; ?></td>
<td><?php echo $type->t_match; ?></td>
<td><?php echo $type->t_type; ?></td>
<td><?php echo $type->t_date; ?></td>
<td><?php echo $type->t_url; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
You need to use AJAX calls. I see you don't have pagination. One easy way to do this is to create an AJAX call that return the count of items from table and then refresh the entire page. Get the count from back end every 1 second. Store the count in a hidden element for comparison. Refreshing the entire page will also update the count stored in hidden field
setTimeout(function(){
var request = $.ajax({
url: "countRows.php",
method: "GET"
});
request.done(function( data ) {
if($('#count').val() != data.count) {
location.reload();
}
});
}, 1000);
Getting the counter:
<?php
// countRows.php
// -- run sql 'SELECT count(id) FROM table_name
// get the count value
echo json_encode(['count' => $count]);
HTML:
<input type="hidden" id="count" value="<?php echo count($types);?>"/>
<div class="row">
<div class="col-md-6">
<legend>Typy Darmowe</legend><hr>
<table class="table" id="types-free">
<thread>
<tr>
<th>ID</th>
<th>Mecz</th>
<th>Typ</th>
<th>Data Dodania</th>
<th>URL</th>
</tr>
</thread>
<tbody>
<?php foreach($types as $type) { ?>
<tr>
<td><?php echo $type->t_id; ?></td>
<td><?php echo $type->t_match; ?></td>
<td><?php echo $type->t_type; ?></td>
<td><?php echo $type->t_date; ?></td>
<td><?php echo $type->t_url; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
If you want advanced and complex table manipulation made only on front end without refresing the entire page you can take a look at datatables

Change Row Color in PHP data table by Date

Today : <?php echo date("m/d/Y"); ?>
<ph>ISSUED BOOKS | DUE BOOKS</ph>
<table border="1" cellpadding="3" cellspacing="0" id="tblissued">
<tr bgcolor="#CCCCCC">
<th height="39">id</th>
<th>Officer</th>
<th>Member_ID</th>
<th>Book_ID</th>
<th>Issued_date</th>
<th>Due_date</th>
<th>Remarks</th>
</tr>
<?php do { ?>
<tr bgcolor="#FFFFFF">
<td height="33"><?php echo $row_rsissuedbooks['id']; ?></td>
<td><?php echo $row_rsissuedbooks['Officer']; ?></td>
<td><?php echo $row_rsissuedbooks['Member_ID']; ?></td>
<td><?php echo $row_rsissuedbooks['Book_ID']; ?></td>
<td><?php echo $row_rsissuedbooks['Issued_date']; ?></td>
<td><?php echo $row_rsissuedbooks['Due_date']; ?></td>
<td><?php echo $row_rsissuedbooks['Remarks']; ?></td>
</tr>
<?php } while ($row_rsissuedbooks = mysql_fetch_assoc($rsissuedbooks)); ?>
</table>
i want to color the rows Due_date if due_date is greater than today (php 1st line in code)
im not good in english..
sorry for that, i hope you will understand..
Something like this will work.
<?php do {
$style = "";
if ($row_rsissuedbooks['Due_date'] > date("m/d/Y")){
$style = "style='background:#FF0000;'";
}
?>
<tr bgcolor="#FFFFFF">
<td height="33"><?php echo $row_rsissuedbooks['id']; ?></td>
<td><?php echo $row_rsissuedbooks['Officer']; ?></td>
<td><?php echo $row_rsissuedbooks['Member_ID']; ?></td>
<td><?php echo $row_rsissuedbooks['Book_ID']; ?></td>
<td><?php echo $row_rsissuedbooks['Issued_date']; ?></td>
<td <?php echo $style;?>><?php echo $row_rsissuedbooks['Due_date']; ?></td>
<td><?php echo $row_rsissuedbooks['Remarks']; ?></td>
</tr>
<?php } while ($row_rsissuedbooks = mysql_fetch_assoc($rsissuedbooks)); ?>
Try this:
<td><?php echo $row_rsissuedbooks['Issued_date']; ?></td>
<?php
echo "<td";
if(strtotime($row_rsissuedbooks['Due_date'])>time())
{
echo " style=\"background-color:#00FF00\"";
}
echo ">".$row_rsissuedbooks['Due_date']."</td>";
?>
<td><?php echo $row_rsissuedbooks['Remarks']; ?></td>

jQuery tablesorter plugin working fine for example but not my table?

It I plug in the html code for the example table they give on the site it works fine:
<table id="myTable" class="tablesorter">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Email</th>
<th>Due</th>
<th>Web Site</th>
</tr>
</thead>
<tbody>
<tr>
<td>Smith</td>
<td>John</td>
<td>jsmith#gmail.com</td>
<td>$50.00</td>
<td>http://www.jsmith.com</td>
</tr>
<tr>
<td>Bach</td>
<td>Frank</td>
<td>fbach#yahoo.com</td>
<td>$50.00</td>
<td>http://www.frank.com</td>
</tr>
<tr>
<td>Doe</td>
<td>Jason</td>
<td>jdoe#hotmail.com</td>
<td>$100.00</td>
<td>http://www.jdoe.com</td>
</tr>
<tr>
<td>Conway</td>
<td>Tim</td>
<td>tconway#earthlink.net</td>
<td>$50.00</td>
<td>http://www.timconway.com</td>
</tr>
</tbody>
</table>
but, if I use my own table which is:
<table id="myTable" class="tablesorter">
<thead>
<tr>
<td>Atasdc</td>
<td>Gamdayed</td>
<td>Wsds</td>
<td>des</td>
<td>Oveaime Losses</td>
<td>Poidts</td>
<td>sd</td>
<td>sdd</td>
<td>Sasdak</td>
</tr>
</thead>
<tbody>
<tr>
<td><? echo $pasdtname ?></td>
<td><? echo $pisadGP ?></td>
<td><? echo $pisdtW ?></td>
<td><? echo $piasdtL ?></td>
<td><? echo $psdOL ?></td>
<td><? echo $pisdttP ?></td>
<td><? echo $psdttH ?></td>
<td><? echo $pisdtR ?></td>
<td><? echo $pitsdtS ?></td>
</tr>
<tr>
<td><? echo $pasdlname ?></td>
<td><? echo $phsdllGP ?></td>
<td><? echo $phsallW ?></td>
<td><? echo $phdasdlOL ?></td>
<td><? echo $philsdP ?></td>
<td><? echo $phidlH ?></td>
<td><? echo $phidlR ?></td>
<td><? echo $phidS ?></td>
</tr>
<tr>
<td><? echo $nysdame ?></td>
<td><? echo $nsdrGP ?></td>
<td><? echo $nydsW ?></td>
<td><? echo $nysdL ?></td>
<td><? echo $nydOL ?></td>
<td><? echo $nysdrP ?></td>
<td><? echo $nysrH ?></td>
<td><? echo $nsyrR ?></td>
<td><? echo $nysrS ?></td>
</tr>
<tr>
<td><? echo $njname ?></td>
<td><? echo $njGP ?></td>
<td><? echo $neejW ?></td>
<td><? echo $njL ?></td>
<td><? echo $nejOL ?></td>
<td><? echo $nejP ?></td>
<td><? echo $njH ?></td>
<td><? echo $nejR ?></td>
<td><? echo $njS ?></td>
</tr>
<tr>
<td><? echo $nyiname ?></td>
<td><? echo $nyeiGP ?></td>
<td><? echo $nyiW ?></td>
<td><? echo $nyiL ?></td>
<td><? echo $nyriOL ?></td>
<td><? echo $nyriP ?></td>
<td><? echo $nyiH ?></td>
<td><? echo $nyiR ?></td>
<td><? echo $nyiS ?></td>
</tr>
</tbody>
What gives? What is different about them? The only thing I can think of is the php coding..? Ihe php vars are all intgers.
The following runs the first table fine and the second table not.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="http://autobahn.tablesorter.com/jquery.tablesorter.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#myTable").tablesorter();
}
);
</script>
did you remember this piece?
$(document).ready(function()
{
$("#myTable").tablesorter();
});
just double checking! :D

Categories