Display table count in html - php

I trying to create the backend for a booking system and need to show all booked appointments on each date for each location. My appointments table looks like this:
id, booked_date, location_id, customer_id
and I would like to display this in a html table like this:
Date 1
Date 2
Location 1 Name
Number of booked appointments
Number of booked appointments
Location 2 Name
Number of booked appointments
Number of booked appointments
I have a separate table of Locations that has full details like and and address.
I also have a table of event dates (id, start_date)
Im struggling to comprehend what I need to do here!
EDIT:
I just need some help putting everything together into the example table above.
Database tables : appointments, locations, event_dates
I have this query and the function below - SELECT t1.location_id, t1.start_datetime, t2.name, COUNT(*) AS count FROM appointment t1 INNER JOIN location t2 ON t1.location_id = t2.id GROUP BY t1.location_id, t1.start_datetime
function countAppointment() {
require 'config.php';
$sql = "SELECT t1.location_id, t1.start_datetime, t2.name, COUNT(*) AS count
FROM appointment t1
INNER JOIN location t2
ON t1.location_id = t2.id
GROUP BY t1.location_id, t1.start_datetime";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$a1 = $row['start_datetime'];
$a2 = $row['location_id'];
$a3 = $row['name'];
$a4 = $row['count'];
//echo "$a1 <br> $a2 <br> $a3 <br> $a4 <br><br>";
echo "<tr><th scope='row'>$a3</th>";
echo "<td><a href='#'>$a4</a></td>";
echo "<td>9</td><td>27</td><td>14</td></tr>";
}
} else {
return "0";
}
$conn->close();
}

try this code
<?php
$con = new mysqli("localhost","root","","test");
// Check connection
if ($con -> connect_errno) {
echo "Failed to connect to MySQL: " . $con -> connect_error;
exit();
}
$data = array();
$date_list= array();
$sql_date_list ="SELECT start_datetime from appointment GROUP BY start_datetime";
$result_date_list = mysqli_query($con, $sql_date_list);
$sql_loc_list = "SELECT `name` FROM `location` GROUP BY `name`";
$result_loc_list = mysqli_query($con, $sql_loc_list);
$k=0;
while ($row =mysqli_fetch_assoc($result_loc_list)){
$data +=[$row['name']=>array()];
if($k==0){
while($row2 =mysqli_fetch_assoc($result_date_list)){
array_push($date_list,$row2['start_datetime']);
$data[$row['name']]+=[$row2['start_datetime']=>0];
}
$k++;
}else{
foreach($date_list as $date){
$data[$row['name']]+=[$date=>0];
}
}
}
$sql_getdata = "SELECT t2.name as loc_name, t1.start_datetime, COUNT(t1.location_id) AS count FROM appointment t1 JOIN location t2 ON t1.location_id = t2.id GROUP BY t1.location_id,t1.start_datetime";
$result = mysqli_query($con, $sql_getdata);
while($row =mysqli_fetch_assoc($result)){
$data[$row['loc_name']][$row['start_datetime']]=$row['count'];
}
$table="<table border='1'>";
$table.="<tr>";
$table.="<th>location</th>";
foreach ($date_list as $date) {
$table.="<th>".$date."</th>";
}
$table.="</tr>";
foreach ($data as $key=>$date) {
$table.="<tr>";
$table.="<td>".$key."</td>";
foreach($date as $key2=>$count){
$table.="<td>".$count."</td>";
}
$table.="</tr>";
}
$table.="<table>";
echo $table;
?>
output

Related

Get variable in sql statment and use in php

