?
Solved

adding the values of multiple records matching a criteria

Posted on 2005-03-15
5
Medium Priority
?
218 Views
Last Modified: 2013-12-12
<?
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
Comment
Question by:paulp75
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13543307
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
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 600 total points
ID: 13543313
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13543316
<?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
 
LVL 32

Accepted Solution

by:
ldbkutty earned 1400 total points
ID: 13543325
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
 
LVL 6

Author Comment

by:paulp75
ID: 13543471
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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question