在php表中添加数组元素

时间:2014-04-07 02:35:29

标签: php mysql sql

我想问我是否会通过以下代码将下面生成的所有驱动程序的点数加起来,因为这只显示每行中所有驱动程序的点...有一个表有点的点,位置和表驱动程序有姓,姓。

    <?php
session_start();

if ( !isset($_SESSION['loggedin']) ) {
    header("Location: login.php");
}
    $con=mysqli_connect("xxxxx","xxxxx","xxxxx","xxxx");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT Drivers.forename, Drivers.surname, Teams.name, Teams.engine, Points.points
    From Drivers, Teams, Points");

    echo "<table border='1'>
    <tr>
    <th>First name</th>
    <th>Last name</th>
    <th>Team Name</th>
    <th>Engine</th>
    <th>Points</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
        echo "<tr>";
        echo "<td>" . $row['forename'] . "</td>";
        echo "<td>" . $row['surname'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['engine'] . "</td>";
        echo "<td>" . $row['points'] . "</td>";

        echo "</tr>";
    }
    echo "</table>";

    mysqli_close($con);
    ?>

1 个答案:

答案 0 :(得分:0)

代码中的

,在$ while

之前
     $counter=0
      while($row = mysqli_fetch_array($result))
{
    $counter += $row['points'];
    echo "<tr>";
    echo "<td>" . $row['forename'] . "</td>";
    echo "<td>" . $row['surname'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['engine'] . "</td>";
    echo "<td>" . $row['points'] . "</td>";

    echo "</tr>";
}

 echo $counter; //echoes sum of points