I have a while loop that keeps track of two variables, incrementing them as the condition passes or fails. It is definitely incrementing them but it shows every iteration of the increment. I am only wanting to show the values once the while loop has finished. I know I need to close the loop and then display the value but I can't have that happen as I need the HTML to display multiple times for multiple users. I am wondering how to get the HTML to display multiple times but only have the two variables display for each user.
$addedItem = $tradedItem = 0;
$sql = "SELECT DISTINCT adminid, log_operation FROM item_log_table WHERE timestamp BETWEEN '$time' and '$time2' ORDER BY adminid ASC";
$result = $db->prepare($sql);
$result->execute();
while ($row = $result->fetch()) {
$username = $functions->getUserInfoSingularFromId('username', $row['adminid']);
$lastLogin = $functions->getUserInfoSingularFromId('previous_visit', $row['adminid']);
$lastLogin = $adminfunctions->displayDate($lastLogin);
if ($row['log_operation'] == "ADDED ITEM") {
$addedItem += 1;
} else {
$tradedItem += 1;
} ?>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="border-radius: 16px; margin-top: 25px;">
<div class="well profile col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 text-center">
<p style="text-align:center;font-size: smaller;"><strong>Name | Employee: </strong><span class="tags" id="user-status"><?php echo $username; ?> | <?php echo $row['adminid']; ?></span></p>
<p style="text-align:center;font-size: smaller;"><strong>Date Last Logged In: </strong><span class="tags" id="user-status"> <?php echo $lastLogin; ?></span></p>
<div class="col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
<h4><p style="text-align: center;"><strong id="user-globe-rank"><?php echo $addedItem; ?></strong></p></h4>
<p><small class="label label-success">Items Added</small></p>
</div>
<div class=" col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
<h4><p style="text-align: center;"><strong id="user-college-rank"><?php echo $tradedItem; ?></strong></p></h4>
<p> <small class="label label-warning">Items Traded</small></p>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
What it looks like in the loop
I only want it to show the last iteration of the $addedItem and $tradedItem for each user. Also, excuse the style attributes I'm just prototyping.
Since you are using DISTINCT you will only retrieve a single record for the adminid
that has log_operation
of ADDED ITEM
.
For example if your table has:
| adminid | log_operation |
| 1 | ADDED ITEM |
| 1 | ADDED ITEM |
| 1 | TRANSFER ITEM |
| 2 | ADDED ITEM |
Your query would only retrieve
| adminid | log_operation |
| 1 | ADDED ITEM |
| 1 | TRANSFER ITEM |
| 2 | ADDED ITEM |
Resulting in $addedItem
always equalling 1
for a specific adminid. Since you're not resetting $addedItem
per adminid
, your loop will continue incrementing it for each adminid
.
To resolve the issue and total all the log_operations
that are ADDED ITEM
for an adminid
use aggregate functions on the desired log_operation
value.
SELECT
adminid,
SUM(CASE log_operation WHEN 'ADDED ITEM' THEN 1 ELSE 0 END) AS addedItems,
SUM(CASE log_operation WHEN 'ADDED ITEM' THEN 0 ELSE 1 END) AS tradedItems
FROM item_log_table
WHERE `timestamp` BETWEEN '$time' AND '$time2'
GROUP BY adminid
ORDER BY adminid ASC
This will retrieve a single row for each adminid, with the total number of log operations that are ADDED ITEM
and not.
| adminid | addedItems | tradedItems |
| 1 | 2 | 1 |
| 2 | 1 | 0 |
Then just retrieve those columns in your html.
<div class="col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
<h4><p style="text-align: center;"><strong id="user-globe-rank"><?php echo $row['addedItems']; ?></strong></p></h4>
<p><small class="label label-success">Items Added</small></p>
</div>
<div class=" col-lg-6 left" style="text-align:center;overflow-wrap: break-word;">
<h4><p style="text-align: center;"><strong id="user-college-rank"><?php echo $row['tradedItems']; ?></strong></p></h4>
<p> <small class="label label-warning">Items Traded</small></p>
</div>