65.9K
CodeProject 正在变化。 阅读更多。
Home

在线 PHP 软件示例:在线次氯酸稀释计算器

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (5投票s)

2014年5月20日

CPOL

4分钟阅读

viewsIcon

34065

downloadIcon

329

在线 PHP 软件示例:次氯酸钠稀释计算器

下载和链接

关于在线软件

  • 在线软件易于使用,无需安装即可运行
  • 您的软件和代码将是安全的,因为它们不会分发给最终用户
  • 您的软件将易于与您的网站集成

引言

  • 次氯酸钠溶液广泛用于家庭护理、游泳池水消毒、家禽生产厂等。
  • 它的最知名商品名是 Clorox。
  • 许多小型工厂批量购买浓缩溶液用于稀释和包装。
  • 此计算器用于提供在线稀释计算。

使用此计算器

  • 确定您要生产的浓度,例如 4% w/w
  • 获取您拥有的原液的浓度。
  • 转到在线页面进行计算 在线计算器
  • 输入您的值,然后按计算
  • 在线计算器将使用 AJAX,因此页面将显示结果而无需重新加载它。
  • 所有输入数据都将发送到 PHP 脚本,该脚本将进行计算。

看点

本文讨论以下技术

  • 如何构建可在免费托管上运行的简单在线软件。
  • Ajax 的简单实现
  • 在您的 PC 上测试您的软件

托管要求

  • 免费或付费托管帐户
  • 此软件不需要数据库
  • 托管应支持 PHP。

为什么选择 PHP?

  • 它是网络服务器上最常用的服务器端脚本,即使在免费计划中也是如此
  • 它在 Windows 和 Unix 服务器上运行

在本地测试您的代码

要在本地测试您的代码,您需要一个支持 PHP 的 Web 服务器。

一些默认支持 PHP 的易于使用的服务器列表。

  • Uniform Server Zero: Uniform Zero 重新审视了 Apache、MySQL、PHP 和 Perl 的可移植性。它有一个功能强大的控制面板
  • XAMPP: 这是一个非常常见的基于 Apache 的 Web 服务器,支持 PHP
  • Server2Go: 基于 Apache 的便携式 Web 服务器,支持 PHP。它可以在 CD 上运行
  • AppServ
  • USBWebserver
  • WampServer

您应该通过附加软件添加 PHP 支持的一些易于使用的服务器列表。

  • Small HTTP server 轻量级、功能强大、多功能 Web 服务器。
  • IIS:IIS 内置于 Windows 中,要添加 PHP 支持,只需下载 PHP for IIs 并运行它

此应用程序如何工作?

  • 此应用程序依赖于不同单位的浓度表和次氯酸钠溶液的密度。它还包含每种浓度所需的过量氢氧化钠。
  • 例如,计算将 8% w/w 溶液稀释至 4% 溶液所需的水量。
  • 我们在表中搜索输入和输出氯的重量百分比和密度。
  • 如果我们找不到浓度,我们将根据最接近的浓度计算它
//This function will search for a given concentration 
//$var:  value to be searched
//$field:unit of the value 
function searchit($var, $field){
    global $hypochlorite_table;  
    //array contains the data table of chlorine concentrations 
    //and density and reqiured sodium hydroxide
    global $precision;           
    //precision for each field in the $hypochlorite_table array
    $var=round($var,$precision[$field]);
    if ( is_numeric ( $var )) $var = (trim($var) == '')? 0 :$var; else $var = 0;
    for ($index = 0; $index < count($hypochlorite_table); $index++) {
        if($var == $hypochlorite_table[$index][$field]){
            return outputrow($index);
        }elseif($var < $hypochlorite_table[$index][$field]){
            if(!$index) return outputrow($index);
            $delta = ($var-$hypochlorite_table[$index-1][$field])
/($hypochlorite_table[$index][$field]-$hypochlorite_table[$index-1][$field]);
            return outputrow($index,$delta);
        }
    }    
    return false;
}


function outputfield($field,&$row,$index,$delta=0){
    global $hypochlorite_table,$precision;
    if($delta==0){
        $row[$field]= round($hypochlorite_table[$index][$field], 
                $precision[$field]);
    }else{
        $row[$field]=round(
          $hypochlorite_table[$index-1][$field] 
        + $delta*($hypochlorite_table[$index][$field]
        - $hypochlorite_table[$index-1][$field]) 
        , $precision[$field]);
    }
}


function outputrow($index,$delta=0){
    $row=array();
    outputfield(ClWV     ,$row,$index,$delta);
    outputfield(d        ,$row,$index,$delta);
    outputfield(dChange  ,$row,$index,$delta);             
    outputfield(ClWW     ,$row,$index,$delta);
    outputfield(ClDegrees,$row,$index,$delta);
    outputfield(NaOClWV  ,$row,$index,$delta);
    outputfield(NaOClWW  ,$row,$index,$delta);
    outputfield(NaOH     ,$row,$index,$delta);
    outputfield(dNone    ,$row,$index,$delta);
    return $row;
}

