我试图让系统显示来自用户数据库的信息,然后它会在表格中显示,但是,我遇到的问题是页面加载,我将需要其中的三个,我认为我走了最长的路线。页面将需要显示收件人和类型,现在在我的数据库中我有5种类型的信息,它现在每个都计算它们然后显示为好坏和标记。我可以使用更少的MySQL
<?php
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT *
FROM `user_flags`
ORDER BY user_flags.timestamp DESC
LIMIT 15");
?>
<tbody>
<?php
while( $row = mysqli_fetch_assoc( $result ) ) {
$GoodRepSql = mysqli_query($conn, "SELECT COUNT(*)
FROM user_flags
WHERE user_flags.recipient = {$row['recipient']}
AND type = 'Good Rep' ");
if ($GoodRepSql->num_rows > 0) {
while($goodrow = $GoodRepSql->fetch_assoc()) {
$GoodRep = $goodrow["COUNT(*)"] ;
}
} else {
$GoodRep = $goodrow["COUNT(*)"] ;
}
$BadRepSql = mysqli_query($conn, "SELECT COUNT(*)
FROM user_flags
WHERE user_flags.recipient = {$row['recipient']}
AND type = 'Bad Rep' ");
if ($BadRepSql->num_rows > 0) {
while($badrow = $BadRepSql->fetch_assoc()) {
$BadRep = $badrow["COUNT(*)"] ;
}
} else {
$BadRep = $badrow["COUNT(*)"] ;
}
$FlagSql = mysqli_query($conn, "SELECT COUNT(*)
FROM user_flags
WHERE user_flags.recipient = {$row['recipient']}
AND type IN ('Smurfing', 'Griefing', 'Suspicious')");
if ($FlagSql->num_rows > 0) {
while($flagrow = $FlagSql->fetch_assoc()) {
$Flags = $flagrow["COUNT(*)"] ;
}
} else {
$Flags = $flagrow["COUNT(*)"] ;
}
echo
"<tr onclick=\"document.location = '{$url}/{$row['recipient']}';\">
<td>{$Recipient->players->player->personaname}</td>
<td>{$BadRep}</td>
<td>{$GoodRep}</td>
<td>{$Flags}</td>
</tr>\n";
}
?>
答案 0 :(得分:0)
如果您需要每种类型的计数,您可以使用:GROUP BY
SELECT COUNT(*),type FROM user_flags WHERE user_flags.recipient = {$row['recipient']} GROUP BY type
由于您希望将某些类型添加到一起,因此您需要使用php添加它们。
$RepSql = mysqli_query($conn, "SELECT COUNT(*) as repcount,type FROM user_flags WHERE user_flags.recipient = {$row['recipient']} GROUP BY type ");
$GoodRep=$BadRep=$Flags=0;
if ($RepSql->num_rows > 0) {
while($rep_row = $RepSql->fetch_assoc()) {
if( $rep_row["type"]=='Smurfing'||$rep_row["type"]=='Griefing'||$rep_row["type"]=='Suspicious'){
$Flags+=$rep_row["repcount"];
}else if($rep_row["type"]=="Good Rep"){
$GoodRep=$rep_row["repcount"];
}else if($rep_row["type"]=="Bad Rep"){
$BadRep=$rep_row["repcount"];
}
}
}