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
Related
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.
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.
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 not really familiar with Ajax Response - I have edited the PHP Ajax Search code from W3schools.com as follows :
<?php
require_once('connect_db.php');
$query = "select item_no from items";
$result = mysql_query($query);
$a = array();
while ($row = mysql_fetch_assoc($result)){
$a[] = $row['item_no'];
}
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="No Suggestion";
}
else
{
$response=$hint;
}
//output the response
echo "<table border=1><tr><td>".$response."</td></tr></table>";
?>
The output of the above code works perfectly, but they are all listed like this (2L500BU , 2L500GO , 2L500NA , 2L500RD , 2L802CA , 2L802WH , 2L803GR , 2L804BE , 2L804BK , 2L804CO , 2L805BU , 2L806BE , 2L806GR ) - Those numbers are Item No in mysql table called items.
Now :
1) I want to output the response into table with <tr> for each like this
2l500BU
2L500GO
.
.
.
.
etc.
2) Do you think its possible to output all table records from Mysql based on the hint entered as follows :
$sql="SELECT * FROM items WHERE item_no = '".**$hint**."'";
$result = mysql_query($sql);
echo "<table align='center' cellpadding='3' cellspacing='3' width='800px' border='1' font style='font-family:arial;'>";
echo "
<tr align=center>
<th style=font-size:18px; bgcolor=#20c500>Item Number</th>
<th style=font-size:18px; bgcolor=#20c500>QTY</th>
<th style=font-size:18px; bgcolor=#20c500>Actual Price</th>
<th style=font-size:18px; bgcolor=#20c500>Selling Price</th>
<th style=font-size:18px; bgcolor=#20c500>Difference</th>
<th style=font-size:18px; bgcolor=#20c500>Date</th>
</tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr align=center bgcolor=#e3e3e3>";
echo "<td style='font-size:18px; font-weight:bold;'>" . strtoupper($row['item_no']) . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['qty'] . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['actual_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['discount_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['difference_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . date("d-m-Y",strtotime($row['date'])) . "</td>";
echo "</tr>";
}
echo "<table>";
If you're wanting to fetch items from a database and display a row for each of them, this is how you'd do it with jQuery.
Your PHP script:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$sql = "SELECT item_no FROM items";
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
$rows[] = $row['item_no'];
}
header('Content-Type: application/json');
echo json_encode($rows);
?>
And your HTML with the table:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Item No.</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
<script src="js/lib/jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('yourscript.php', function(items) {
$.each(items, function(i, item) {
$('tbody').append('<tr><td>' + item + '</td></tr>);
});
});
});
</script>
I've also used MySQLi (MySQL improved) rather than the standard mysql_ functions, as the mysql_ library is deprecated and you should be using either MySQLi or PDO now.