I have two tables one having employee name, employee id and another table tblleaves having empid,Leave_Date, fromDate, toDate, Description.
If user choose one day leave it stores the date value to Leave_Date and if user choose multiple days it store value of from date and to date.
Now I want the monthly report of employees. In this page I want an employee name, Leave Days and Leave Dates. I tried codes but I got employee name repeatedly because they apply many leaves in that month i want to display employee name one time.
<?php
if(isset($_POST['apply'])){
$ym=$_POST['month'];
list($Year, $Month) = explode("-", "$ym", 2);
$sql = "SELECT distinct tblleaves.id as lid,tblemployees.FirstName,tblemployees.LastName,tblemployees.EmpId,
tblemployees.id,tblleaves.LeaveType,tblleaves.PostingDate,
tblleaves.Leave_Date from tblleaves join tblemployees on tblleaves.empid=tblemployees.id
WHERE YEAR(Leave_Date) = 2019 AND MONTH(Leave_Date) = 6";
echo $sql;
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{ ?>
<tr>
<td> <?php echo htmlentities($cnt);?></td>
<td><?php echo htmlentities($result->FirstName);?> <?php echo htmlentities($result->LastName);?></td>
<td><?php ?></td>
<td><?php $result->Leave_Date?></td>
<?php $cnt++;}}}?>
I want employee monthly leave report
employee name Leave Days Leave Dates
KrishnanR 3 12-06-2019, 13-06-2019, 14-06-2019
Really that sort of formatting should be done in the report itself by having a band that occurs once per emplyee and shows the name and then within band show all the records for that employee.
However you can get a dataset containing the example result you posted by using the MySQL function GROUP_CONCAT().
The following code will give you three columns, FirstName, LastName and leave_dates with one row per employee. The leave_date column will contain all their tblleaves.Leave_Date values, separated by a comma and a space (or whatever appears after the key word SEPARATOR.
You won't need the DISTINCT
SELECT
tblemployees.FirstName,
tblemployees.LastName,
count(tblleaves.empid) as Leave_Days,
GROUP_CONCAT( tblleaves.Leave_Date SEPARATOR ', ' ) AS leave_dates
FROM
tblleaves
JOIN tblemployees
ON tblleaves.empid = tblemployees.id
WHERE YEAR(Leave_Date) = 2019
AND MONTH(Leave_Date) = 6
GROUP BY tblemployees.EmpId
Related
you see I am trying to list the records of a table in my database, but I want to do it in the following way:
First, it has to display the date
and then all the records on that date should appear
In my table of the database I have 4 fields:
id, task, date, time
For example there are multiple tasks that are performed in a day, but at different times. Then I have stored in my database many tasks of different days and in different hours. What I want is to list them per day. Consult the database and show a list where the date appears first and then all the tasks that were done on that date, then show the other date and then all the tasks of that date and so on.
Something like that
That's my php code
$obj = new Task();
$consult = $obj->Lists();
date_default_timezone_set("America/Mexico_City");
$dateActual = date("Y-m-d");
while ($result = $consult->fetch_object()) {
echo "<button class='btn btn-default'>date = " . $result->date . "</button><br>";
$consult2 = $obj->Lists2($dateActual);
while($result2 = $consult2->fetch_object()) {
echo "<span>". $result2->time ."</span><br>";
}
$dateActual = $result->date;
}
my query to the database is:
public function Lists2($date)
{
global $conexion;
$sql = "SELECT ar.*, date_format(ar.date, '%d/%m/%Y') as date,
date_format(ar.time, '%r') as time,
u.user as User
FROM task_recents ar
INNER JOIN user u ON ar.iduser = u.iduser
WHERE date = '$date'
ORDER BY ar.time DESC";
$query = $conexion->query($sql);
return $query;
}
public function Lists()
{
global $conexion;
$sql = "SELECT ar.*, date_format(ar.date, '%d/%m/%Y') as date,
date_format(ar.time, '%r') as time,
u.user as User
FROM task_recents ar
INNER JOIN user u ON ar.iduser = u.iduser
ORDER BY ar.time DESC";
$query = $conexion->query($sql);
return $query;
}
The result is that it shows me the repeated date with their respective records.
What I'm trying to achieve is something like this:
How could I do it?
PD: The result that I'm getting is this:
But I don't like that...
The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. If there are records in the "Orders" table that do not have matches in "Customers", these orders will not be shown!
The following query should no longer generate duplicate records
SELECT
ar.id,
ar.task,
date_format(ar.date, '%d/%m/%Y') as formattedDate,
date_format(ar.time, '%r') as formattedTime,
u.user as User
FROM
task_recents ar
LEFT JOIN
user u
ON
u.iduser = ar.iduser
WHERE
date = '$date'
ORDER BY
ar.time
DESC
I have a table containing records created by different users on different dates. My goal is to get the date, users, count of records created by each user, and total count of records per date in this format.
<?php
$q = "SELECT enroledby, enrolment_date, COUNT(enroledby) coun FROM sample_data GROUP BY enroledby";
$result = mysqli_query($db,$q);
while($row = mysqli_fetch_array($result))
{
?>
<th><?php echo $row[0]; ?></th>
<?php
}
?>
The code above is dynamically echoing the users in table header as desired. How do I echo the date, count per user and total count as table data (td) like the above format ?
Thanks.
I have a database with many records (major columns are Date, Company Names, Closing Price. What i want to achieve is to use php to display all unique company names in the first column, most recent price in the 2nd (using date), Highest Price of all unique companies in database in the 3rd column and lowest price of all unique companies in the database. Please, how can i achieve this?
$query = "SELECT date, company, MIN(close), MAX (Close) FROM pricelist GROUP BY company";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
for ($i=0;$i<$num_rows;$i++){
$id = mysql_result($result,$i,"id");
$date = mysql_result($result,$i,"date");
$company = mysql_result($result,$i,"company");
$close = mysql_result($result,$i,"close");
echo "<tr bgcolor=\"#d7dde3\"><td align=right class=no_text>".$company."</td><td align=right class=norm_text>".number_format($close, 2, '.', ',')." </td><td align=right class=norm_text>".$date." </td></tr>";
}
Here is what i am trying to achieve:
Company Name Year High Date Year Low Date
Google $20 02/17/2014 $10 05/13/2014
Apple $40 01/22/2014 $34 04/05/2014
Select all records by date, then use php arrays, asort() - low->high, arsort() - high->low.
Find more about php array here: PHP Array Sorting
You can try it, but not tested
SELECT DISTINCT company, price, MAX (price),MIN(price) FROM pricelist
ORDER BY date DESC
I have 3 Tables:
1.Personal - with the list fo the workers.
2.Codes - with the list of the shift.
3.Dienstplan - that like a working schedule
I have a following query
$query4 = $db->query("SELECT count(codes.lcfruh) AS front_lcfruh, name
FROM dienstplan
LEFT JOIN codes ON (dienstplan.schicht = codes.lcfruh)
LEFT JOIN personal ON personal.perso_id = dienstplan.perso_id
WHERE personal.status_sum = 'rezeption' AND dienstplan.kw = '$kw' AND dienstplan.schicht!='' AND personal.zeigen='ja' GROUP BY dienstplan.datum");
I want to have a count(codes.lcfruh) listed as in the inputfields listed as follows:
$names = array();
while ($result = $query4 ->fetch_object()) {
$names[] = $result->name;
echo '<p class="taglist1"><input name="" type="text" title="'.implode(', ', $names).'" class="zbroj'.$result->front_lcfruh.'" value="'.$result->front_lcfruh.'"></p>';
}
The result should be an array of 7 input fields with the count of count(codes.lcfruh) for every day in the week. I am getting the correct number of fields (7) and the count in the fields correctly. Now I wanted to list the names in the title="'.implode(', ', $names).'" of the fields that are having one of the codes in the codes table column "lcfruh". The problem is I am getting only one name listed for the first days, 2 names on the second day an so on.... And it is always the first name in the table.
Instead of just selecting the name, make a GROUP_CONCAT on it. As you have a GROUP BY, this gives you all names of that day separated by a comma.
SELECT count(codes.lcfruh) AS front_lcfruh, GROUP_CONCAT(name) AS name
Then in your echo just show the concated names like this:
echo '<p class="taglist1"><input name="" type="text" title="'.$result->name.'" class="zbroj'.$result->front_lcfruh.'" value="'.$result->front_lcfruh.'"></p>';
I want to display the employee's leaves which is same department with me. I only want the employee which same department with me but the output show all of the employee in database
Here is my database
table leave:{Leave_ID(PK), Data_Apply, Leave_Type, Status, Emp_ID(FK)}
table employee: {Emp_ID(PK), Emp_Name, Dept_ID(FK)}
table department: {Dept_ID(PK), Dept_Name, Dept_Desc}
As a example I'm head of department of Marketing and I want to see employee's leave detail who under me and in a same department. I tried to use function in_array to display but fail.
Here is my code
<?php
//$test = mysql_query("select * from employee");
//if(in_array($search["Dept_ID"], array($test)))
$result = mysql_query("select * from `leave`");
if ($result == FALSE)
{
die(mysql_error());
}
while($row = mysql_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row["Leave_ID"];?></td>
<td><?php echo $row["Emp_ID"];?></td>
<td><?php echo $row["Date_Apply"];?></td>
<td><?php echo $row["Leave_Type"];?></td>
<td><?php echo $row["Status"];?></td>
<td>Profile</td>
</tr>
<?php
}
?>
Is there is any function or anything as a suggestion to used. I'm a newbie in programming and sorry for my bad english
$department_id = 4;
$result = mysql_query(' select l.*
from leave as l
join employee as e
on l.emp_id = e.emp_id
where e.dept_id = '.mysql_real_escape_string($department_id));
$department_name = 'this one';
$result = mysql_query(" select l.*
from leave as l
join employee as e
on l.emp_id = e.emp_id
join department as e
on d.dept_id = e.dept_id
where d.dept_name like '%".mysql_real_escape_string($department_name))."%'");
edit
After reading the first comment down there, I think you're saying that you essentially have an employee_id and you want to filter the query by that employee's department. So... here's some code to do that:
http://sqlfiddle.com/#!2/41bf7/1/0
There are two queries there... they're about the same so I would just choose which ever is easier for you to understand. You would add them to the PHP like this (using the first query from the sql fiddle):
$logged_in_employee_id = 1;
$result = mysql_query('select e.emp_id, e.emp_name,
l.date_apply, l.leave_type, l.status,
d.dept_name, d.dept_desc
from `leave` as l
join employee as e
on l.emp_id = e.emp_id
join department as d
on d.dept_id = e.dept_id
where d.dept_id in (
select dd.dept_id
from employee as ee
join department as dd
on dd.dept_id = ee.dept_id
where ee.emp_id = '.mysql_real_escape_string($logged_in_employee_id)).' )');
I'm not sure where you're getting the employee_id or the department_id but make sure you sanitize and validate anything you put into a query like this. I am using mysql_real_escape_string which helps but that query will still break if someone hijacks your POST data (or something) and uses a string instead of an integer value. There are some great posts on StackOverflow about how to do this; just search for sanitizing input, sql injection with PHP, and how to do prepared statements or use PDO.
Try to change your query (at the moment it takes all the records in "leave" table).
Assuming that you know the code of your department, you can use something similar (not tested):
SELECT leave.*, employee.Emp_Name, department.Dept_Name, department.Dept_Desc
FROM leave, employee, department
WHERE leave.Emp_ID=employee.Emp_ID
AND department.Dept_ID=employee.Dept_ID
AND department.Dept_ID="<your department ID>"