I am sorry if my title is causing confusion, but I don't know how to explain better in such short amount of space, so I'll try here: I have a table that stores logs, it has this structure:
log_id, username, action, ip, date
The "action" is for example changed password
or something similar.
If I have 4 logs in a row that is for the same user and the same action, I don't want to display it four times. I only want to display like this:PHPDeveloper: Changed password (4)
I tried doing this myself but I couldn't really figure it out, I think I just need to structure my array better and overwrite the old values for the same log. Currently, my code displays:PHPDeveloper: Changed password (1)
PHPDeveloper: Changed password (2)
PHPDeveloper: Changed password (3)
PHPDeveloper: Changed password (4)
My code:
<?php
include("../inc/classes.php");
if(!DB::isLoggedIn()) {
header("Location: ../");
exit;
} else if(DB::query("SELECT * FROM users WHERE id=:id", array(":id" => DB::isLoggedIn()))[0]['username'] != "Chrille332") {
header("Location: ../");
exit;
}
$view_logs = array();
$logs = DB::query("SELECT * FROM logs");
for($i = 0; $i < count($logs); $i++) {
$encountered = 0;
// Check if the log has been encountered before, and check until you find something else
$view_logs[$i] = "<li>".$logs[$i]['account_name']." ".$logs[$i]['action']."</li>";
for($j = 1; $j < $i+1; $j++) {
$encountered++;
if(($view_logs[$i]['username'] && $view_logs[$i]['action'])==($view_logs[$j]['username'] && $view_logs[$j]['action'])) {
$view_logs[$i] = "<li>".$logs[$i]['account_name']." ".$logs[$i]['action']." (".$encountered.")</li>";
} else {
$view_logs[$i] = "<li>".$logs[$i]['account_name']." ".$logs[$i]['action']."</li>";
}
}
}
// $view_logs[$i] = "<li>".$logs[$i]['account_name']." ".$logs[$i]['action']." (".$encountered.")</li>";
// $view_logs[$i] = "<li>".$logs[$i]['account_name']." ".$logs[$i]['action']."</li>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing website</title>
</head>
<body>
<?php
var_dump($view_logs);
?>
</body>
</html>
Just do a group by on your SQL:
SELECT username, action, COUNT(*) total FROM users WHERE id=:id GROUP BY username, action
With the above query you will get all info you need for your display