这个php项目显示横传转移球次数?

wen PHP项目 3

PHP 项目实现"横传转移球次数"统计

我理解你想在 PHP 项目中统计足球比赛里的横传转移球次数(横向传球/转移进攻方向),不过你没有提供具体的代码或数据结构,所以我先给出一个通用的实现方案,你可以根据实际情况调整。

这个php项目显示横传转移球次数?

数据结构设计

传球记录表 passes

CREATE TABLE passes (
    id INT AUTO_INCREMENT PRIMARY KEY,
    match_id INT NOT NULL,
    player_id INT NOT NULL,
    from_x DECIMAL(5,2),  -- 起始位置 X 坐标(0-100,球场宽度)
    from_y DECIMAL(5,2),  -- 起始位置 Y 坐标(0-100,球场长度)
    to_x DECIMAL(5,2),    -- 目标位置 X
    to_y DECIMAL(5,2),    -- 目标位置 Y
    pass_time INT,
    success TINYINT DEFAULT 1
);

横传判定逻辑

横传:球在球场左右方向移动,纵向(进攻方向)位移很小。

<?php
class PassAnalyzer
{
    // 纵向位移阈值(小于此值视为"横传")
    const HORIZONTAL_THRESHOLD = 15.0; // 单位:百分比
    /**
     * 判断是否为横传
     */
    public function isHorizontalPass($fromY, $toY): bool
    {
        $deltaY = abs($toY - $fromY);
        return $deltaY <= self::HORIZONTAL_THRESHOLD;
    }
    /**
     * 判断是否为"转移球"(跨区域横向长传)
     * 一般指从一侧边路转移到另一侧边路
     */
    public function isSwitchPass($fromX, $toX, $fromY, $toY): bool
    {
        $deltaX = abs($toX - $fromX);   // 横向移动距离
        $deltaY = abs($toY - $fromY);   // 纵向移动距离
        // 横向移动超过球场 30% 宽度,且纵向移动不大
        return $deltaX >= 30.0 && $deltaY <= 20.0;
    }
}

完整查询逻辑

<?php
require_once 'db.php';
$matchId = (int)($_GET['match_id'] ?? 0);
// 查询该场比赛所有成功传球
$stmt = $pdo->prepare("
    SELECT player_id, from_x, from_y, to_x, to_y
    FROM passes
    WHERE match_id = ? AND success = 1
");
$stmt->execute([$matchId]);
$passes = $stmt->fetchAll(PDO::FETCH_ASSOC);
$analyzer = new PassAnalyzer();
$horizontalCount = 0;   // 横传次数
$switchCount = 0;       // 横传转移次数(长距离横传)
$perPlayer = [];        // 每名球员统计
foreach ($passes as $p) {
    if ($analyzer->isHorizontalPass($p['from_y'], $p['to_y'])) {
        $horizontalCount++;
        $perPlayer[$p['player_id']]['horizontal'] = 
            ($perPlayer[$p['player_id']]['horizontal'] ?? 0) + 1;
        if ($analyzer->isSwitchPass($p['from_x'], $p['to_x'], $p['from_y'], $p['to_y'])) {
            $switchCount++;
            $perPlayer[$p['player_id']]['switch'] = 
                ($perPlayer[$p['player_id']]['switch'] ?? 0) + 1;
        }
    }
}
// 输出结果
echo "全场横传次数:{$horizontalCount}\n";
echo "横传转移球次数:{$switchCount}\n";

视图中显示

<table>
    <thead>
        <tr>
            <th>球员</th>
            <th>横传次数</th>
            <th>转移球次数</th>
        </tr>
    </thead>
    <tbody>
    <?php foreach ($perPlayer as $playerId => $stats): ?>
        <tr>
            <td><?= htmlspecialchars($playerId) ?></td>
            <td><?= $stats['horizontal'] ?? 0 ?></td>
            <td><?= $stats['switch'] ?? 0 ?></td>
        </tr>
    <?php endforeach; ?>
    </tbody>
</table>

需要你补充的信息

为了让方案更贴合你的项目,请告诉我:

  1. 数据库表结构是怎样的?字段名是什么?
  2. 球场的坐标系是怎么定义的?((0,0) 在哪,进攻方向是 X 还是 Y?)
  3. "横传转移"的判定标准是什么?(比如位移多少算横传?是否必须跨过中线?)
  4. 你现在已有的代码是什么?报什么错或想改哪里?

把代码贴出来,我可以直接帮你改。

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