AVG function in SELECT affects the number of rows in query output - php

Below is my Query:
$query = "
SELECT *
FROM Teacher t
INNER JOIN Session s ON t.TeacherId = s.TeacherId
JOIN Grade_Report gr ON s.SessionId = gr.SessionId
WHERE
('".mysql_real_escape_string($sessionid)."' = '' OR gr.SessionId = '".mysql_real_escape_string($sessionid)."') ";
The results are stored in a table which code looks like this:
<table border='1'>
<tr>
<th>Session ID</th>
<th>Student Username</th>
<th>Student Name</th>
<th>Mark</th>
<th>Grade</th>
</tr>
<?php
while ($row = mysql_fetch_array($result)) {
echo "
<tr>
<td>{$row['SessionId']}</td>
<td>{$row['StudentUsername']}</td>
<td>{$row['StudentForename']} {$row['StudentSurname']}</td>
<td>{$row['Mark']}</td>
<td>{$row['Grade']}</td>
</tr>";
}
This query outputs 13 rows in the results which is fine when it selects all rows.
Now what happens is that I want the average mark for each session. So I include 'AVG(gr.Mark) as AvgMark' in the query and include <td>{$row['AvgMark']}</td> in the table. The Calculation it outputs is correct The problem is that the query now only outputs 1 row which is the top row of the query result. I understand this as AVG(gr.Mark) only requires one row when outputted.
So my question is that except using SELECT'AVG(gr.Mark) as AvgMark' in the query to find average marks of each session, is there a way I can workout average of each mark outside the query by using php so that the query output shows the 13 rows and is not affected and I can store the calculation of the average below the table rather than in the table?

The reason AVG returns one row is that it's an aggregate function, and without a GROUP BY clause operates on all rows.
Averages in PHP are just like averages everywhere else; total your values and divide by the number of samples. Hence, you can easily calculate the average in code by changing the code to:
<?php
$total = 0;
$count = 0;
while ($row = mysql_fetch_array($result)) {
$count++;
$total += $row['Mark'];
echo "
<tr>
<td>{$row['SessionId']}</td>
<td>{$row['StudentUsername']}</td>
<td>{$row['StudentForename']} {$row['StudentSurname']}</td>
<td>{$row['Mark']}</td>
<td>{$row['Grade']}</td>
</tr>";
}
$average = (int)($total/$count);
echo "<tr><td colspan=3></td><td>Average</td><td>$average</td></tr>".

Related

php table <th> and <td> from Database Selected but they are not Matching in the right indexing position

all Developers.
I am developing School Management System, in the Database, I have two tables one is For Subjects, and the other one is designed for obtained marks of the Subjects and it is called scores.
So I am trying to fetch subject Names as Table Head
I have another Query below this query and I am trying to fetch from scores as table data .
At this point, I failed to match the subject name and its score from the scores table.
Here is my code:
<table class="table table-striped table-bordered">
<head>
<tr>
<?php
// Query for Subject Names
$view_subject = $config->prepare("SELECT * FROM subjects");
$view_subject->execute();
while($row = $view_subject->fetch()){
$sub_name = htmlspecialchars($row['sub_name']);
?>
<th class="text-center"style="background-color:#395C7F;color:#fff;"><?php echo $sub_name;?></th>
<?php } ?>
</tr>
</thead>
<body>
<?php
// Query for Subject Scores
$view_scores = $config->prepare("SELECT * FROM scores INNER JOIN subjects ON scores.score_sub_id = subjects.sub_id WHERE scores.std_rand = :random_id ORDER BY scores.score_sub_id ASC");
$view_scores->execute(['random_id' => $rand_ID]);
while($row = $view_scores->fetch()){
$score_id = htmlspecialchars($row['score_id']);
$score_sub_id = htmlspecialchars($row['score_sub_id']);
$score_mid_amount = htmlspecialchars($row['score_mid_amount']);
$score_final_amount = htmlspecialchars($row['score_final_amount']);
?>
<tr>
<td class="text-black" data-title="Subject"><?php echo $score_mid_amount;?></td>
</tr>
<?php } ?>
</tbody>
</table>
Database images:
1- Subjects table
2- Scores table
** Browser UI **
On your second loop you have entered '<tr>' wrapping each '<td>' that means that each one arrives at a different line , '<td>'s should be as much as there are '<th>' for each line.... so :
<?php
// Query for Subject Scores
$view_scores = $config->prepare("SELECT * FROM scores INNER JOIN subjects ON scores.score_sub_id = subjects.sub_id WHERE scores.std_rand = :random_id ORDER BY scores.score_sub_id ASC");
$view_scores->execute(['random_id' => $rand_ID]);
?>
<tr>
<?php
while($row = $view_scores->fetch()){
$score_id = htmlspecialchars($row['score_id']);
$score_sub_id = htmlspecialchars($row['score_sub_id']);
$score_mid_amount = htmlspecialchars($row['score_mid_amount']);
$score_final_amount = htmlspecialchars($row['score_final_amount']);
?>
<td class="text-black" data-title="Subject"><?php echo $score_mid_amount;?></td>
<?php } ?>
</tr>
</tbody>
</table>
This should fix your table but it will only create one line! if you have more than one line you will need to add another loop to wrap this one and it will create the new '<tr>' outside the inner loop.
BTW: I assume that the 2nd while loop is exactly long as the first one... since you are supposed to have the same amount of <td> per line per <th> if it's not in the same length or not sorted the same way you will have an issue... which can be resolved either by adjusting your SELECT or creating an array with ids and injecting to it the data from the second loop according to the keys brought in the first.

