将两行组合在一起

时间:2015-11-25 09:10:38

标签: php

也许有人可以帮助我。任何帮助表示赞赏!我正在尝试结合“???”带有“未知文件”行的行。所以总数将是4245.只需一行。

我正在使用while循环。这是我的代码

enter image description here

<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";

$query = "SELECT company, username, COUNT(company), username FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company ORDER BY company ASC";

$result = mysql_query($query) or die(mysql_error());

echo "<div style='margin-top:100px;'><center><h2>";
echo date(' \ F jS Y  - l');
echo "<br />";
echo "</h2><center></div>";


echo '
<center> <table class="pure-table pure-table-horizontal">
    <thead>
        <tr>
            <th>Company</th>
            <th>Total</th>
            <th>Users</th>
        </tr>
    </thead>
<tbody>
';


// Print out result
while($row = mysql_fetch_assoc($result)){

    echo "<tr>";
    echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
    echo "<td>" . $row['COUNT(company)'] . "</td>";
    echo "<td> ... </td>";
    echo "</tr>";
}


echo '
    </tbody>
</table> </center>
';

?>

1 个答案:

答案 0 :(得分:5)

您必须在???

之外计算unknown filewhile loop的总和
$total = 0;
while($row = mysql_fetch_assoc($result))
{
    if($row['company'] == NULL || $row['company'] == "unknown file")
        $total += $row['COUNT(company)'];
}   

然后你可以在主输出循环中使用该总数

while($row = mysql_fetch_assoc($result))
{
    echo "<tr>";
    echo "<td><strong>" .($row['company'] == NULL ? "???" : $row['company']). "</strong></td>";
    if($row['company'] == "unknown file")
        echo "<td>" . $total . "</td>";
    else
        echo "<td>" . $row['COUNT(company)'] . "</td>";
    echo "<td> ... </td>";
    echo "</tr>";
}