在Php

时间:2015-05-31 11:35:11

标签: php sql json

这是我的代码

<?php
Id  =$_GET['ID'];
error_reporting(0);
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ehealth monitoring", $con); 
$sth = mysql_query("SELECT DateandTime,BP FROM table  WHERE ID='".$Id."'");
$rows = array();
$rows['name'] = 'BP';
while($r = mysql_fetch_array($sth)) {
$rows['data'][] = [$r['DateandTime'],$r[eval('BP')]];
}
echo json_encode($rows, JSON_NUMERIC_CHECK);
mysql_close($con);
?>

在此代码中, BP 包含100 / 77,100 / 50,99 / 45种值。 我想将此值转换为十进制。我无法找到我的错误。请帮我转换值并将值传递给json。

2 个答案:

答案 0 :(得分:4)

此功能可以帮助您,随意根据需要修改功能。

   <?php
    echo convertToDecimal ("100/77");

    function convertToDecimal ($fraction)
    {
        $numbers=explode("/",$fraction);
        return round($numbers[0]/$numbers[1],6);
    }
    ?>

此函数会将分数转换为十进制。

答案 1 :(得分:0)

上述功能的小扩展:

<?php
echo convertToDecimal ("100/77/22");
function convertToDecimal ($sFraction, $iPrecision = 6, $sFractionSign = '/')
{
    $fResult = 0;
    $aNumbers=explode($sFractionSign,$sFraction);
    if(count($aNumbers) == 1){
        //user wrote a normal number with no fraction sign
        $fResult = $aNumbers[0];
    } else {
        //user wrote a fraction with at least one $sFractionSign
        $fResult = $aNumbers[0];
        foreach($aNumbers as $iNumber){
            $fResult /= $iNumber;
        }
    }
    return round($fResult, $iPrecision);
}
?>