如果我的标题造成混乱,我很抱歉,但我不知道如何在如此短的空间内更好地解释,所以我会在这里尝试: 我有一个存储日志的表,它具有以下结构:
log_id, username, action, ip, date
“行动”例如是changed password
或类似的东西
如果我连续4个日志用于同一个用户并且操作相同,我不想再显示四次。我只想这样显示:
PHPDeveloper: Changed password (4)
我尝试自己这样做,但我无法弄清楚,我想我只需要更好地构建我的数组并覆盖相同日志的旧值。
目前,我的代码显示:
PHPDeveloper: Changed password (1)
PHPDeveloper: Changed password (2)
PHPDeveloper: Changed password (3)
PHPDeveloper: Changed password (4)
我的代码:
<?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>
答案 0 :(得分:0)
在你的SQL上做一个小组:
SELECT username, action, COUNT(*) total FROM users WHERE id=:id GROUP BY username, action
通过上述查询,您将获得显示所需的所有信息