I want to filter my data by date-range as well as the time range.
I am done with date-range but not getting a solution for the time.
I want to retrieve data between two-time range. How could I do that?
here is my code for date range:
1) Ajax
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
$(function(){
$("#from_date").datepicker();
$("#to_date").datepicker();
});
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '')
{
$.ajax({
url:"filter.php",
method:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data)
{
$('#order_table').html(data);
}
});
}
else
{
alert("Please Select Date");
}
});
});
</script>
2) SQL Query :
if(isset($_POST["from_date"], $_POST["to_date"])) {
$connect = mysqli_connect("localhost", "root", "", "data");
$output = '';
$query = "
SELECT * FROM date_filter
WHERE date BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'
";
$result = mysqli_query($connect, $query);
$output .= '
<table class="table table-bordered">
<tr>
<th width="5%">ID</th>
<th width="30%">Customer Name</th>
<th width="12%"> Date</th>
<th width="12%"> Time</th>
</tr>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'. $row["id"] .'</td>
<td>'. $row["customer_name"] .'</td>
<td>'. $row["date"] .'</td>
<td>'. $row["time"] .'</td>
</tr>
';
}
}
else
{
$output .= '
<tr>
<td colspan="5">No data Found</td>
</tr>
';
}
$output .= '</table>';
echo $output; } ?>
Help me with Time filtering. To Retrieve data from Database.
Related
I want to pass the value from jquery to php using AJAX, the problem is the value in the php is not properly set.
Here is my sample codes:
Button trigger to pass data into jquery.
<button class="btn btn-info add-percentage"
data-landing_id-percentage="'.$row['hidden_id'] .'"
data-toggle="add-percentage"><i class="glyphicon glyphicon-time"></i></button>
Below is the ajax and jquery code.
<script>
$(".add-percentage").click(function(){
var landing_id = $(this).data('landing_id-percentage');
$.ajax({
url: 'ajax/readPercentage.php',
type: 'POST',
data: {
landing_id: landing_id
},
success: function(msg) {
alert('Email Sent'+msg);
$('#add-percentage').modal('show');
readRecords();
}
});
});
<script>
The result below in the php code is the landing_id is not properly set.
readPercentage.php
<?php
$data = '
<table class="table table-bordered table-hover table-striped" id="ModalMyTable">
<thead>
<tr class="success">
<th ><center>No.</center></th>
<th ><center>Percentage ID</center></th>
<th ><center>Landing ID</center></th>
<th><center>Percentage</center></th>
<th><center>Date Added</center></th>
</tr>
</thead>';
if(isset($_POST['landing_id'])){
$landing_id = $_POST['landing_id'];
$query = mysqli_query($conn, "SELECT
percentage.percent_id,
percentage.landing_id,
percentage.percentage,
percentage.date_recorded
FROM
percentage
WHERE percentage.landing_id = '$landing_id'");
$number = 1;
while($row = mysqli_fetch_assoc($query)) {
$data .= '
<tr>
<td><center>' . $number . '</center></td>
<td><center>' . $row['percent_id'] . '</center></td>
<td><center>' . $row['landing_id'] . '</center></td>
<td><center>' . $row['percentage'] . '%</center></td>
<td><center>' . date("M. d, Y", strtotime($row['date_recorded'])) . '</center></td>
</tr>';
$number++;
}
}else{
$data .='<tr><td colspan="6">Landing id is not properly set!</td></tr>';
}
$data .= '</table>
<script>
{
$(document).ready(function(){
$("#ModalMyTable").DataTable();
readRecords();
});
}
</script>
';
echo $data;
?>
I hope some one can help me in my problem. Thanks in advance.
For report generation in the project I made I want to use 2 methods:
By date range (from date and up to date)
By employee ID
While using the above keywords the complete record of the employee should be displayed in that particular range.
For ex- emp1234 is the employee whose record is to be checked between date 20 may 2018 to 30 june 2018. The result must be displayed in between the range
Another thing is that the search can be done either by date or by employee id or both but both should not be compulsory.
the script is as follows:
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
$(function(){
$("#from_date").datepicker();
$("#to_date").datepicker();
});
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '')
{
$.ajax({
url:"filter.php",
method:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data)
{
$('#report_table').html(data);
}
});
}
else
{
alert("Please Select Date");
}
});
});
</script>
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"fetch.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#report_table').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
I need to combine these two different queries into one.
filter.php
<?php
//filter.php
if(isset($_POST["from_date"], $_POST["to_date"]))
{
$connect = mysqli_connect("localhost", "root", "", "ais");
$output = '';
$query = "
SELECT * FROM tbl_employees,tbl_in_time_attendance,tbl_out_time_attendance WHERE tbl_employees.EmpId=tbl_in_time_attendance.EmpId AND tbl_in_time_attendance.EmpId=tbl_out_time_attendance.EmpId AND tbl_in_time_attendance.date=tbl_out_time_attendance.date AND tbl_in_time_attendance.date AND tbl_out_time_attendance.date BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'
";
$result = mysqli_query($connect, $query);
$cnt=1;
$output .= '
<table class="table table-bordered">
<tr>
<th width="6%">Sr No.</th>
<th width="15%">EMPLOYEE ID</th>
<th width="20%">EMPLOYEE NAME</th>
<th width="15%">IN_TIME</th>
<th width="15%">OUT_TIME</th>
<th width="15%">DATE</th>
<th width="25%">WORKING HOURS</th>
</tr>
';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$outtime = $row["outtime"];
$intime = $row["intime"];
$array1 = explode(':', $intime);
$array2 = explode(':', $outtime);
$minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
$minutes2 = ($array2[0] * 60.0 + $array2[1])/60;
$diff = round(($minutes2 - $minutes1),1).' hrs';
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " . $row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td> ';
if ($diff < 8) {
$output .= '<td align="center" style="background-color: #F42E2E;"> ' . $diff . ' </td>';
} else {
$output .= '<td align="center" style="background-color: #55E443;"> ' . $diff . ' </td>';
}
$output .= '</tr>';
$cnt++;
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Record Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
}
?>
Fetch.php
<?php
$connect = mysqli_connect("localhost", "root", "", "ais");
$output = '';
if(isset($_POST["query"]))
{
$query = "
SELECT * FROM tbl_employees,tbl_in_time_attendance,tbl_out_time_attendance WHERE tbl_employees.EmpId=tbl_in_time_attendance.EmpId AND tbl_in_time_attendance.EmpId=tbl_out_time_attendance.EmpId AND tbl_in_time_attendance.date=tbl_out_time_attendance.date AND tbl_in_time_attendance.date AND tbl_employees.EmpId LIKE '".$_POST["query"]."' ";
}
else
{
$query = "SELECT * from tbl_employees, tbl_in_time_attendance , tbl_out_time_attendance WHERE tbl_employees.EmpId=tbl_in_time_attendance.EmpId AND tbl_in_time_attendance.EmpId=tbl_out_time_attendance.EmpId AND tbl_in_time_attendance.date=tbl_out_time_attendance.date";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$cnt=1;
$output .= '
<table class="table table-bordered">
<tr>
<th width="6%">Sr No.</th>
<th width="15%">EMPLOYEE ID</th>
<th width="20%">EMPLOYEE NAME</th>
<th width="15%">IN_TIME</th>
<th width="15%">OUT_TIME</th>
<th width="15%">DATE</th>
<th width="25%">WORKING HOURS</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$outtime = $row["outtime"];
$intime = $row["intime"];
$array1 = explode(':', $intime);
$array2 = explode(':', $outtime);
$minutes1 = ($array1[0] * 60.0 + $array1[1])/60;
$minutes2 = ($array2[0] * 60.0 + $array2[1])/60;
$diff = round(($minutes2 - $minutes1),1).' hrs';
$output .= '
<tr>
<td align="center">'. $cnt .'</td>
<td align="center">'. $row["EmpId"] .'</td>
<td align="center">'. $row["FirstName"] . " " . $row["LastName"].'</td>
<td align="center">'. $row["intime"] .'</td>
<td align="center"> '. $row["outtime"] .'</td>
<td align="center">'. $row["date"] .'</td> ';
if ($diff < 8) {
$output .= '<td align="center" style="background-color: #F42E2E;"> ' . $diff . ' </td>';
} else {
$output .= '<td align="center" style="background-color: #55E443;"> ' . $diff . ' </td>';
}
$output .= '</tr>';
$cnt++;
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Record Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
?>
I want these to separate fetch and filter files into one file.
Please help.
please dont delete the question again, i was writing a few minutes and then the question was deleted haha
soo, as you have a filter button which has to be clicked to get the results there are two ways you can choose!
you display your table with datatable plugin which means you only need to filter by date and then write the employeeid in the searchbox and you will only see the results for this person WITHOUT having to query it again!
https://datatables.net/
you get the two dates AND your search query by clicking on the filter button which means you will only have one php file (i dont know why you actually want to use fetch & filter).. In your php file you then look if there are only dates submitted OR the query is also submitted:
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
var query = $("#search_text").val();
if(from_date != '' && to_date != '')
{
$.ajax({
url:"filter.php",
method:"POST",
data:{from_date:from_date, to_date:to_date, employee: query},
success:function(data)
{
$('#report_table').html(data);
}
});
}
else
{
alert("Please Select Date");
}
});
in php file
if(isset($_POST["employee"]) AND $_POST["employee"] != "") {
// make your sql call with dates AND employeeid
} else {
// make your sql call only with dates
}
depending on how many results you have I'm sure its also possible to dont even have to query again for different dates and instead tweak datatable so that you have one from_date, one to_date and one search field and filter the table after keyup.. see example here: https://datatables.net/examples/api/multi_filter.html
I am using mysql queries to fetch data from db. All my data are showing fine in tables. Now I want to color the status by value less than 2 or more than 3.
The below code is not working.
Need help.
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
?>
<?php
function status_style($row) {
if ($row < 2) return 'background-color: #ff0000'; //red
if ($row > 3) return 'background-color: #33cc33'; //green
return '';
}
?>
<?php
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
?>
You have already created function to color the rows but haven't called the function during showing output. That's why it is not working.
Change your $output variable with:
$output .= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td style="'.status_style($row["status"]).'">'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
You can try something like this:
<?php
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) > 0)
{
$output .= '<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>name</th>
<th>genre</th>
<th>time</th>
<th>status</th>
<th>more...</th>
</tr>';
function status_style($row) {
if ($row < 2) return $color = '#ff0000'; //red
if ($row > 3) return $color = '#33cc33'; //green
return $color = '';
}
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr style='"'background-color: '"' . $color . '"'>
<td>'.$row["name"].'</td>
<td>'.$row["genre"].'</td>
<td>'.$row["time"].'</td>
<td>'.$row["status"].'</td>
<td>'.$row["more"].'</td>
</tr>
';
}
echo $output;
{
echo 'Data Not Found';
}
You didn't do anything with the background color.
I have a problem with pagination, i see only first page, i try to undestand. I have a file HTML with a script and the script launch a file php. But the result is only a file
<?php
include 'database.php';
$output = '';
$start=0;
$limit=2;
if(isset($_GET['id'])){
$id=$_GET['id'];
$start=($id-1)*$limit;
}else{
$id=1;
}
$search = $_POST["search"];
if($search == ''){
$sql = "SELECT * FROM dati ORDER BY id DESC LIMIT $start, $limit";
}
else {
$sql = "SELECT * FROM dati WHERE nome LIKE '%".$_POST["search"]."%' ORDER BY id DESC LIMIT $start, $limit";
}
$retval = mysqli_query( $dbconfig, $sql);
$output .= '
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Nome</th>
<th>Categoria</th>
<th>Numero di Telefono</th>
</tr>
</thead>
<tbody>';
while($row=mysqli_fetch_array($retval)){
$output .= '
<tr>
<td>'. $row['nome'] . '</td>
<td>'. $row['categoria'] . '</td>
<td>'. $row['telefono'] . '</td>
</tr>';
}
$output .= '
</tbody>
</table>';
$ret = mysqli_num_rows(mysqli_query( $dbconfig, $sql));
$total=ceil($ret/$limit);
$output .= '
<div class="row">
<ul class="page">';
for($i=1;$i<=$total;$i++){
if($i==$id) {
$output .= '<li class="corrente">'.$i.'</li>';
}else {
$output .= '<li><a href=?id='.$i.'>'.$i.'</a></li>';
}
}
$output .= '
</ul>
</div>';
echo $output;
?>
This is file PHP with pagination but nothing
<input type="submit" name="test" id="test" value="Ricerca">
I'm trying to make an addition (or subtraction if needed) to a php page with ajax.
What this code is supposed to do is add all the prices up and give me the correct sum with the live-edit-table.
if a user changes the value of price $price_total should change acordingly.
Even if a new row is added it should still give the new sum (displayed in $price_total and $final_price_total) with the new row included.
I have been trying to figure out how to do this but I can't figure out how.
$price_total and $final_price_total do not get saved into a database
this is the code that I have so far:
index.php
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<div id="live_data"></div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
function fetch_data(){
$.ajax({
url:"select.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
function formatDecimal(val, n) {
n = n || 2;
var str = "" + Math.round ( parseFloat(val) * Math.pow(10, n) );
while (str.length <= n) {
str = "0" + str;
}
var pt = str.length - n;
return str.slice(0,pt) + "." + str.slice(pt);
}
fetch_data();
function edit_data(id, text, column_name){
$.ajax({
url:"edit.php",
method:"POST",
data:{
id:id,
text:text,
column_name:column_name
},
dataType:"text",
success:function(data){
/*alert(data);*/
}
});
}
$(document).on('click', '#btn_add', function(){
/* this function would also add the prices to the table*/
var price = $('#price').text();
var final_price = $('#final_price').text();
if(price == ''){
alert("Enter Price");
return false;
}
if(final_price == ''){
alert("Enter Final Price");
return false;
}
$.ajax({
url:"insert.php",
method:"POST",
data:{price:price, final_price:final_price},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
})
});
$(document).on('blur', '.price', function(){
var id = $(this).data("id1");
var price = $(this).text();
edit_data(id, price, "price");
/*I'm trying so that I can add the new input price with the old total to give the exact value*/
$total = parseFloat( price ) + parseFloat( $('#price_total').value );
$('#price_total').value = formatDecimal(total);
});
$(document).on('blur', '.final_price', function(){
var id = $(this).data("id2");
var final_price = $(this).text();
edit_data(id, final_price, "final_price");
});
});
select.php
<?php
$connect = mysqli_connect("localhost", "root", "", "test_db");
$output = '';
$sql = "SELECT * FROM tbl_sample ORDER BY id DESC";
$result = mysqli_query($connect, $sql);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">
<tr>
<th width="10%">Id</th>
<th width="40%">Price</th>
<th width="40%">Final Price</th>
<th width="10%">Delete</th>
</tr>';
if(mysqli_num_rows($result) > 0){
$final_price_total = 0;
$price_total = 0;
while($row = mysqli_fetch_array($result)){
$output .= '
<tr>
<td>'.$row["id"].'</td>
<td class="price" data-id1="'.$row["id"].'" contenteditable>'.$row["price"].'</td>
<td class="final_price" data-id2="'.$row["id"].'" contenteditable>'.$row["final_price"].'</td>
<td><button type="button" name="delete_btn" data-id3="'.$row["id"].'" class="btn btn-xs btn-danger btn_delete">x</button></td>
</tr>
';
$final_price_total += $row["final_price"];
$price_total += $row["price"];
}
$output .= '
<tr>
<td></td>
<td class="price_total" id="price_total" name="price_total" value="'.$price_total.'" contenteditable>'.$price_total.'</td>
<td class="final_price_total" id="final_price_total" name="final_price_total" value="'.$final_price_total.'" contenteditable>'.$final_price_total.'</td>
<td></td>
</tr>
';
$output .= '
<tr>
<td></td>
<td id="price" contenteditable></td>
<td id="final_price" contenteditable></td>
<td><button type="button" name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>
';
}
else{
$output .= '<tr>
<td colspan="4">Data not Found</td>
</tr>';
}
$output .= '</table>
</div>';
echo $output;
?>
From the little discussion in the question comments, I've managed to spot this variable typo:
$total = parseFloat( price ) + parseFloat( $('#price_total').value );
$('#price_total').value = formatDecimal(total);
On the second line you are using total instead of $total.
If you check your browser's error console, you will see an error about undeclared variable total.
Edit: after further discussion, you wanted to know how to loop over the table in JavaScript and add up the total. Try this:
var newTotal = 0.0;
$('.price').each(function() {
newTotal += parseFloat($(this).text());
});
If you do this on your edit finding, you can then use the newTotal variable in the final_price field in place of where you are using the $total variable.