How do I add another mysqli query into a while loop?

So I am carrying out a query and returning the results in the form of a table. The code below works well.
<table>
<thead>
<tr style="text-align: center;">
<th>Activity</th>
<th>Description</th>
<th>Frequency</th>
<th>Mandatory</th>
<th>Added Yet</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $conn->prepare("SELECT * FROM knowledgebase ORDER BY category ASC");
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows === 0) echo "<tr><td>No activities found</td><td></td><td></td><td></td><td></td><td></td></tr> </tbody>
</table></br></br>
Why not add your first activity from our Knowledge base or create a new activity of your own";
else {
while($row = mysqli_fetch_array($result)) {
$activity_id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$frequency = $row['frequency'];
$mandatory = $row['mandatory'];
echo "<tr><td>".$title."</td><td>".$description."</td><td style=\"text-align: center;\">".$frequency."</td><td style=\"text-align: center;\">".$mandatory."</td><td></td></tr>";
}
}
$stmt->close();
?>
</tbody>
</table>
What I want to add in is another query inside the final <td></td>. What I want to do is query a second db table and say if this activity has already been added to the users table, echo YES, otherwise, echo NO.
The problems is, adding the query into the while loop keeps throwing errors.
Any suggestions gratefully received.
Instead of running another query for every row, you could join the users table to the knowledgebase table in your first query. If you use a left join, you'll still get all the rows from knowledgebase.
SELECT knowledgebase.*, userstable.activity_id
FROM knowledgebase
LEFT JOIN userstable ON knowledgebase.id = userstable.activity_id
ORDER BY category ASC
Then in your last <td>, you can print YES/NO depending on whether or not there was a matching row in the users table.
...<td><?php echo $row['activity_id'] ? 'YES' : 'NO' ?></td>...
(I made up names for your other table and column, but I think it shows the general idea.)

Returns all rows with Minimum Value in Php

I have a table that consist of records and i want to retrieve rows with minimum price. I try the code below, and i am only able to retrieve only one rows. i need to return all rows with minimum price for the same partno.
<tr>
<th>S/N</th>
<th>Part Number</th>
<th>Description </th>
<th nowrap="nowrap">Recommended Price</th>
<th nowrap="nowrap">Recommended Supplier</th>
</tr>
<?php
$get = mysqli_query($con,"SELECT MIN(price)As price,partno,supplier,description FROM tab_stockqty2 ");
$c=0;
while($rw = mysqli_fetch_array($get)){
$c++;?>
<tr>
<td nowrap="nowrap"><?php echo $c;?></td>
<td nowrap="nowrap"><?php echo $rw['partno'];?></td>
<td nowrap="nowrap"><?php echo $rw['description'];?></td>
<td><?php echo number_format($rw['price'],2);?></td>
<td><?php echo $rw['supplier'];?></td>
</tr>
<?php };?>
See Database Record:
It show return two rows, but it is only retuning only one row for the first set of partno-2070081
To retrieve all records with minimal price, per partno, the query should be something like this:
SELECT s.price,s.partno,s.supplier,s.description FROM tab_stockqty2 s
INNER JOIN (
SELECT MIN(price) as m, partno FROM tab_stockqty2 GROUP BY 2
) m on s.price = m.m and s.partno = m.partno
Demo: http://sqlfiddle.com/#!9/3e089/1
try this ==>
SELECT MIN(price)As price,partno,supplier,description FROM tab_stockqty2 group by partno
The SQL MIN returns only 1 row which is the most minimum.
What you can do is fetch all records and sort them on price based on ascending order.
SELECT price,partno,supplier,description FROM tab_stockqty2 ORDER BY price ASC;
Try this instead.
In this way you will get records based on price from Low to High.

How to keep a running total in php foreach loop

