mysql循环中的php循环信息

deyfvvtc  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(451)

我正在创建一个页面,显示球员的详细信息,并通过罚款拉信息。我还有一个连接表,它从另一个表中提取单个记录,以便连接不是问题。
但现在完全停留在如何循环通过已经循环的数据上。
在我的players.php类中,我有一个函数,可以遍历所有需要的信息,并可以在前端很好地显示出来。

function getById($id)
{
    $conn = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    $sql = "SELECT *, nationalities.nationality FROM players 
            LEFT JOIN nationalities ON players.nationality = nationalities.id 
            WHERE players.id = :id";

    $st = $conn->prepare($sql);
    $st->bindValue(":id", $id, PDO::PARAM_INT);
    $st->execute();

    $row = $st->fetch();
    $conn = null;

    if ($row) return new Players($row);
}

function getList($numRows = 1000000, $order = "lastname DESC")
{
    $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
    $sql = "SELECT SQL_CALC_FOUND_ROWS *, lastname AS lastname FROM players
            ORDER BY " . ($order) . " LIMIT :numRows";

    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->execute();

    $list = array();

    while ($row = $st->fetch())
    {
        $player = new Players($row);
        $list[] = $player;
    }

    //Now get the total number of players that matched the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";

    $totalRows = $conn->query($sql)->fetch();
    $conn = null;

    return (array("results" => $list, "totalRows" => $totalRows[0]));
}

我的挑战是一个球员可以有多个位置,而不是只有一个?我是如何做到这一点的,我在用sql循环这个过程中也尝试过,但它似乎在任何我处理它的方式中都不起作用。我的table是:


**players**

id, firstname, lastname ...

**positions**

id, position

**player_positions**

player_id, position_id

使用以下数据集进行如下查询:

SELECT positions.position, players.firstname, players.lastname
FROM positions 
LEFT JOIN player_positions ON positions.id = player_positions.position_id
LEFT JOIN players ON player_positions.players_id = players.id
WHERE players_id = :id

.

position, firstname, lastname
RWB, Lewis, Robinson
CB, Lewis, Robinson

我的第一个循环是遍历播放器并很好地检索数据,但现在我需要它在这个数据集上的数据循环中循环,并将其存储为位置,然后能够在前端显示它:(
任何关于做这样一件事的帮助或指点都是很好的

fzwojiic

fzwojiic1#

我会按玩家分组,然后使用双循环来显示玩家的位置。

function getList($numRows = 1000000, $order = "lastname DESC")
{
    $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
    $sql = "SELECT SQL_CALC_FOUND_ROWS *, lastname AS lastname, id AS id FROM players
            ORDER BY " . ($order) . " LIMIT :numRows";

    $st = $conn->prepare( $sql );
    $st->bindValue( ":numRows", $numRows, PDO::PARAM_INT );
    $st->execute();

    $list = array();

    while ($row = $st->fetch())
    {
        $id = $row[2];
        $player = new Players($row);
        if(!isset($list[$id])) list[$id] = array(); 
        $list[$id][] = $player;
    }

    //Now get the total number of players that matched the criteria
    $sql = "SELECT FOUND_ROWS() AS totalRows";

    $totalRows = $conn->query($sql)->fetch();
    $conn = null;

    return (array("results" => $list, "totalRows" => $totalRows[0]));
}

//Used like this (just an example for the loops):

$list = getList();

foreach($list as $id => $positions) {
    echo $positions[0]->playerName."<br>";
    foreach($positions as $player) {
        //Output
        echo print_r($player, true)."<br>";
    }
}

相关问题