从另一个获取一个PHP变量的百分比

I am trying to get a percentage of a php variable from another, for example i have two databases of information, the first database is full of unidentified information. The second database is full of the information we have identified. and so i want to work out dynamically the percentage of the total information identified.

Below is a php function to work out a percentage,

//TOTAL ENTRIES INTO Table 1 FOR total data
$result   = mysql_query( "SELECT * FROM table1" );
$num_total = mysql_num_rows( $result );

 //TOTAL ENTRIES IN Table 2  FOR THE PERCENTAGE
$result   = mysql_query( "SELECT * FROM table2" );
$num_amount = mysql_num_rows( $result );

 function percent($num_amount, $num_total) {
 $count1 = $num_amount / $num_total;
 $count2 = $count1 * 100;
 $count3 = 100 - $count2;
 $count = number_format($count3, 0);
 echo $count;
 }

Both queries return the correct information of the total number of rows counted in the database, however the count variable does not return any information.

Any ideas or suggestions would be appreciated.

Thanks

Stan

You echo the result, but you do not return it from the function. That is probably the problem.

However, you can do the percentage calculation all in SQL:

SELECT 100*( SELECT COUNT(*) FROM table1 ) / ( SELECT COUNT(*) FROM table2 )
    AS percent;

Otherwise,

function percent($num, $total)
{
    return number_format((100.0*$num)/$total, 2);
}

My guess is that something like this should probably work (mind you, I have not tested this, so it probably needs tweaking):

<?php

//TOTAL ENTRIES INTO Table 1 FOR total data
$result   = mysql_query("SELECT COUNT(*) AS total FROM table1");
$table1_total = mysql_result($result, 0);

 //TOTAL ENTRIES IN Table 2  FOR THE PERCENTAGE
$result2 = mysql_query("SELECT COUNT(*) AS total FROM table2");
$table2_total = mysql_result($result2, 0);

function percent($num_amount, $num_total) {
    $count1 = $num_amount / $num_total;
    $count2 = $count1 * 100;
    $count3 = 100 - $count2;
    $count = number_format($count3, 0);
    echo $count;
}

percent($table1_total, $table2_total);

Try this:

    <?php

    function percent($num_amount, $num_total) {
     $count1 = $num_amount / $num_total;
     $count2 = $count1 * 100;
     $count = number_format($count2, 0);
     echo $count2."<br>";
     }

     percent(50,100);
     percent(13,100);
     percent(13,200);

    ?>