没有循环的PHP回声。 SQL查询

时间:2015-11-03 07:37:09

标签: php html mysqli

以下是主题:Link,点击播放器名称即可查看。

我想让玩家的名字作为标题,这样玩家的名字就不会在名册表中的每一个结果行上反复重复。我知道如何遍历行的结果,但我只希望名称在顶部一次,并且不知道如何做到这一点。这就是我打印表格代码的方式

<?php

$pid = $_POST['PID'];
$query = "SELECT Rosters.PID, Rosters.Goals, Rosters.Assists, Rosters.PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname,
            (Rosters.Goals + Rosters.Assists) AS Points
             FROM Rosters
             INNER JOIN Players
             ON Rosters.PID = Players.pid
             WHERE Rosters.PID = $pid
             ORDER BY Points DESC, Goals DESC";
$result = $conn->query($query);

$query2 = "SELECT Rosters.PID, SUM( Rosters.Goals ) Goals, SUM( Rosters.Assists ) Assists, SUM( Rosters.PIM ) PIM, Rosters.Num, Rosters.TID, Players.pid, Players.firstname, Players.lastname, 
              SUM((Rosters.Goals + Rosters.Assists)) AS Points
              FROM Rosters
              INNER JOIN Players ON Rosters.PID = Players.pid
              WHERE Rosters.PID =$pid
              ORDER BY Points DESC , Goals DESC";
$result2 = $conn->query($query2);
echo

if (!empty($_POST["PID"])) {
    echo "<table class='stat-table-wide table sortable' align='center'>";
    echo "<tr>";
    echo "<th class='hover'>Season</th>";
    echo "<th class='hover'>Num</th>";
    echo "<th class='hover'>Player</th>";
    echo "<th class='hover'>G</th>";
    echo "<th class='hover'>A</th>";
    echo "<th class='hover'>Pts</th>";
    echo "<th class='hover'>PIM</th>";
    echo "</tr>";
    $i = 13;
    $j = 14;
    while ($row = $result->fetch_assoc()) {
        $i++;
        $j++;
        $goals = $row["Goals"];
        $assists = $row["Assists"];
        $points = $goals + $assists;
        echo "<tr><td>20" . $i . "-20" . $j . "</td>";
        echo "<td>" . $row["Num"] . "</td>";
        echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
        echo "<td>" . $row["Goals"] . "</td>";
        echo "<td>" . $row["Assists"] . "</td>";
        echo "<td>" . $row["Points"] . "</td>";
        echo "<td>" . $row["PIM"] . "</td></tr>";
    }
    echo "</table>";
    echo "<span class='style35'>Total</span><br />";
    echo "<table class='stat-table-wide table sortable' align='center'>";
    echo "<tr>";
    echo "<th class='hover'>Player</th>";
    echo "<th class='hover'>G</th>";
    echo "<th class='hover'>A</th>";
    echo "<th class='hover'>Pts</th>";
    echo "<th class='hover'>PIM</th>";
    echo "</tr>";
    while ($row = $result2->fetch_assoc()) {
        $goals = $row["Goals"];
        $assists = $row["Assists"];
        $points = $goals + $assists;
        echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
        echo "<td>" . $row["Goals"] . "</td>";
        echo "<td>" . $row["Assists"] . "</td>";
        echo "<td>" . $row["Points"] . "</td>";
        echo "<td>" . $row["PIM"] . "</td></tr>";
    }
    echo "</table>";
}
?>

我只想在生成任何表之前打印一次Players.firstname和Players.lastname。

3 个答案:

答案 0 :(得分:1)

你可以在循环之前获得结果的第一行,然后只需重置光标,并完全忽略循环中的玩家名称。

$first = $result->fetch_assoc();

// print out first name
echo($first['name'] . " " . $first['surname']);

// reset cursor
$result->data_seek(0);

// do your loops and stuff.
while ($row = $result->fetch_assoc()) {
    ...
}

答案 1 :(得分:1)

你可以这样做。如果你不想改变sql查询。

$ctr = 0;
while ($row = $result->fetch_assoc()) {
$ctr++;
if($ctr == 1) {
echo "</table>";
echo "<span class='style35'>Total</span><br />";
echo "<table class='stat-table-wide table sortable' align='center'>";
echo "<tr>";
echo "<th class='hover'>Player</th>";
echo "<th class='hover'>G</th>";
echo "<th class='hover'>A</th>";
echo "<th class='hover'>Pts</th>";
echo "<th class='hover'>PIM</th>";
echo "</tr>";
echo "<tr><td>" . $row["firstname"] . " " . $row["lastname"] . "</td> </tr>";
}
    $i++;
    $j++;
    $goals = $row["Goals"];
    $assists = $row["Assists"];
    $points = $goals + $assists;
    echo "<tr><td>20" . $i . "-20" . $j . "</td>";
    echo "<td>" . $row["Num"] . "</td>";
    echo "<td>" . $row["firstname"] . " " . $row["lastname"] . "</td>";
    echo "<td>" . $row["Goals"] . "</td>";
    echo "<td>" . $row["Assists"] . "</td>";
    echo "<td>" . $row["Points"] . "</td>";
    echo "<td>" . $row["PIM"] . "</td></tr>";
}

答案 2 :(得分:1)

试试这个:

sentence = buildExpressionParser table term where
  table = [ [prefix tildeP]
          , [Infix amperP AssocLeft]
          , [prefix bangP]
          ]
  term = (bangP <*> sentence) <|> identP
  prefix  p = Prefix . chainl1 p $ return (.)

我不知道你是否想要总计中玩家的名字,所以我把它留在那里,但如果你愿意,你可以删除它。我还向玩家姓名申请了与总数相同的风格。