php mysql 小项目_PHP+MySQL开发小项目的集合笔记整合

本文介绍了使用PHP和MySQL进行小项目开发的过程,特别是如何通过SQL查询关联数据库并限制小数位数。通过示例展示了如何根据ID获取相关数据,累加特定字段值,并使用sprintf函数格式化小数。代码实现中包含对查询结果的处理,以展示清晰的项目数据。
摘要由CSDN通过智能技术生成

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;

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值