I am trying to make a report that will show attendance of who was present in each class by date.
My tables only have each person present in one class.
But my php sql report shows same person in all classes.
Tables - attendant_record, _person, _person_group
<?php
$sql = 'SELECT * FROM _person, attendance_record, _person_group WHERE _person.id = attendance_record.personid ORDER BY
attendance_record.date';
$query = mysqli_query($conn, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($conn));
}
?>
<html>
<head>
<title>Attendance Report</title>
</head>
<body>
<table>
<caption class="title">Attendance Report</caption>
<thead>
<tr>
<th>NO</th>
<th>FirstName</th>
<th>LastName</th>
<th>Class</th>
<th>Present</th>
<th>DATE</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['personid'].'</td>
<td>'.$row['first_name'].'</td>
<td>'.$row['last_name'].'</td>
<td>'.$row['name'].'</td>
<td>'.$row['present'].'</td>
<td>'. date('F d, Y', strtotime($row['date'])) . '</td></tr>
';
$no++;
}?>
</tbody>
</table>
</body>
</html>
image of result page
You need to modify the SQL query to select only the group of each person.
$sql = 'SELECT * FROM _person, attendance_record, _person_group WHERE
_person.id = attendance_record.personid
AND
_person.group_id = _person_group.group_id
ORDER BY
attendance_record.date';
This will select only records from the group table that match the each person.
I assumed the column name group_id but you can change to what is your column name.
EDIT: to filter by date use something like this (order by date is no longer needed if all dates are the same).
$sql = 'SELECT * FROM _person, attendance_record, _person_group WHERE
_person.id = attendance_record.personid
AND
_person.group_id = _person_group.group_id
AND
_attendance_record.date = '2018-09-08';
Related
This is a tricky question to search, hence my post here. I have a a header that displays the sum of all the values in a column of a table that is printed below it. However the table is generated from a MYSQL table and the sum of the column values is calculated as it is generated. So somehow I have to have a variable printed but only after the table is generated and I am not sure how to pass the variables back up to the print statement so it doesn't always print out a 0
I feel like the solution is that the sum should call a script (Javascipt) that generates and prints the table returning the sum of columns to then be printed. But I am not sure how I would do this
echo "
<h3>EPL</h3>
<h5>Total Score: $total</h5>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
<?php
require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT * FROM $tablename ORDER BY position_order";
$users = $mysqli->query($sql);
while($user = $users->fetch_assoc()){
$con = mysqli_connect('localhost', 'root', 'root', 'predictions');
$sql1 = "SELECT * FROM `predictions`.`".$tablename."` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
$sql2 = "SELECT * FROM `predictions`.`epl` WHERE (CONVERT(`title` USING utf8) LIKE '%".$user['title']."%')";
$result = mysqli_query($con, $sql1);
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
$result->close();
$result = mysqli_query($con, $sql2);
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
$total += abs($position1-$position2);
?>
<tr id="<?php echo $user['id'] ?>">
<td><?php echo $user['position_order'] ?></td>
<td><?php echo $user['title'] ?></td>
<td><?php echo abs($position1-$position2); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
To explain the table further, each user has their own table with the same format username_league, so I use some php code to grab the table. Additionally I have a base case table in this case 'epl' in which I compare the tables and calculate the scores based on the absolute difference in the column 'position_order'.
You should always aim for separation of your code and presentation. And beyond that, your database code is extremely inefficient. You're re-creating an entire database object for every instance of your original query.
Just put your PHP code before the HTML, or better yet in a separate file.
<?php
$total = 0;
require('db_config.php');
$tablename = $_SESSION['username'] . "_epl";
$_SESSION['tablename'] = $tablename;
$sql = "SELECT id, position_order, title FROM `$tablename` ORDER BY position_order";
$users_result = $mysqli->query($sql);
while($user = $users_result->fetch_assoc()) {
$users[] = $user;
}
foreach ($users as $user) {
$sql1 = "
SELECT position_order FROM `$tablename` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
UNION
SELECT position_order FROM `epl` WHERE CONVERT(`title` USING utf8) LIKE '%$user[title]%' LIMIT 1
";
$result = $mysqli->query($sql1);
$row = $result->fetch_assoc();
$position1 = $row['position_order'];
$user["position1"] = $position1;
$row = $result->fetch_assoc();
$position2 = $row['position_order'];
$user["position2"] = $position2;
$total += abs($position1 - $position2);
}
?>
There you are; using a single database object, one third fewer queries, all your values in an array. They're ready to use later in the file, or in your separate HTML view:
<h3>EPL</h3>
<h5>Total Score: <?=$total?></h5>
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>
<?php foreach($users as $user):?>
<tr id="<?=$user['id'] ?>">
<td><?=$user['position_order'] ?></td>
<td><?=$user['title'] ?></td>
<td><?=abs($user["position1"]-$user["position2"]); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
I don't know enough about the structure here, but I'd be very surprised if you couldn't make this a single database query.
Output buffering is your friend! Simply call ob_start before outputting the table i.e.
ob_start();
echo "
<table class='table table-bordered'>
<tr>
<th>#</th>
<th>Name</th>
<th>Score</th>
</tr>
<tbody class='row_position'>"?>
...
then once you have done generating the table, you can collect the output of the table's content using ob_get_clean, output the header and then the table content:
...
$table = ob_get_clean();
echo "<h3>EPL</h3>
<h5>Total Score: $total</h5>";
echo $table;
I am creating a medical application for a Drs Practice as part of an assignment.
I have a table which is displaying the information with to the visits table which is linked in the image here.
Currently the table is displaying the Id values of the foriegn keys like this
How do i display the text value for example rather than patient name being 9, i would like to display the actual name and so on for the rest of the table.
so it will display as
patient name | doctor | condition | medication prescribed | Date
Andrew Terry | Dr Iqbal | Depression | Fluoxetine | 20th April 2018
I am also when i have worked this out, wanting to add a search where i can search for patients with a certain condition within a certain time period.
The current code is as follows
<?php // Include config file
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/config.php");
?>
<?php // Include header File
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/header.php");
?>
<?php
if (!$db) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "SELECT * FROM visit JOIN doctor
ON visit.doctor_id = doctor.doctor_id
JOIN patient
ON visit.patient_id = patient.patient_id
WHERE patient.patient_id BETWEEN 1 AND 9
ORDER BY date DESC";
$query = mysqli_query($db, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($db));
}
?>
<body>
<?php // Navigation File
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/nav.php");?>
<div class="container"><br><br>
<?php include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/logo.html"); ?>
<h2>APPOINTMENTS</h2>
<p>Most Recent Appointments:</p>
<table class="table table-striped">
<thead>
<tr>
<th>Patient Name</th>
<th>Doctor</th>
<th>Condition</th>
<th>Medication Prescribed</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['patient_id'].'</td>
<td>'.$row['doctor_id'].'</td>
<td>'.$row['con_id'].'</td>
<td>'.$row['drugs_id'].'</td>
<td>'.$row['date'].'</td>
<td>Delete</td>
</tr>';
$no++;
}?>
</tbody>
</table>
New Appointment
Admin Area
</div>
</body>
</html>
I have amended code following #kamal assistance earlier.. but now i am getting undefined notice errors.
Code is as follows
<?php // Include config file
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/config.php");
?>
<?php // Include header File
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/header.php");
?>
<?php
if (!$db) {
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}
$sql = "$sql = "SELECT patient.fName AS pfname, patient.sName AS psname, doctor.sName AS doc, conditions.con_name AS con, drugs.medication AS meds, visit.visitdate FROM visit
JOIN doctor ON visit.doctor_id = doctor.doctor_id
JOIN patient ON visit.patient_id = patient.patient_id
LEFT JOIN conditions ON visit.con_id = conditions.con_id
LEFT JOIN drugs ON visit.drugs_id = drugs.med_id
WHERE patient.patient_id BETWEEN 1 AND 100
ORDER BY date DESC";
$query = mysqli_query($db, $sql);
if (!$query) {
die ('SQL Error: ' . mysqli_error($db));
}
?>
<body>
<?php // Navigation File
include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/nav.php");?>
<div class="container"><br><br>
<?php include("$_SERVER[DOCUMENT_ROOT]/freddies/inc/logo.html"); ?>
<h2>APPOINTMENTS</h2>
<p>Most Recent Appointments:</p>
<table class="table table-striped">
<thead>
<tr>
<th>Patient Name</th>
<th>Doctor</th>
<th>Condition</th>
<th>Medication Prescribed</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['pfName'].'</td>
<td>'."Dr ".$row['doc'].'</td>
<td>'.$row['con'].'</td>
<td>'.$row['meds'].'</td>
<td>'.$row['date'].'</td>
<td>Delete</td>
</tr>';
$no++;
}?>
</tbody>
</table>
New Appointment
Admin Area
</div>
</body>
</html>
I am now getting the following. enter image description here
I assume few of the tables in the following answer.
Here how your query should look like as #Barmar mentioned in comment:
SELECT fName,date,visit_id,patient.patient_name, doctor.name AS doctor_name, condition.name AS condition_name, drugs.name AS drugs_name FROM visit JOIN doctor
ON visit.doctor_id = doctor.doctor_id
JOIN patient
ON visit.patient_id = patient.patient_id
LEFT JOIN condition ON visit.con_id = condition.id
LEFT JOIN drugs ON visit.drugs_id = drugs.id
WHERE patient.patient_id BETWEEN 1 AND 9
ORDER BY date DESC
Then within your PHP code, you should change the column names in while loop as below:
echo '<tr>
<td>'.$row['patient_name'].'</td>
<td>'.$row['doctor_name'].'</td>
<td>'.$row['condition_name'].'</td>
<td>'.$row['drugs_name'].'</td>
<td>'.$row['date'].'</td>
<td>Delete</td>
</tr>';
I hope this will help you to move forward.
I've done something similar to this table below quite a few times, where I use a while statement to populate the <td> section of a table, but I've never done it before where part of the <td> is populated with a foreach statement, and it's confusing me.
In this example, I create a table, then I populate the first column with a list of suppliers, based on the number of tables inside of my database. (each supplier has its own table).
$supplierList = array();
$showTable = "SHOW TABLES from dbOne";
$getSuppliers = mysqli_query($con, $showTable);
while ($row = mysqli_fetch_row($getSuppliers)) {
$supplierList[] = $row;
}
$supplierList = array_reduce($supplierList, 'array_merge', array());
Now I have my array that contains the list of all my suppliers, and I use that to generate the <td>s in my table.
<table>
<thead>
<th style="text-align: center;">Supplier</th>
<th style="text-align: center;">Earliest line</th>
<th style="text-align: center;">Latest line</th>
<th style="text-align: center;"># of total lines</th>
</thead>
<?php
foreach ($supplierList as $subList) {
$supplierName = $subList;
$earlyExp = "SELECT date FROM $subList ORDER BY date DESC LIMIT 1" ;
$earlyExpQuery = mysqli_query($con, $earlyExp);
$lateExp = "SELECT date FROM $subList ORDER BY date ASC LIMIT 1" ;
$lateExpQuery = mysqli_query($con, $lateExp);
$countLines = "SELECT * from $subList";
$countLinesQuery = mysqli_query($con, $countLines);
$countLinesCount = mysqli_num_rows($countLinesQuery);
while ($row = mysqli_fetch_array($earlyExpQuery)) {
$earlyExpDate = $row['date'];
?>
<tbody>
<td><img src = "/img/suppliers/<?= $supplierName ?>.png"></td>
<td style="text-align: center;"><?= $earlyExpDate ?></td>
<td style="text-align: center;"><?= $lateExpDate ?></td>
<td style="text-align: center;"><?= $countLinesCount ?></td>
</tbody>
<?php
}
}
?>
</table>
The table itself builds correctly, and displays each supplier in a unique row. I cannot figure out though how to populate the other parts of the row with the information based off the unique supplier in the foreach statement.
Okay, I sorted this out. In case it helps anyone else, here's how I did it:
This code goes in the same spot as my post above - so in between the header and the body of the table. I did not need to use any <tr>.
<?php
foreach ($supplierList as $subList) {
$supplierName = $subList;
$earlyExp = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne ASC LIMIT 1" ;
$earlyExpQuery = mysqli_query($con, $earlyExp);
$lateExp = "SELECT * FROM $subList where dateOne != '' UNION SELECT * from $subList where dateTwo != '' ORDER BY dateTwo, dateOne DESC LIMIT 1" ;
$lateExpQuery = mysqli_query($con, $lateExp);
$countLines = "SELECT * from $subList";
$countLinesQuery = mysqli_query($con, $countLines);
$countLinesCount = mysqli_num_rows($countLinesQuery);
while ($row = mysqli_fetch_array($earlyExpQuery)) {
$earlyExpDate = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
}
while ($row = mysqli_fetch_array($lateExpQuery)) {
$lateExpDate = $row['dateTwo'] ? $row['dateTwo'] : $row['dateOne'];
?>
They key is that I had to close off each individual while statement except the last one - closing that after I closed the </tbody> tag but before the </table>, like so:
</tbody>
<?php
}
}
?>
</table>
I have this pagination script
1 Show-page
$result_p = mysqli_query($conexao, "select count(*) as count FROM `banner-destaque`");
$row_p = mysqli_fetch_array($result_p);
$quant_resul = 10;
$pagina = 1;
$paginas = ceil($row_p['count'] / $quant_resul);
$result = mysqli_query($conexao, "select * FROM `banner-destaque` limit 0 , " . $quant_resul);
The script works fine to one mysql table, but I need to reuse the same pagination script for other mysql tables with diferent sizes.
This is the table that show te results
<table class="flat-table flat-table-1">
<thead>
<th>Id</th> **//Here i need get tables columns name to make it dinamically for reuse in other tables**
<th>Photo</th>
<th>Title</th>
<th>Description</th>
</thead>
<tbody>
<tr>
<?php
while ($row = mysqli_fetch_object($result)) {
echo
'
//and here i need get all columns values to populate the above column.
<td>'.$row->id.'</td>
<td>'.$row->foto.'</td>
<td>'.$row->titulo.'</td>
<td>'.$row->descricao.'</td>';
};
?>
Any solution? Thanks. I'm using mysqli to connect to the database.
make a function from the query script and then call it passing the database table name to it as a parameter. You can then re-use this for different tables
function paginate($conexao, $tblname){
$result_p = mysqli_query($conexao, "select count(*) as count FROM ".$tblname);
$row_p = mysqli_fetch_array($result_p);
$quant_resul = 10;
$pagina = 1;
$paginas = ceil($row_p['count'] / $quant_resul);
$result = mysqli_query($conexao, "select * FROM ".tblname." limit 0 , " . $quant_resul);
return $result;
}
<table class="flat-table flat-table-1">
<thead>
<th>Id</th> **//Here i need get tables columns name to make it dinamically for reuse in other tables**
<th>Photo</th>
<th>Title</th>
<th>Description</th>
</thead>
<tbody>
<tr>
<?php
$result = paginate($conexao, 'banner-destaque');
while ($row = mysqli_fetch_object($result)) {
echo
'
//and here i need get all columns values to populate the above column.
<td>'.$row->id.'</td>
<td>'.$row->foto.'</td>
<td>'.$row->titulo.'</td>
<td>'.$row->descricao.'</td>';
};
?>
I have created these 3 columns in my mysql table so I can have a list of each users transactions.
ticket_date, ticket_num, ticket_result
I also created a function to echo the value in those columns and it worked successfully.
function.php:
$sql = "SELECT *
FROM members
WHERE user_id = '{$_SESSION['user_id']}'";
$query = $this->db_connection->query($sql);
while ($row = $query->fetch_object()) {
global $me, $me2, $date;
$me = $row->ticket_num;
$me2 = $row->ticket_result;
$date = $row->ticket_date;
}
transactions.php
<table class="table">
<thead>
<th>Date</th>
<th>Ticket ID</th>
<th>Result</th>
</thead>
<tr>
<td><?php echo $date; ?> </td>
<td><?php echo $me; ?> </td>
<td><?php echo $me2; ?> </td>
</tr>
</table>
My problem now is that if a user has more than one transaction how would I be able to echo each value from a particular column separately. I am trying to echo each transaction into a .
Would I be able to store each value as an array so I could call it like this?
$row->ticket_num['0']
EDIT: I meant to ask how can I store the transactions into their respective columns. Such as storing more than one $ticket_date that apply to each transaction. Could I store information into the columns as an array so I can call each transaction using the array pointer?
I think the best thing to do, would be not to use global variables for this, unless there is any reason that your function can't return this data, i.e. it's returning something else. You could have something like this in your function.php:
$sql = "SELECT *
FROM members
WHERE user_id = '{$_SESSION['user_id']}'";
$query = $this->db_connection->query($sql);
$return = array();
while ($row = $query->fetch_object()) {
array_push($return, array($row->ticket_num, $row->ticket_result, $row->ticket_date ));
}
return $return
Then you can do this in your transactions.php:
<table class="table">
<thead>
<th>Date</th>
<th>Ticket ID</th>
<th>Result</th>
</thead>
<?php
$ticket = Whatever_Function();
foreach($ticket as $t){
echo"<tr> <td> ".$t[0]." </td><td>".$t[1]."</td><td>".$t[2]."</td></tr>";
}
?>
</table>
Edit
In relation to your additional question in the comments, a database structure like this should be set up:
By doing this, you are separating it, so that every table belongs to one thing or action. These can then be related to each other using an SQL Join like this:
SELECT * FROM Transactions tr
JOIN Users u ON u.UID = tr.UID
JOIN Tickets ti ON ti.TID = tr.TID
By looking at the above SQL code snippet, you should be able to see how it matches up the columns on the different tables. This creates one big virtual table that you can then search for stuff with, where their column names prepended with their given pseudonyms.
So if you wanted to get the email address of everyone that bought the ticket whose price was over £20, even though the tables you need aren't directly connected you could do:
SELECT u.email FROM Transactions tr
JOIN Users u ON u.UID = tr.UID
JOIN Tickets ti ON ti.TID = tr.TID
WHERE ti.Price > 20
Do something like this:
global $me, $me2, $date;
while ($row = $query->fetch_object()) {
$me[] = $row->ticket_num;
$me2[] = $row->ticket_result;
$date[] = $row->ticket_date;
}
transactions.php
<table class="table">
<thead>
<th>Date</th>
<th>Ticket ID</th>
<th>Result</th>
</thead>
<?php foreach($me as $k => $m){
echo "<tr>";
echo "<td>".$date[$k]."</td>";
echo "<td>".$m."</td>";
echo "<td>".$me2[$k]."</td>";
echo "</tr>";
?>
</table>