• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

adding the values of multiple records matching a criteria

<?
require_once 'dbconnect.php';
 $result = mysql_query("select * from stats order by count desc");
   while($r=mysql_fetch_array($result))
   {
 $count=$r["count"];
      $refdomain=$r["refdomain"];
      echo "
<table><tr><td><a class='mnu1'>$refdomain</td><td class='infobox2'><a class='mnu1'>$count</td></tr></table>";
}
?>


ok what this will bring up is stats like this.



domain1      54
domain1      38
domain2      25
domain3      22
                  21
domain4      20
domain4      19

the blank ones are for no referrers.

what i'd like to do is this
i'd like to add all of the records of domain1,2,etc
and order by the total per domain
and preferably have the blank records outputting as "bookmark"

so the output would look like this

domain1.com  92
domain4.com  39
domain2.com  25
domain3.com  22
bookmark       21


how would i go about that?

thanks
0
paulp75
Asked:
paulp75
  • 2
  • 2
2 Solutions
 
BatalfCommented:
Maybe you could just change the query:


$result = mysql_query("select refdomain,sum(count) as count from stats group by refdomain order by count desc");

0
 
BatalfCommented:
i.e.:


<?
require_once 'dbconnect.php';
$result = mysql_query("select refdomain,sum(count) as count from stats group by refdomain order by count desc");
   while($r=mysql_fetch_array($result))
   {
 $count=$r["count"];
      $refdomain=$r["refdomain"];
      echo "
<table><tr><td><a class='mnu1'>$refdomain</td><td class='infobox2'><a class='mnu1'>$count</td></tr></table>";
}
?>
0
 
ldbkuttyCommented:
<?php
 require_once 'dbconnect.php';
 $result = mysql_query("select sum(count) as sumcount, refdomain from stats group by refdomain order by sumcount desc");
 while($r=mysql_fetch_array($result))
 {
    $count = $r["sumcount"];
    $refdomain = (empty($r["refdomain"])) ? "bookmark" : $r["refdomain"];
    echo "
<table><tr><td><a class='mnu1'>$refdomain</td><td class='infobox2'><a class='mnu1'>$count</td></tr></table>";
 }
?>
0
 
ldbkuttyCommented:
Or this :

<?php
 require_once 'dbconnect.php';
 $result = mysql_query("select sum(count) as sumcount, if(refdomain='', 'bookmark', refdomain) as refdomain from stats group by refdomain order by sumcount desc");
 while($r=mysql_fetch_array($result))
 {
    $count = $r["sumcount"];
    $refdomain = $r["refdomain"];
    echo "
<table><tr><td><a class='mnu1'>$refdomain</td><td class='infobox2'><a class='mnu1'>$count</td></tr></table>";
 }
?>
0
 
paulp75Author Commented:
thanks for the answers there.
the last one worked awesome.
the other two were good, but didnt include the bookmark thingy.
thanks for all your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now