techker Posted February 10, 2012 Share Posted February 10, 2012 hey guys is it possible to have an output that will echo a percentage between 2 tables? like students vs detentions would give like 45%.. Link to comment https://forums.phpfreaks.com/topic/256809-percentage/ Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 With a little math, sure. function returnPercent($num1,$total) { return round($num / $total * 100) . "%"; } Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316523 Share on other sites More sharing options...
techker Posted February 10, 2012 Author Share Posted February 10, 2012 With a little math, sure. function returnPercent($num1,$total) { return round($num / $total * 100) . "%"; } Thx. So the $num1 = the first filed in table? $num = field 2 Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316783 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 err.. function should look like this. function returnPercent($num,$total) { return round($num / $total * 100) . "%"; } With a little math, sure. function returnPercent($num1,$total) { return round($num / $total * 100) . "%"; } thanks. So the $num1 = the first filed in table? $num = field 2 i don't follow... the above function will find the percentage of two numbers divided together. If this is not what you want, give an example of exactly what you want. Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316787 Share on other sites More sharing options...
techker Posted February 10, 2012 Author Share Posted February 10, 2012 so i have a table with a number of students.(names-profileid...) and i have a detention table(name-date-time..) so i want to do a stats page that will calculate the percentage between the number of students and the detentions.. example 20% of the students go to detention.. then i can use it for months -weeks -per teacher.... Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316788 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 so you would find the total number of students, total number of students that have detention or whatever, then plug them into the function. $percentage = returnPercent($students_detention_num,$total_students) Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316792 Share on other sites More sharing options...
techker Posted February 10, 2012 Author Share Posted February 10, 2012 ok thats not a problem. so returnPercent will automaticly calculate the %. thats cool..thx Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316805 Share on other sites More sharing options...
techker Posted February 10, 2012 Author Share Posted February 10, 2012 i get a Call to undefined function returnPercent() in /home/ecole514/public_html/V2/Main/Pourcentage.php on line 31 $x = 'SELECT COUNT(*) FROM Etudiant '; $result = mysql_query($x) or die(mysql_error()); $total_rows = mysql_fetch_row($result); $students_detention_num= $total_rows[0]; $x8 = 'SELECT COUNT(*) FROM Formulaire_S'; $result8 = mysql_query($x8) or die(mysql_error()); $total_rows8 = mysql_fetch_row($result8); $total_students= $total_rows8[0]; $percentage = returnPercent($students_detention_num,$total_students) Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316812 Share on other sites More sharing options...
scootstah Posted February 10, 2012 Share Posted February 10, 2012 Because you did not define the function with this: function returnPercent($num,$total) { return round($num / $total * 100) . "%"; } For the record, you don't have to use the function, you can just do the math if you want. $percentage = round(($students_detention_num / $total_students) * 100); Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316815 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 true, i use functions where I can for re-usability in my code, as i believe it should be. Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316822 Share on other sites More sharing options...
techker Posted February 10, 2012 Author Share Posted February 10, 2012 true, i use functions where I can for re-usability in my code, as i believe it should be. ok so i did it like this one $percentage = round(($students_detention_num / $total_students) * 100); but the number gives me this? Percentage 400% 270 total detentions 1080 total students 400 2701080 Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316826 Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 true, i use functions where I can for re-usability in my code, as i believe it should be. ok so i did it like this one $percentage = round(($students_detention_num / $total_students) * 100); but the number gives me this? Percentage 400% 270 total detentions 1080 total students 400 2701080 well, lets break it down (like MC hammer),if you did it correctly it would look like this. $percentage = round((270 / 1080) * 100); //down a step $percentage = round(.25 * 100); //down another step $percentage = round(25); $percentage = 25%; it seems that you have switched the parameters. Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316829 Share on other sites More sharing options...
jcbones Posted February 10, 2012 Share Posted February 10, 2012 Thats funny, I get 25% with that code. Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316832 Share on other sites More sharing options...
techker Posted February 10, 2012 Author Share Posted February 10, 2012 ya i reversed the calculation.thx for the help! Link to comment https://forums.phpfreaks.com/topic/256809-percentage/#findComment-1316834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.