Zergman Posted October 27, 2008 Share Posted October 27, 2008 So I have a bunch of queries on a page and what I want to do is calculate the percent of each one from the total. Say each record has a column with either Value1 or Value2. All of them have either one. I just want to output the percent of each. Value1 = 68% Value2 = 32% Is this done easier via php or mysql? Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 $percent = round ( ( ($lower_value/$higher_value) *100) , 2); Quote Link to comment Share on other sites More sharing options...
Zergman Posted October 27, 2008 Author Share Posted October 27, 2008 Thanks for the quick reply! I sorta see what your doing, can't picture how to use it in my head. Here are 2 queries that I want to calculate the percent. SELECT * FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Resolved' SELECT * FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Routed' Any examples would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 Any idea which would be the lower value? Where is the total coming from? Quote Link to comment Share on other sites More sharing options...
Zergman Posted October 27, 2008 Author Share Posted October 27, 2008 The amount of each varies a lot from month to month, but neither is always the lowest/highest. Im just grabbing the total amount of records from each of the queries above. $totalRows_rsdatadisplayresolved = mysql_num_rows($rsdatadisplayresolved); and $totalRows_rsdatadisplayrouted = mysql_num_rows($rsdatadisplayrouted); Quote Link to comment Share on other sites More sharing options...
Zergman Posted October 27, 2008 Author Share Posted October 27, 2008 So if I get the total amount of records from each query and set them as a variable, I can do something like this? $resolved = $_GET['$totalRows_rsdatadisplayrouted']; $routed = $_GET['$totalRows_rsdatadisplayresolved']; then $percent = round ( ( ($resolved/$routed) *100) , 2); Quote Link to comment Share on other sites More sharing options...
IndyTechNerd Posted October 27, 2008 Share Posted October 27, 2008 So if I get the total amount of records from each query and set them as a variable, I can do something like this? $resolved = $_GET['$totalRows_rsdatadisplayrouted']; $routed = $_GET['$totalRows_rsdatadisplayresolved']; then $percent = round ( ( ($resolved/$routed) *100) , 2); That's if you want to know how many were resolved out of how many were routed. if you want a total, $total = $resolved+$routed; $res_percent = round ( ( ($resolved/$total) *100) , 2); $rout_percent = round ( ( ($routed/$total) *100) , 2); Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 Got ya $res = "SELECT COUNT ( * ) AS resnum FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Resolved'"; $query = mysql_query($res); $totRes = mysql_fetch_object($query); $rou = "SELECT COUNT ( * ) AS rounum FROM `data` WHERE MONTH(`tdate`) = '$month' AND YEAR(`tdate`) = '$year' AND`data`.status = 'Routed'"; $query2 = mysql_query($rou); $totRou = mysql_fetch_object($query2); $total = ($totRes->resnum + $totRou->rounum); echo 'Resolved: ' . round ( ( ($totRes->resnum/$total) *100), 2) . '%<br>'; echo 'Routed: ' . round ( ( ($totRou->rounum/$total) *100), 2) . '%<br>'; Like that? Quote Link to comment Share on other sites More sharing options...
Zergman Posted October 27, 2008 Author Share Posted October 27, 2008 Awesome stuff, works like a charm. Thanks guys for the help, both suggestions work fantastic! Just curious now more than anything. Can either of these examples be adapted to more than 2 queries? Say I have a group of 8 queries that I want to calculate the percent. Can I just expand on these or are these meant for only 2? Quote Link to comment Share on other sites More sharing options...
IndyTechNerd Posted October 27, 2008 Share Posted October 27, 2008 Awesome stuff, works like a charm. Thanks guys for the help, both suggestions work fantastic! Just curious now more than anything. Can either of these examples be adapted to more than 2 queries? Say I have a group of 8 queries that I want to calculate the percent. Can I just expand on these or are these meant for only 2? yup, you can do this with as many queries as you want to give you parts of a whole. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted October 27, 2008 Share Posted October 27, 2008 You would just add all of the query results into the total then divide each number by the total in the same way. Quote Link to comment Share on other sites More sharing options...
Zergman Posted October 27, 2008 Author Share Posted October 27, 2008 Fantastic stuff! Thanks you guys for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.