php项目统计脚后跟传球成功几次?

wen PHP项目 2

本文目录导读:

php项目统计脚后跟传球成功几次?

  1. 方案1:基于数组统计数据
  2. 方案2:从数据库查询
  3. 方案3:简单函数统计
  4. 数据库表结构建议

我来帮您创建一个统计脚后跟传球成功次数的PHP解决方案,这需要根据您的具体数据格式来实现,我提供几种常见的实现方式:

方案1:基于数组统计数据

<?php
class HeelPassStats {
    private $passes = [];
    /**
     * 添加传球记录
     * @param array $pass 传球数据
     */
    public function addPass(array $pass) {
        $this->passes[] = [
            'player_id' => $pass['player_id'] ?? null,
            'player_name' => $pass['player_name'] ?? '',
            'is_heel' => $pass['is_heel'] ?? false,
            'is_success' => $pass['is_success'] ?? false,
            'match_id' => $pass['match_id'] ?? null,
            'timestamp' => time()
        ];
    }
    /**
     * 统计所有脚后跟传球成功次数
     */
    public function countSuccessfulHeelPasses() {
        $count = 0;
        foreach ($this->passes as $pass) {
            if ($pass['is_heel'] && $pass['is_success']) {
                $count++;
            }
        }
        return $count;
    }
    /**
     * 统计每个球员的脚后跟传球成功次数
     */
    public function countByPlayer() {
        $playerStats = [];
        foreach ($this->passes as $pass) {
            if ($pass['is_heel'] && $pass['is_success']) {
                $playerId = $pass['player_id'];
                if (!isset($playerStats[$playerId])) {
                    $playerStats[$playerId] = [
                        'name' => $pass['player_name'],
                        'success_count' => 0
                    ];
                }
                $playerStats[$playerId]['success_count']++;
            }
        }
        return $playerStats;
    }
    /**
     * 按比赛统计
     */
    public function countByMatch() {
        $matchStats = [];
        foreach ($this->passes as $pass) {
            if ($pass['is_heel'] && $pass['is_success']) {
                $matchId = $pass['match_id'];
                if (!isset($matchStats[$matchId])) {
                    $matchStats[$matchId] = 0;
                }
                $matchStats[$matchId]++;
            }
        }
        return $matchStats;
    }
}
// 使用示例
$stats = new HeelPassStats();
// 添加测试数据
$stats->addPass([
    'player_id' => 1,
    'player_name' => '梅西',
    'is_heel' => true,
    'is_success' => true,
    'match_id' => 101
]);
$stats->addPass([
    'player_id' => 1,
    'player_name' => '梅西',
    'is_heel' => true,
    'is_success' => false, // 失败
    'match_id' => 101
]);
$stats->addPass([
    'player_id' => 2,
    'player_name' => 'C罗',
    'is_heel' => true,
    'is_success' => true,
    'match_id' => 102
]);
// 输出统计结果
echo "脚后跟传球成功总次数: " . $stats->countSuccessfulHeelPasses() . "\n";
print_r($stats->countByPlayer());
print_r($stats->countByMatch());
?>

方案2:从数据库查询

<?php
class DatabaseHeelPassStats {
    private $pdo;
    public function __construct(PDO $pdo) {
        $this->pdo = $pdo;
    }
    /**
     * 统计所有脚后跟传球成功次数
     */
    public function getTotalSuccessfulHeelPasses() {
        $sql = "SELECT COUNT(*) as total 
                FROM passes 
                WHERE pass_type = 'heel' 
                AND is_success = 1";
        $stmt = $this->pdo->query($sql);
        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        return $result['total'];
    }
    /**
     * 统计每个球员的脚后跟传球成功次数
     */
    public function getPlayerHeelPassStats() {
        $sql = "SELECT 
                    p.player_id,
                    p.player_name,
                    COUNT(*) as success_count
                FROM passes p
                WHERE p.pass_type = 'heel' 
                AND p.is_success = 1
                GROUP BY p.player_id, p.player_name
                ORDER BY success_count DESC";
        $stmt = $this->pdo->query($sql);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    /**
     * 按比赛统计脚后跟传球成功次数
     */
    public function getMatchHeelPassStats($matchId = null) {
        $sql = "SELECT 
                    match_id,
                    COUNT(*) as success_count
                FROM passes
                WHERE pass_type = 'heel' 
                AND is_success = 1";
        if ($matchId !== null) {
            $sql .= " AND match_id = :match_id";
            $stmt = $this->pdo->prepare($sql);
            $stmt->execute(['match_id' => $matchId]);
        } else {
            $sql .= " GROUP BY match_id";
            $stmt = $this->pdo->query($sql);
        }
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    /**
     * 统计指定日期范围内的脚后跟传球成功次数
     */
    public function getDateRangeHeelPassStats($startDate, $endDate) {
        $sql = "SELECT 
                    DATE(p.created_at) as pass_date,
                    COUNT(*) as success_count
                FROM passes p
                WHERE p.pass_type = 'heel' 
                AND p.is_success = 1
                AND DATE(p.created_at) BETWEEN :start_date AND :end_date
                GROUP BY DATE(p.created_at)
                ORDER BY pass_date";
        $stmt = $this->pdo->prepare($sql);
        $stmt->execute([
            'start_date' => $startDate,
            'end_date' => $endDate
        ]);
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
// 数据库连接示例
$dsn = 'mysql:host=localhost;dbname=football_stats;charset=utf8mb4';
$pdo = new PDO($dsn, 'username', 'password');
$stats = new DatabaseHeelPassStats($pdo);
// 使用示例
echo "总成功次数: " . $stats->getTotalSuccessfulHeelPasses() . "\n";
print_r($stats->getPlayerHeelPassStats());
print_r($stats->getMatchHeelPassStats());
?>

方案3:简单函数统计

<?php
/**
 * 简单统计脚后跟传球成功的次数
 * @param array $passes 传球数据数组
 * @return int
 */
function countSuccessfulHeelPasses(array $passes) {
    $count = 0;
    foreach ($passes as $pass) {
        if (($pass['type'] ?? '') === 'heel' && ($pass['success'] ?? false)) {
            $count++;
        }
    }
    return $count;
}
/**
 * 从CSV文件统计
 */
function countFromCSV($filePath) {
    if (!file_exists($filePath)) {
        return 0;
    }
    $count = 0;
    $handle = fopen($filePath, 'r');
    if ($handle !== false) {
        // 假设CSV格式: player_id,player_name,pass_type,is_success
        fgetcsv($handle); // 跳过表头
        while (($data = fgetcsv($handle)) !== false) {
            if ($data[2] === 'heel' && $data[3] == 1) {
                $count++;
            }
        }
        fclose($handle);
    }
    return $count;
}
// 使用示例
$passes = [
    ['type' => 'heel', 'success' => true],
    ['type' => 'normal', 'success' => true],
    ['type' => 'heel', 'success' => false],
    ['type' => 'heel', 'success' => true],
];
echo "脚后跟传球成功次数: " . countSuccessfulHeelPasses($passes);
?>

数据库表结构建议

CREATE TABLE passes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    match_id INT NOT NULL,
    player_id INT NOT NULL,
    pass_type ENUM('normal', 'heel', 'other') DEFAULT 'normal',
    is_success BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX idx_match_player (match_id, player_id),
    INDEX idx_pass_type (pass_type)
);

您可以根据实际需求选择合适的方案,如果您能提供更详细的数据结构或需求,我可以进一步优化代码。

抱歉,评论功能暂时关闭!