是一个"开关"最适合在以下场景中使用?

时间:2015-09-20 15:20:33

标签: php switch-statement short

我在php中有一个很长的代码,使用switch或其他任何其他方法可以缩短它吗?我有一个工资率表,不同的州有不同的相同指定率。我想在表格中插入array个值,其中有特定的名称(desg)和state,然后相应地计算费率。

表格

id  desgn      rate   state
1   MAZDOOR    6600   MN
2   SKD-I      10890  MN
3   SKD-I      11400  MZ
4   SKD-II     9270   MN
5   SKD-II     9300   MZ

到目前为止我的脚本:

if(!empty($_POST['u'])) {
    foreach ($_POST['u'] as $uid) {
        $sql=mysql_query("select * from cpl_pay_temp where id='$uid' ");

        while ($cal = mysql_fetch_array($sql)) {

            $ticket=$cal['ticketno'];
            // $month_yr=$_POST['month'];
            $desgn=$cal['desg'];
            $empname=$cal['name'];
            $empdesg=$cal['desg'];
            $cat=$cal['sex'];
            $father=$cal['fathersname'];
            $present=$cal['day_present'];
            $state=$cal['state'];

            if(($desgn=='MAZDOOR')&& ($state=='MZ')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='MAZDOOR' and state='MZ'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='MAZDOOR')&& ($state=='MN')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='MAZDOOR' and state='MN'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            } 

            if(($desgn=='SKD-I') && ($state=='MZ')){
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-I' and state='MZ'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='SKD-I') && ($state=='MN')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-I' and state='MN'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='SKD-I') && ($state=='MZ')){
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-I' and state='MZ'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='SKD-II') && ($state=='MN')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-II' and state='MN'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='SKD-II') && ($state=='MZ')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-II' and state='MZ'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='SKD-III') && ($state=='MN')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-III' and state='MN'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='SKD-III') && ($state=='MZ')) {
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='SKD-III' and state='MZ'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            if(($desgn=='HSKD') && ($state=='MN')){
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='HSKD' and state='MN'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            } 

            if(($desgn=='HSKD') && ($state=='MZ')){
                $sql_rate=mysql_query("select * from cpl_rate_mast where desg='HSKD' and state='MZ'");
                $rate=mysql_result($sql_rate,$i,'rate') ;
                $pay=round($present*($rate/$days)); 
            }

            mysql_query("insert into  cpl_pay(billno,ticketno,name,desg,sex,fathersname,day_present,total_wages,from_date,to_date,status,state) values('$vrno','$ticket','$empname','$desgn','$cat','$father','$present','$pay','$fdate1','$tdate2','Y','$state' )  ") or die(mysql_error());

1 个答案:

答案 0 :(得分:2)

以下是我的评论的快速演示,如果您发现在整个页面中复制并粘贴相同代码,则会重新编写脚本。你似乎没有snippit中的所有变量,所以我不能保证它会按原样运行。注意,您需要切换到PDOmysqli_进行数据库连接:

<?php
function FetchCPL($array = false)
    {
        $rate   =   0;
        $design =   (!empty($array['design']))? mysql_real_escape_string($array['design']) : false;
        $state  =   (!empty($array['state']))? mysql_real_escape_string($array['state']) : false;
        $count  =   (!empty($array['count']) && is_numeric($array['count']))? $array['count'] : 1;

        if($design && $state) {
                $sql_rate   =   mysql_query("select * from `cpl_rate_mast` where `desg` = '".$design."' and `state` = '".$state."'");
                $rate       =   mysql_result($sql_rate,$i,'rate') ;
            }

         return $rate;
    }

    if(!empty($_POST['u'])) {
            foreach ($_POST['u'] as $uid) {
                    $sql    =   mysql_query("select * from `cpl_pay_temp` where `id` = '".mysql_real_escape_string($uid)."'");

                    while ($cal = mysql_fetch_array($sql)) {
                            $empdesg    =   $cal['desg'];
                            $pay        =   round($present*(FetchCPL(array("design"=>$cal['desg'],"state"=>$cal['state'], "count"=>$i))/$days));    
                            mysql_query("insert into  `cpl_pay`
                                (`billno`,`ticketno`,`name`,`desg`,`sex`,`fathersname`,`day_present`,`total_wages`,`from_date`,`to_date`,`status`,`state`) 
                                values('$vrno','".$cal['ticketno']."','".$cal['name']."','".$cal['desg']."','".$cal['sex']."','".$cal['fathersname']."','".$cal['day_present']."','$pay','$fdate1','$tdate2','Y','".$cal['state']."' )  ") or die(mysql_error());
                        }
                }
        }
?>