本文目录导读:

我来帮你设计一个PHP项目统计红黄牌数量的解决方案,根据不同的数据存储方式,我提供两种常见方案:
MySQL数据库存储
数据库表结构设计
-- 球队表
CREATE TABLE teams (
id INT PRIMARY KEY AUTO_INCREMENT,
team_name VARCHAR(50) NOT NULL,
league VARCHAR(50)
);
-- 球员表
CREATE TABLE players (
id INT PRIMARY KEY AUTO_INCREMENT,
player_name VARCHAR(50) NOT NULL,
team_id INT,
FOREIGN KEY (team_id) REFERENCES teams(id)
);
-- 红黄牌记录表
CREATE TABLE cards (
id INT PRIMARY KEY AUTO_INCREMENT,
match_id INT NOT NULL,
player_id INT,
team_id INT,
card_type ENUM('yellow', 'red') NOT NULL,
minute INT,
card_date DATE,
FOREIGN KEY (player_id) REFERENCES players(id),
FOREIGN KEY (team_id) REFERENCES teams(id)
);
PHP统计代码
<?php
// 数据库连接配置
$host = 'localhost';
$username = 'root';
$password = '';
$dbname = 'football_db';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
die("数据库连接失败: " . $e->getMessage());
}
/**
* 统计所有球队的红黄牌数量
*/
function getTeamCards($pdo) {
$sql = "SELECT
t.id,
t.team_name,
SUM(CASE WHEN c.card_type = 'yellow' THEN 1 ELSE 0 END) as yellow_count,
SUM(CASE WHEN c.card_type = 'red' THEN 1 ELSE 0 END) as red_count,
COUNT(c.id) as total_cards
FROM teams t
LEFT JOIN cards c ON t.id = c.team_id
GROUP BY t.id, t.team_name
ORDER BY total_cards DESC";
return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
/**
* 比较哪支球队红黄牌更多
*/
function compareTeams($pdo, $teamId1, $teamId2 = null) {
$teams = getTeamCards($pdo);
if ($teamId2 === null) {
// 返回排名第一的球队
return $teams[0] ?? null;
}
// 找出指定两支球队并进行比较
$team1 = null;
$team2 = null;
foreach ($teams as $team) {
if ($team['id'] == $teamId1) $team1 = $team;
if ($teamId2 !== null && $team['id'] == $teamId2) $team2 = $team;
}
if ($team1 && $team2) {
// 总牌数比较
if ($team1['total_cards'] > $team2['total_cards']) {
return ['winner' => $team1, 'loser' => $team2, 'message' => $team1['team_name'] . ' 红黄牌更多'];
} elseif ($team1['total_cards'] < $team2['total_cards']) {
return ['winner' => $team2, 'loser' => $team1, 'message' => $team2['team_name'] . ' 红黄牌更多'];
} else {
return ['message' => '两队红黄牌数量相同'];
}
}
return null;
}
// 使用示例
$allTeams = getTeamCards($pdo);
echo "<h3>所有球队红黄牌统计</h3>";
echo "<table border='1'>";
echo "<tr><th>球队</th><th>黄牌</th><th>红牌</th><th>总牌数</th></tr>";
foreach ($allTeams as $team) {
echo "<tr>";
echo "<td>{$team['team_name']}</td>";
echo "<td>{$team['yellow_count']}</td>";
echo "<td>{$team['red_count']}</td>";
echo "<td>{$team['total_cards']}</td>";
echo "</tr>";
}
echo "</table>";
// 找出红黄牌最多的球队
$maxTeam = compareTeams($pdo, 1); // 获取所有球队中牌最多的
echo "<p>红黄牌最多的球队: " . ($maxTeam['team_name'] ?? '无数据') .
" (总牌数: " . ($maxTeam['total_cards'] ?? 0) . ")</p>";
?>
JSON文件存储
数据结构文件 (cards.json)
{
"teams": [
{"id": 1, "name": "曼联"},
{"id": 2, "name": "利物浦"},
{"id": 3, "name": "曼城"}
],
"cards": [
{"team_id": 1, "type": "yellow", "match": "比赛1"},
{"team_id": 1, "type": "red", "match": "比赛2"},
{"team_id": 2, "type": "yellow", "match": "比赛1"},
{"team_id": 2, "type": "yellow", "match": "比赛3"},
{"team_id": 3, "type": "red", "match": "比赛4"}
]
}
PHP统计代码
<?php
class CardStatistics {
private $data;
public function __construct($filePath) {
if (file_exists($filePath)) {
$json = file_get_contents($filePath);
$this->data = json_decode($json, true);
} else {
$this->data = ['teams' => [], 'cards' => []];
}
}
/**
* 统计所有球队的红黄牌
*/
public function getTeamCardsStats() {
$stats = [];
// 初始化所有球队
foreach ($this->data['teams'] as $team) {
$stats[$team['id']] = [
'name' => $team['name'],
'yellow' => 0,
'red' => 0,
'total' => 0
];
}
// 统计卡片
foreach ($this->data['cards'] as $card) {
$teamId = $card['team_id'];
if (isset($stats[$teamId])) {
if ($card['type'] == 'yellow') {
$stats[$teamId]['yellow']++;
} elseif ($card['type'] == 'red') {
$stats[$teamId]['red']++;
}
$stats[$teamId]['total']++;
}
}
// 按总牌数排序
usort($stats, function($a, $b) {
return $b['total'] - $a['total'];
});
return $stats;
}
/**
* 找出红黄牌最多的球队
*/
public function getMostCardsTeam() {
$stats = $this->getTeamCardsStats();
return !empty($stats) ? $stats[0] : null;
}
/**
* 比较两支球队
*/
public function compareTwoTeams($teamId1, $teamId2) {
$stats = $this->getTeamCardsStats();
$team1 = null;
$team2 = null;
foreach ($stats as $team) {
if ($team['id'] == $teamId1) $team1 = $team;
if ($team['id'] == $teamId2) $team2 = $team;
}
if ($team1 && $team2) {
if ($team1['total'] > $team2['total']) {
return "{$team1['name']} 红黄牌更多 ({$team1['total']} vs {$team2['total']})";
} elseif ($team1['total'] < $team2['total']) {
return "{$team2['name']} 红黄牌更多 ({$team2['total']} vs {$team1['total']})";
} else {
return "两队红黄牌数量相同";
}
}
return "球队信息不存在";
}
}
// 使用示例
$stats = new CardStatistics('cards.json');
echo "<h3>球队红黄牌排行榜</h3>";
echo "<table border='1'>";
echo "<tr><th>排名</th><th>球队</th><th>黄牌</th><th>红牌</th><th>总牌数</th></tr>";
$teamStats = $stats->getTeamCardsStats();
foreach ($teamStats as $index => $team) {
echo "<tr>";
echo "<td>" . ($index + 1) . "</td>";
echo "<td>{$team['name']}</td>";
echo "<td>{$team['yellow']}</td>";
echo "<td>{$team['red']}</td>";
echo "<td>{$team['total']}</td>";
echo "</tr>";
}
echo "</table>";
// 找出牌最多的球队
$mostCards = $stats->getMostCardsTeam();
if ($mostCards) {
echo "<p>红黄牌最多的球队: <strong>{$mostCards['name']}</strong> " .
"(黄牌: {$mostCards['yellow']}, 红牌: {$mostCards['red']}, 总计: {$mostCards['total']})</p>";
}
// 比较两支球队
echo $stats->compareTwoTeams(1, 2);
?>
纯数组(简单演示)
<?php
// 简单数组存储球队牌数
$teams = [
'曼联' => ['yellow' => 15, 'red' => 3],
'利物浦' => ['yellow' => 12, 'red' => 5],
'曼城' => ['yellow' => 10, 'red' => 2],
'切尔西' => ['yellow' => 18, 'red' => 4]
];
// 计算每支球队总牌数
$totalCards = [];
foreach ($teams as $team => $cards) {
$totalCards[$team] = $cards['yellow'] + $cards['red'];
}
// 找出最多牌的球队
arsort($totalCards);
$topTeam = key($totalCards);
$topCount = reset($totalCards);
echo "红黄牌最多的球队是: <strong>$topTeam</strong>,总牌数: $topCount 张<br>";
echo "详细统计:<br>";
foreach ($totalCards as $team => $total) {
echo "$team: 黄牌 {$teams[$team]['yellow']} 张, 红牌 {$teams[$team]['red']} 张, 总计 $total 张<br>";
}
?>
功能扩展建议
- 增加筛选功能:按联赛、赛季、比赛类型筛选
- 图表展示:使用Chart.js等库展示柱状图
- 实时更新:支持动态添加比赛数据
- 多维度统计:按球员、比赛场次等维度统计
选择适合你项目的数据存储方式,根据需要调整代码即可。