开发者

Creating a Variable From Two Others

开发者 https://www.devze.com 2023-01-01 11:16 出处:网络
In the HTML table below, I would like to add a third column that equals $row[\"countSubmissions\"] times 10 plus $row[\"countComments\"].How could I do this?

In the HTML table below, I would like to add a third column that equals $row["countSubmissions"] times 10 plus $row["countComments"]. How could I do this?

Thanks in advance,

John

$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>";


You can modify the SQL to include the extra column:

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
...

or do the calculation in 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>';

I don't think the calls to stripslashes are necessary so I removed them.


after

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

add the following

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

精彩评论

暂无评论...
验证码 换一张
取 消