欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP+MySQL开发小项目的集合笔记整合

程序员文章站 2022-03-13 14:22:05
php+mysql开发小项目的集合笔记(三)关联数据库,并限制小数位数 mysql语句: select `id`,`tdc_planninghc`,`tdc_entryhc`,`tdc_...

    php+mysql开发小项目的集合笔记(三)关联数据库,并限制小数位数

    mysql语句:

    select 
    	`id`,`tdc_planninghc`,`tdc_entryhc`,`tdc_exithc`,`priority_num`,`priority_char` from rmk 
    	where `fix_version` = '19a_block' and `status` != 'cancelled'
    	order by 
    	case
    		when `priority_char` = 'highest' then 1
    		when `priority_char` = 'high' then 2
    		when `priority_char` = 'medium' then 3
    		when `priority_char` = 'low' then 4
    	end,
    	`priority_num` desc

    增加where语句可以限制显示的条数。

    从test_sizing中查询赋值到数据内:

    $tid = $row["id"];
    			$temp = "select * from test_sizing where  `rmk` =  '{$tid}' ";
    			$tresult = $mysqli->query($temp);
    			$trow = mysqli_fetch_array($tresult,mysqli_assoc);
    			if($trow == null){
    				$user->tdc_planninghc = "0";
    				$user->tdc_entryhc = "0";
    				$user->tdc_exithc = "0";
    				
    				}else{
    					for($i = 0;$i < 3;$i++){
    						$tsql = "select sum(headcount) from test_sizing where  `rmk` =  '{$tid}' and  `phase` =  '{$i}'";
    						$tresult = $mysqli->query($tsql);
    						$trow = mysqli_fetch_array($tresult,mysqli_assoc);
    						if($i == 0){
    							$user->tdc_planninghc = $trow['sum(headcount)'];
    						}else if($i == 1){
    							$user->tdc_entryhc = $trow['sum(headcount)'];
    						}else if($i == 2){
    							$user->tdc_exithc = $trow['sum(headcount)'];
    						}
    					}
    					
    				}

    首先,提取id,根据id查看test_sizeing表的数据,如相同数据进行,下一步查看phase数据为0的叠加为tdc_planninghc 。如果phase数据为1的叠加为tdc_entryhc ,如果phase数据为2的叠加为tdc_exithc。如果都没有,就赋值为字符串0。

    增加sprintf("%.1f",$first1)函数,限定小数点后一位。

    $firsty=null;
    			$first1=null;
    			$first2=null;
    			$first3=null;
    		for($i = 0;$i < count($djson);$i++){
    			$firsty = 1 + $i;
    			$threey = strval($firsty);
    			$djson[$i]["y"]=$threey;
    
    			$first1 = $first1 + floatval($djson[$i]["tdc_planninghc"]);
    //			$three1 = strval($first1);
    			$three1 = sprintf("%.1f",$first1);
    			$djson[$i]["tdc_planninghc"]=$three1;
    
    			$first2 = $first2 + floatval($djson[$i]["tdc_entryhc"]);
    //			$three2 = strval($first2);
    			$three2 = sprintf("%.1f",$first2);
    			$djson[$i]["tdc_entryhc"]=$three2;
    
    			$first3 = $first3 + floatval($djson[$i]["tdc_exithc"]);
    //			$three3 = strval($first3);
    			$three3 = sprintf("%.1f",$first3);
    			$djson[$i]["tdc_exithc"]=$three3;
    			
    		}

    如代码。最好是在随后限制,这样结果明显。