从其他两个创建变量

时间:2010-05-31 15:41:52

标签: php

在下面的HTML表格中,我想添加第三列,等于$ row [“countSubmissions”]乘以10加$ row [“countComments”]。我怎么能这样做?

提前致谢,

约翰

$sqlStr = "SELECT 
    l.loginid, 
    l.username, 
    COALESCE(s.total, 0) AS countSubmissions, 
    COALESCE(c.total, 0) AS countComments
FROM login l    
LEFT JOIN (
    SELECT loginid, COUNT(1) AS total 
    FROM submission 
    GROUP BY loginid
) s ON l.loginid = s.loginid
LEFT JOIN (
    SELECT loginid, COUNT(1) AS total 
    FROM comment 
    GROUP BY loginid
) c ON l.loginid = c.loginid
GROUP BY l.loginid
ORDER BY countSubmissions DESC 
LIMIT 10";

  $result = mysql_query($sqlStr);

$arr = array(); 
echo "<table class=\"samplesrec1edit\">";
while ($row = mysql_fetch_array($result)) { 
    echo '<tr>';
    echo '<td class="sitename1edit1"><a href="http://www...com/.../members/index.php?profile='.$row["username"].'">'.stripslashes($row["username"]).'</a></td>';
    echo '<td class="sitename1edit2">'.stripslashes($row["countSubmissions"]).'</td>';
    echo '<td class="sitename1edit2">'.stripslashes($row["countComments"]).'</td>';
    echo '</tr>';
    }
echo "</table>";

2 个答案:

答案 0 :(得分:2)

您可以修改SQL以包含额外列:

SELECT 
    l.loginid, 
    l.username, 
    COALESCE(s.total, 0) AS countSubmissions, 
    COALESCE(c.total, 0) AS countComments,
    COALESCE(s.total, 0) * 10 + COALESCE(c.total, 0) AS totalScore
...

或在PHP中进行计算:

echo '<td class="sitename1edit2">'. $row['countSubmissions'] . '</td>';
echo '<td class="sitename1edit2">'. $row['countComments'] . '</td>';
echo '<td class="sitename1edit2">'. ($row['countSubmissions'] * 10 + row['countComments']) . '</td>';

我认为调用stripslashes是不必要的,所以我删除了它们。

答案 1 :(得分:0)

之后

echo '<td class="sitename1edit2">'.stripslashes($row["countComments"]).'</td>';

添加以下内容

echo '<td>' . ($row["countSubmissions"] * 10 + $row["countComments"]) . '</td>';