create dynamic reports using core php and mysql - php

Currently we are doing a project to manage projects and our client need a dynamic reports which he can get Time Sheet Project wise.
Let me explain the situation.
3 database tables.
Project table, user table and task table.
This is the sample reports format with a sample data, but structure is the same.
Multiple users can work on a single project and user can work on multiple projects at the same time.
This is what i have tried to up to now.
<?php
/////get projects which are not finished/////
$sql_project = "SELECT * FROM project where P_Status!='finished'";
$result_project = mysql_query($sql_project) or die("Error in query: $sql_project.".mysql_error());
////////get users from the users table
$sql_getUsers="select * from tbl_user where status='Active' order by U_Name";
$result_getUsers=mysql_query($sql_getUsers);
?>
<tr>
<td>Project</td>
<td>Client</td>
<td>Estimated Cost</td>
<td >Total spent to date </td>
<?php
while($row_getUsers=mysql_fetch_array($result_getUsers))
{
?>
<td ><?php echo $row_getUsers['U_Name']; ?></td>
<?php
}
?>
</tr>
<?php
while($row_project=mysql_fetch_array($result_project))
{
$individualProject=trim($row_project['P_Title']);
$sql_tasks = "Select SUM(Time)as totalTime from tbl_txn where project like '%$individualProject%' and Date<='$endDate'";
$result_sql = mysql_query($sql_tasks) or die("Error in query: $sql_file_no.".mysql_error());
?>
<?php
while($row_sql_tasks=mysql_fetch_array($result_sql))
{
?>
<tr>
<td><?php echo $row_project['P_Title']; ?></td>
<td ><?php echo $row_project['P_Client']; ?></td>
<td ><?php echo $row_project['estimatedCost']; ?></td>
<td ><?php echo round($row_sql_tasks['totalTime']); ?></td>
<td><?php echo round($row_getIndiUserTime['Time']); ?></td>
<?php
}
}
?>
I have managed to display the users, but i have no idea how to display users have work on each project?.
on project A, how much time User 1 has spent and user 2 like wise.
hope i have clearly mentioned about the problem.
can anyone help me on this please?
Thanks in advance

The idea to do this is to join a table in mysql.
Here is a resource you can learn how to use mysql join table and show the data accordingly. Hope this will help you.

Related

Fetch data that don't exist in a table with a specific id from another table

I'm new to MySQL and PHP and I need your help.
I have made a web application for the check-ins that employees make at the company.
I have an SQL table named tblemployees that has the employees' emp_id, Name, Email ID etc.
The other table is named tblentries and has all the entries of each employee and variables such as id. emp_id, Name, Date, Hour etc.
The primary key of tblemployees is emp_id and emp_id is the foreign key of the tblentries table.
I want to make a table in my web app that shows the pending checks of the day.
To be more specific, my code so far is:
<div class = "pb-20" id = "tab">
<table class = "data-table table stripe hover nowrap">
<thead>
<tr>
<th class = "table-plus">Full Name</th>
<th>Email</th>
<th>1st Comp.</th>
<th>2nd</th>
<th>3rd</th>
<th>4th</th>
<th>5th</th>
<th>Department</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$sql = "SELECT tblemployees.Name, tblemployees.EmailId, tblemployees.Company1, tblemployees.Company2, tblemployees.Company3, tblemployees.Company4, tblemployees.Company5, tblemployees.Department, tblemployees.role FROM tblemployees LEFT JOIN tblentries ON tblentries.Date < '$todaysdate'";
$query = mysqli_query($conn, $sql) or die(mysqli_error());
while ($row = mysqli_fetch_array($query)) {
?>
<td class = "table-plus">
<div class = "name-avatar d-flex align-items-center">
<div class = "txt">
<div class = "weight-600"><?php echo $row['Name']; ?></div>
</div>
</div>
</td>
<td><?php echo $row['EmailId']; ?></td>
<td><?php echo $row['Company1']; ?></td>
<td><?php echo $row['Company2']; ?></td>
<td><?php echo $row['Company3']; ?></td>
<td><?php echo $row['Company4']; ?></td>
<td><?php echo $row['Company5']; ?></td>
<td><?php echo $row['Department']; ?></td>
<td><?php echo $row['role']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
*'$todaysdate' is a variable that stores today's date. It has the same format as tblentries.Date.
Whatever I have tried so far I haven't managed to show the right results.
I have made some entries for testing the app and the table that I want to make shows the names of the employees whose entries I have made for testing. (The test entries have made in the past, so their dates are smaller than $todaysdate.
I don't know which is better to use and how Not In, Not Exists or a Left Join?
Thank you :)
Found the solution. I used WHERE NOT EXISTS(), so as to fetch the names etc of those who belong to tblemployees table and do not have an entry for CURATE() in tblentries table :
$sql = "SELECT DISTINCT tblemployees.Name, tblemployees.EmailId, tblemployees.Company1, tblemployees.Company2, tblemployees.Company3, tblemployees.Company4, tblemployees.Company5, tblemployees.Department, tblemployees.role
FROM tblemployees
WHERE NOT EXISTS ( SELECT DISTINCT tblentries.emp_id, tblentries.Date FROM tblentries WHERE tblentries.emp_id = tblemployees.emp_id AND tblentries.Date = '$todaysdate')";

Retrieve User data from user id

I have just started learning PHP, so please bear with me. I have two tables in my database, users and appointments tables. The appointments table has idUsers as a foreign key for the id field in users table. I'm trying to display data from appointments table based on current user logged in. The problem is, it displays data from all users and not the one who logged in.
I'm using fetch_assoc() to get the data from the database.
<?php
while($row = $rows->fetch_assoc()): ?>
<td><?php echo $row['idApp'] ?></td>
<td class="col-md-10"><?php echo $row['fname'] ?> </td>
<td class="col-md-10"><?php echo $row['lname'] ?> </td>
<td class="col-md-10"><?php echo $row['email'] ?> </td>
<td class="col-md-10"><?php echo $row['phone'] ?> </td>
<td class="col-md-10"><?php echo $row['date'] ?> </td>
<td class="col-md-10"><?php echo $row['time'] ?> </td>
<td class="col-md-10"><?php echo $row['message'] ?> </td>
</tr>
<?php endwhile; ?>
The expected results would be for the user to log into the system and only see the data , that they have stored against their id.
you should to make "one to one relationship". for example you have two tables users and posts. the posts table must be some like: id,user_id,title,...
so when you want to get posts articles by specific user run this query:
$query = 'SELECT * FROM `posts` WHERE `user_id`= '.$_GET['id'].'';
however do not foget SQL injection!
learn PDO :)

