Jump to content

percentage


techker

Recommended Posts

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.