Page 1 of 1

Calculating a percentage

Posted: Sun Jan 04, 2004 1:23 pm
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;

Posted: Sun Jan 04, 2004 1:52 pm
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>";

Posted: Sun Jan 04, 2004 3:57 pm
by therat
Where would I get the $ie variable from. Would that need a seperate query?

Posted: Sun Jan 04, 2004 4:00 pm
by JAM
You could add something like sum(hits) to your existing query posted above.

Posted: Mon Jan 05, 2004 2:13 am
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?

Posted: Mon Jan 05, 2004 5:32 am
by m3mn0n
you need [php_man]round[/php_man]() for that formula

Posted: Mon Jan 05, 2004 1:48 pm
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.

Posted: Mon Jan 05, 2004 3:14 pm
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

Posted: Mon Jan 05, 2004 3:20 pm
by therat
Thanks, i'll give that a go.