I am trying to create a table that displays member data for three different types of members - it should display how many members were added each day, and then there needs to be a column that keeps a cumulative total month to date for the three different types of members. Example:
Members Added in March:
Date Type A Type A Total Type B Type B Total
1 23 23 16 16
2 13 36 13 29
3 16 52 17 46
My query looks like this:
$results=$wpdb->get_results("SELECT DAY(Subscription_Start_Date) as dayOfMonth, COUNT(a.user_id) as typeA, COUNT(b.user_id) as typeB, COUNT(c.user_id) as typeC
FROM users u
LEFT OUTER JOIN typeAUser a ON u.type_id = a.User_Id
LEFT OUTER JOIN typeBUser b ON u.type_id = b.User_Id
LEFT OUTER JOIN typeCUser c ON u.type_id = c.user_Id AND c.phone_opt_out != '1'
WHERE MONTH(Subscription_Start_Date) = MONTH(curdate()) AND YEAR(Subscription_Start_Date) = YEAR(curdate())
GROUP BY DAY(Subscription_Start_Date)
ORDER BY DAY(Subscription_Start_Date)");
I am outputting the data into a table using a foreach loop like so:
$resultTable = "<table><thead>
<tr><td colspan='7'>Month to Date Members Added $currentMonth</td></tr>
<tr>
<th>Day of the Month</th>
<th>Type A</th>
<th>Type A Month to Date</th>
<th>Type B</th>
<th>Type B Month to Date</th>
<th>Type C</th>
<th>Type C Month to Date</th>
</tr></thead><tbody>";
foreach($results as $r){
$aMemPerDay = $r->typeA;
$bMemPerDay = $r->typeB;
$cMemPerDay = $r->typeC;
$resultTable .= "<tr>
<td>$r->dayOfMonth</td>
<td>$aMemPerDay</td>
<td>???</td>
<td>$bMemPerDay</td>
<td>???</td>
<td>$cMemPerDay</td>
<td>???</td>
</tr>";
}
$resultTable .="</tbody></table>";
echo $resultTable;
The query returns the info I need for the number of members added per day, but I can't figure out how to keep a running total that increase each day. I have tried many things, but nothing seems to work. I tried implementing a variation of this solution within the foreach on each of the member types but when I put the $totalA variable into the table, it just printed out Array:
$totalA = array();
$runningSumA = 0;
foreach ($aMemPerDay as $aMem) {
$runningSumA += $aMem;
$totalA[] = $runningSum;
}
I even tried using a MySQL variable in the query (since there are only 30 days per month, I figured it wouldn't be overly cumbersome), but I wasn't able to get a cumulative result there either. I know that there is probably some manageable answer out there, but I can't seem to come up with it. Any direction would be appreciated!
$TotalA = 0;
$TotalB = 0;
$TotalC = 0;
foreach($results as $r){
$aMemPerDay = $r->typeA;
$bMemPerDay = $r->typeB;
$cMemPerDay = $r->typeC;
$TotalA += $aMemPerDay;
$TotalB += $bMemPerDay;
$TotalC += $bMemPerDay;
$resultTable .= "<tr>
<td>$r->dayOfMonth</td>
<td>$aMemPerDay</td>
<td>???</td>
<td>$bMemPerDay</td>
<td>???</td>
<td>$cMemPerDay</td>
<td>???</td>
</tr>";
}

Using while() display even with NULLS

I have a table and I am displaying its contents using PHP and a while(); I have about three fields in the table that are NULL but can be change, but I want them to still display all the results in my table.
But, it only shows the records with data in EVERY field. Anyone how I can display it? I get a count of the table and it gives me 2, but only displays one.
<h3>Viewing All Updates</h3>
<h4>Below are all active updates for COTC</h4>
<table>
<thead>
<tr>
<th>Site Name</th>
<th>Page</th>
<th>Flag</th>
<th>Date Sent</th>
<th>View</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT sname,page_name,date_submitted,u_id,clients.c_id,flag,completed FROM updates INNER JOIN clients ON updates.c_id = clients.c_id INNER JOIN pages ON updates.page = pages.p_id ORDER BY date_submitted DESC";
$query = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($query)){
$completed = $row['completed'];
if($completed == 1){
print '<tr class="quiet">';
}else{
print '<tr>';
}
print '<td>'.$row['sname'].'</td>';
print '<td>'.$row['page_name'].'</td>';
print '<td>'.$row['flag'].'</td>';
print '<td>'.$row['date_submitted'].'</td>';
print '<td class="center"><img src="images/page_edit.png" alt="Edit entry!" /></td>';
print '</tr>';
}
?>
</tbody>
</table>
Your PHP is correctly printing every row returned. I believe your problem is in the query.
SELECT sname,page_name,date_submitted,u_id,clients.c_id,flag,completed
FROM updates INNER JOIN clients ON updates.c_id = clients.c_id
INNER JOIN pages ON updates.page = pages.p_id
ORDER BY date_submitted DESC
This query will only return a row in updates if it has a matching one in clients and a matching one in pages. If you want the clients or the pages joins to be optional (a updates row that has c_id or page of NULL will still return) change them from INNER JOINs to LEFT JOINs.

Categories