The function below generates a table like the image below.
The location_id is being accessed but I would like to use this ID again at the bottom of the table in a link. See the LOCATION_ID at the bottom of the function.
Im not sure how to do this.
function countAppointment2() {
require 'config.php';
$data = array();
$date_list= array();
$sql_date_list ="SELECT start_date from appointment GROUP BY start_date";
$result_date_list = mysqli_query($conn, $sql_date_list);
$sql_loc_list = "SELECT `name` FROM `location` GROUP BY `name`";
$result_loc_list = mysqli_query($conn, $sql_loc_list);
$k=0;
while ($row =mysqli_fetch_assoc($result_loc_list)){
$data +=[$row['name']=>array()];
if($k==0){
while($row2 =mysqli_fetch_assoc($result_date_list)){
array_push($date_list,$row2['start_date']);
$data[$row['name']]+=[$row2['start_date']=>0];
}
$k++;
}else{
foreach($date_list as $date){
$data[$row['name']]+=[$date=>0];
}
}
}
$sql_getdata = "SELECT t2.name as loc_name, t1.start_date,t1.location_id, COUNT(t1.location_id) AS count FROM appointment t1 JOIN location t2 ON t1.location_id = t2.id GROUP BY t1.location_id,t1.start_date";
$result = mysqli_query($conn, $sql_getdata);
while($row =mysqli_fetch_assoc($result)){
$data[$row['loc_name']][$row['start_date']]=$row['count'];
}
$table="<table class='table table-bordered'>";
$table.="<thead><tr>";
$table.="<th>Dealership Location</th>";
foreach ($date_list as $date) {
$edate = date("d/m/Y", strtotime($date));
$table.="<th>".$edate."</th>";
}
$table.="</tr></thead>";
foreach ($data as $key=>$date) {
$table.="<tbody><tr>";
$table.="<td>".$key."</td>";
foreach($date as $key2=>$count){
$table.="<td>".$count."</td>";
}
$table.="</tr>";
}
$table.="</tbody></table>";
echo $table;
}
Change
$data[$row['loc_name']][$row['start_date']]=$row['count'];
to
$data[$row['loc_name']][$row['start_date']] = [
'count' => $row['count'],
'location_id' => $row['location_id']
];
Then replace the code in the loop to:
$table.="<td>".$count['count']."</td>";
Essentially changing it from a single value to an array.

PHP how to find same values in column

Hi i'm quite newbie in php language and i need to find same values in DESCRIPTION column in my database table.
id-------DESCRIPTION
1-------Final
2-------Exam
3-------Test
4-------Test
5-------Mid
6-------Quiz
7-------Quiz
As output it needs to be like:
Final
Exam
Test
Test
Mid
Quiz
Quiz
If a value repating just change them style is enough but i'm really don't know how to do it.
<?php
$check = mysqli_query($connection,"SELECT * FROM EXAMS");
while($test=mysqli_fetch_array($check))
{
echo $test["DESCRIPTION"];
}
?>
Use an EXISTS subquery to look if the same value exists for another id:
$result = $connection->query("
SELECT e.id, e.DESCRIPTION, EXISTS(
SELECT *
FROM EXAMS e2
WHERE e2.DESCRIPTION = e.DESCRIPTION
AND e2.id <> e.id
) as is_duplicate
FROM EXAMS e
ORDER BY e.id
");
Then check in PHP if it is a duplicate (if ($row['is_duplicate'] == 1)) and mark it bold:
while($row = $result->fetch_assoc())
{
if ($row['is_duplicate'] == 1) {
echo "<strong>$row['DESCRIPTION']</strong><br>";
} else {
echo "$row['DESCRIPTION']<br>";
}
}
PHP solution
$result = $connection->query("SELECT * FROM EXAMS");
$data = $result->fetch_all(MYSQLI_ASSOC);
$counts = array_count_values(array_column($data, 'DESCRIPTION'));
foreach($data as $row)
{
if ($counts[$row['DESCRIPTION']] > 1) {
echo "<strong>$row['DESCRIPTION']</strong><br>";
} else {
echo "$row['DESCRIPTION']<br>";
}
}

Fetch assoc error

Hello
i'm trying to make a table with this query but i cant get it to work. the thing is that it cant select 2 query's at a time but i dont know another way for it..
$active_ids = '1, 3, 4';
$query = "SELECT * FROM users WHERE id IN ({$active_ids})";
$result = $mysqli->query($query);
$query = "SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
echo $row['username'], "<br/>";
echo $row['n'], "<br/>";
}
Try this:
$active_ids = '1, 3, 4';
$result = $mysqli->query("
(SELECT * FROM users WHERE id IN ({$active_ids}))
UNION
(SELECT dj, count(*) AS n FROM timetable WHERE dj IN ({$active_ids}) GROUP BY dj)
") ;
if (!$rec = mysqli_fetch_array($result)) {
echo ("Sorry, no records found");
}
else {
do {
echo ($rec["username"]);
echo "<br />";
echo ($rec["n"]);
echo "<br />";
}
while ($rec = mysqli_fetch_array($result));
}
$active_ids = '1, 3, 4';
$query = "SELECT users.* from users
LEFT JOIN timetable ON users.id=timetable.dj
WHERE users.id IN ({$active_ids})
UNION
SELECT timetable.dj,timetable.count(*) as n from timetable
RIGHT JOIN users ON timetable.dj=users.id
WHERE timetable.dj IN ({$active_ids}) GROUP BY timetable.dj
";
$result = $mysqli->query($query);
while($row = $result->fetch_assoc()){
echo $row['username'], "<br/>";
echo $row['n'], "<br/>";
}
I don't know it is certainly true but can you try this code ?

Selecting rows from multiple tables

I would like to know what the fastest way is to make the following SQL call using PHP. I am using procedural mySQLi.
$dcount = "SELECT max(id) FROM deposits";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(id)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
//second query
$ucount = "SELECT max(int) FROM anothertable";
$result2 = mysqli_query($conn,$ucount);
if (mysqli_num_rows($result2) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$count = $row["max(int)"];
echo $count;
echo ",";
}
} else {
echo '0';
}
Is there a way to make the execution faster than like this, maybe to echo the results from both queries after the first if statement?
It just seems rather long to me.
Thanks in advance.
SELECT max(id) as max_id, (SELECT max(int) as max_int FROM anothertable) as max_int
FROM deposits
Not tested, but something like it should work
$dcount = "
SELECT max(id) as max,'deposit' as table_name FROM deposits
UNION
SELECT max(id) as max,'another' as table_name FROM anothertable
";
$result = mysqli_query($conn,$dcount);
if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo $row["table_name"].":".$row["max"]."\n";
}
} else {
echo '0';
}
SELECT d.max(id) as d_max_id, a.max(int) as a_max_int FROM deposits as d JOIN anothertable as a ON d.id = a.id;
is what you need for multiple tables
$row['d_max_id']
will give you deposits.max(id) now
You have to edit the d.id = a.id accordingly to what you want the two tables to match on
If you cant join try this:
SELECT max(id) as max_id, (SELECT max(int) FROM anothertable) as max_int FROM deposits;
SELECT max(id) as max_id, max(int) as max_int FROM deposits ,anothertable

