douqiao8032 2012-09-23 21:14
浏览 67
已采纳

从另一个获取一个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

  • 写回答

3条回答 默认 最新

  • du958642589 2012-09-23 21:18
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