Calculating a percentage

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Calculating a percentage

Post by therat »

I have the following that displays info on browsers used to view a site. How do I change it to calculate the percentage for each type of browser? I need it to show in place of the hardcoded 50%.

Code: Select all

function browser() {
        $sql ="SELECT browser, hits FROM stats_browser
			   ORDER BY hits DESC LIMIT 10";
        $browse = mysql_query($sql) or die(mysql_error());
	echo "<table width='95%' align='center' class='sborder'> 
    	 <tr> 
            <td colspan='4' class='statsheader'><b>Browser's : </b> top 10 ( <a href='http://www.test.site.com/e107/stats.php?1'>View all</a> )</td> 
         </tr>\n";
	do 
	echo "<tr> 
            <td style='width:25%' class='statsheader'>".$row_browse&#1111;'browser']."</td> 
            <td nowrap style='width:55%' class='statsheader'>".mk_percbar($mise,200,"$msie&#1111;1] %")."</td> 
            <td style='width:10%; text-align:center' class='statsheader'>".$row_browse&#1111;'hits']."</td> 
            <td style='width:10%; text-align:center' class='statsheader'>50%</td> 
     	 </tr>\n";
	 while ($row_browse = mysql_fetch_assoc($browse));
echo "</table>\n";
&#125;
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

take the hits from the specific browser, divide it by total hits, and times it by 100

so something like

Code: Select all

$percent = ($ie / $hits) * 100;
echo "<td>" . $percent . "</td>";
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Where would I get the $ie variable from. Would that need a seperate query?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You could add something like sum(hits) to your existing query posted above.
mwong
Forum Commoner
Posts: 34
Joined: Sun Dec 28, 2003 2:58 am

Post by mwong »

How does your database work? It lists the browsers and keeps track of how many hits each get? Or does it track the browser of individual hits?...then what is the HITS column for?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

you need [php_man]round[/php_man]() for that formula
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

mwong wrote:How does your database work? It lists the browsers and keeps track of how many hits each get? Or does it track the browser of individual hits?...then what is the HITS column for?
There are 3 columns in the table called id, browser & hits. ID is set to autoincrement, browser stores the name of the browser and hits has 1 addedd each time that browser is used.
basdog22
Forum Contributor
Posts: 158
Joined: Sun Nov 30, 2003 3:03 pm
Location: Greece

Post by basdog22 »

Code: Select all

$query=mysql_query("select * from stats");
while($row=mysql_fetch_array($query))
&#123;
if ($row&#1111;'browser']=="ie")
&#123;$ie=$row&#1111;'hits'];&#125;

....

...

$percent=$ie / $totalhits * 100;
echo $percent;
&#125;
Hope i helped
therat
Forum Commoner
Posts: 62
Joined: Wed Oct 01, 2003 2:44 pm
Location: London

Post by therat »

Thanks, i'll give that a go.
Post Reply