select fields and group them in a category in php and mysql

i have a table with staff_id and subjects, i want to display all staffs according to their subjects.
my table
result i want
Physics
-001
-004
-006
Chemistry
-002
-009
Biology
-003
-008
Mathematics
-005
My code
$q = mysql_query("Select staff_id from my_table");
while($row = mysql_fetch_array($q)){
echo $subject .'</br>';
echo $staff_id.'</br>';
}
but this doesn't give the result i want.
any help?
What you need is ORDER BY.
Change your query to:
SELECT STAFF_ID, SUBJECT FROM my_table ORDER BY SUBJECT, STAFF_ID
So you get the records in the right order to work with them.
Something like this?
$q = mysql_query("SELECT `staff_id`, `subject` FROM `my_table`;");
$data = array();
while($row = mysql_fetch_array($q)){
$data[$row['subject']][] = '-'.$row['staff_id'];
}
print_r($data);
Or to echo out the rows
foreach($data as $heading => $rows){
echo $heading.'<br>';
foreach($rows as $row){
echo $row.'<br>';
}
}
You can write your code like this below:
$q = mysql_query("SELECT * FROM my_table ORDER BY SUBJECT, STAFF_ID");
while($row = mysql_fetch_array($q)){
//Do staff
}
The following code should help. You should split each subject into a separate array within your query. Once your query is complete, you should iterate through the subject array, and then within each staff id.
$subjects = array();
$q = mysql_query("Select staff_id from my_table");
while($row = mysql_fetch_array($q)){
if ($subjects[$row['SUBJECT']] == nil) {
$subjects[$row['SUBJECT']] = array();
}
array_push($subjects[$row['SUBJECT']], $row['STAFF_ID']);
}
foreach ($subjects as $key=>$value) {
echo $key . '<br>;
foreach ($vaue as &$staff) {
echo $staff . '<br>';
}
}
$result=mysql_query("SELECT * from table GROUP BY subject");
while($ext=mysql_fetch_object($result)) {
$query=mysql_query(" SELECT * from table WHERE subject='".$ext->subject."'");
echo $ext->subject;
while($res=mysql_fetch_object($query)) {
echo $res->staff_id;
}
}

Categories