这里是表格的一部分

Cl %W/V Cl% 氯量

°Cl
NaOCl %W/V NaOCl% 必需
过量
NaOH
%W/W
比重
添加时更改
1% NaOH
比重
带所需的
过量 NaOH
比重
没有 NaOH
0.0% 0.0% 0.0 0.0% 0.0% 0.25% 0.012 1.001 0.998
2.5% 2.4% 8.0 2.6% 2.5% 0.36% 0.044 1.041 1.025
5.0% 4.6% 16.0 5.2% 4.9% 0.47% 0.028 1.076 1.063
5.5% 5.1% 17.6 5.8% 5.3% 0.49% 0.027 1.083 1.070
6.0% 5.5% 19.2 6.3% 5.8% 0.51% 0.024 1.089 1.077
6.5% 5.9% 20.8 6.8% 6.2% 0.53% 0.023 1.096 1.084
7.0% 6.3% 22.4 7.3% 6.7% 0.55% 0.022 1.103 1.091
7.5% 6.8% 24.0 7.9% 7.1% 0.57% 0.019 1.109 1.098
8.0% 7.2% 25.6 8.4% 7.5% 0.59% 0.019 1.116 1.105

将表格数据存储在数组中

  • 有 3 个数据字段,其他字段是计算字段。
  • 我将数据字段存储在一个数组中
     $hypochlorite_table =array(
    array(00.0, 1.001, 0.012),
    array(01.0, 1.020, 0.064),
    array(01.5, 1.027, 0.053),
    array(02.0, 1.034, 0.050),
    array(02.1, 1.035, 0.049),
    array(02.5, 1.041, 0.044),
    array(02.6, 1.043, 0.041),
    ........
    </pre lang="php"> 
  • 我定义字段常量
    $fields=array(
    0=>'ClWV',       //Cl(%w/v): weight of chlorine in 100 ml of the solution
    1=>'d',          //Specific Gravity of Sodium Hypochlorite solution that 
                     //contains default required excess of Sodium Hydroxide
    2=>'dChange',    //Specific Gravity change when adding 1% NaOH
    3=>'ClWW',       //Cl%= ClWV /d
    4=>'ClDegrees',  //°Cl Chlorometric Degrees = = 3.2 * ClWV
    5=>'NaOClWV',    //NaOCl% w/v: weight of Sodium Hypochlorite in 100 ml of the solution 
    6=>'NaOClWW',    //NaOCl%: weight of Sodium Hypochlorite in 100 g of the solution  
    7=>'NaOH',       //NaOH%: Default required excess of Sodium Hydroxide for 
                     //this concentration of hypochlorite=0.25+ClWW*0.75/16
    8=>'dNone'       //Specific gravity of Sodium Hypochlorite solution that 
                     //does not contain any excess of Sodium Hydroxide    
    );
    
    foreach ($fields as $key => $value) define($value,$key);
    
  • 然后我将计算字段存储在数组中,以便于搜索。
  • 我编写了一个 Html 页面,它将是应用程序的界面

使用 AJAX 调用 PHP 数据

用户填写完表单并按下计算按钮后,它将调用 AjaxRefresh 函数。

此函数将执行以下操作

  • 将所有表单数据存储为字符串
  • 创建 XMLHttpRequest 对象
  • 创建一个将处理 AJAX 响应的函数
  • 打开 XMLHttpRequest 对象并发送数据
function AjaxRefresh(){

var q = 'ajax.php'  + '?id='        + 
'&CIn='         + CIn.value         + 
'&COut='        + COut.value        + 
'&dIn='         + dIn.value         + 
'&lang='        + lang.value        + 
'&NaOHIn='      + NaOHIn.value      + 
'&QOut='        + QOut.value        + 
'&UnitIn='      + UnitIn.value      + 
'&UnitInAr='    + UnitInAr.value    + 
'&UnitOut='     + UnitOut.value     + 
'&UnitOutAr='   + UnitOutAr.value   +
'&UnitQOut='    + UnitQOut.value    +
'&UnitQOutAr='  + UnitQOutAr.value  ;

var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState===4 && xmlhttp.status===200)
    var pairs = xmlhttp.responseText.split('&');
    for(i=0;i<pairs.length;i++){
       var pair = pairs[i].split('=');
       var element = document.getElementById(pair[0]);
        if(element===null){
        }else{ 
            try{element.innerHTML = pair[1].trim();}catch(e){};
            try{element.value = pair[1].trim();}catch(e){};
        };
    };
};
xmlhttp.open("GET",q,true);
xmlhttp.send();
}

PHP 脚本将发送 Html 元素 id 及其新值

 

© . All rights reserved.