Display own data only for users in PHP

I currently have made a page that shows all the users, but I just want to only display the currently logged in user, Please help me im begging you I spent 4 hours of nothing T_T im new at php and im very lost, im so hopeless right now
<?PHP
$customerquery=mysql_query("select * from customerinfo");
while($customerrows=mysql_fetch_array($customerquery)){
?>
<tr>
<td>Id</td><td>First Name</td><td>Last Name</td><td>Address</td><td>Contact No</td> <td>Username</td><td>Password</td><td>Edit</td>
</tr>
<tr>
<td><?PHP echo $customerrows['id'];?></td>
<td><?PHP echo $customerrows['fname'];?></td>
<td><?PHP echo $customerrows['lname'];?></td>
<td><?PHP echo $customerrows['address'];?></td>
<td><?PHP echo $customerrows['contactno'];?></td>
<td><?PHP echo $customerrows['username'];?></td>
<td><?PHP echo $customerrows['password'];?></td>
<td>edit</td>
</tr>
<?PHP } ?>
You need to specify a WHERE condition in your query. That will fetch only one row, so you will not need a while loop:
$id = (int) $_GET['id']; // assuming you pass user id in URL
$customerquery = mysql_query("select * from customerinfo WHERE id = $id");
$customerrows = mysql_fetch_array($customerquery);
// rest of your html

Formatting database info to rows of repeating blocks

I have a database containing several rows of event info (ie state, date, time, location, etc). I have a php homepage and using PDO database connection. I am looking to pull in each row of the database and have them display within a php include that is a styled table-ized block. I want these blocks to repeat side-by-side, displaying each events information in a grid-like pattern.
e.g.
Evt1 Evt2 Evt3
Evt4 Evt5 Evt6
Evt7 Evt8 Evt9
I have successfully connected to my database, and pulled/displayed individual records.
I need to call all the database information (which I believe I am doing)
Align columns to associated variables in include page
Repeat include page with next row from database, so on and so forth
If anyone can steer me in the right direction on where to turn my research, it would be greatly appreciated. The terminology is new to me, so narrowing my search results would be a huge start. Any piece of the puzzle would be greatly welcomed. The evolution of what I have figured out from countless of hours on here and other forums is below. If anyone would like further info, please don't hesitate.
Variables code:
<?php
$sql = "SELECT * FROM event_info";
foreach ($connection->query($sql) as $row) {
}
$state = $row['state'];
$city_state = $row['city_state'];
$regURL = $row['RegURL'];
$date = $row['date'];
$time = $row['time'];
$location = $row['location'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$connection = null;
?>
Include code:
<div id="eventInfo">
<div class="group">
<h2><?php echo $state; ?>:</h2>
<table>
<tr>
<td colspan="2" class="localReg">
<div class=location><?php echo $city_state; ?></div>
<div class=regNow>Register Now</div>
</td>
</tr>
<tr>
<td width="55">Date:</td>
<td width="185"><?php echo $date; ?></td>
</tr>
<tr>
<td id="everyOther">Time:</td>
<td id="everyOther"><?php echo $time; ?></td>
</tr>
<tr>
<td>Location:</td>
<td><?php echo $location; ?></td>
</tr>
<tr>
<td id="everyOther">Address:</td>
<td id="everyOther"><?php echo $address1; ?><br><?php echo $address2; ?></td>
</tr>
</table>
</div>

Getting contenteditable div's current value and update database

I'm new to web development and I'm trying to create editable database interface. I have got checkboxes in every row and an edit button. I need to update table(in database) when button clicked(rows that are checked). However I can't get current values in cells. I tried contentedittable div to display attiributes.
<?php while ( $rows = mysql_fetch_array($result)): ?>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<?php echo $rows['UniqueID']; ?>]" type="checkbox" id="checkbox[<?php echo $rows['UniqueID']; ?>]" value="<?php echo $rows['UniqueID']; ?>"></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Timestamp']; ?></div></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Name']; ?></div></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Email']; ?></div></td>
<?php endwhile; ?>
I also tried to give unique names to divs, but it didn't work. I want to get current values from there if possible.
<?php
if( ! empty($_POST['edit']) ){
$query = 'UPDATE `asd` SET `Timestamp` = '????' WHERE `UniqueID`='.(int)$id;
mysql_query($query);
}
?>
Checkboxes work fine when I delete rows from table. Any suggestions ?
Thanks.
There are several javascript libraries that help with this problem:
x-editable http://vitalets.github.io/x-editable/ (bootstrap)
datatables https://datatables.net/ (jquery)
I had a similar issue that I solved here:
http://www.abrandao.com/2019/12/saving-contenteditable-html-using-php/
basically, it involves reading the HTML and using PHP parser to exctract the tag and the update the data

Categories