Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / Apache

Online PHP Software Example: Online Hypochlorite Dilution Calculator

4.67/5 (5 votes)
20 Jan 2017CPOL4 min read 33.5K   297  
Online PHP Software Example: Sodium Hypochlorite Dilution Calculator
Online software is easy to use and does not need installation to run. Your software and your code will be safe since they will not be distributed to the final user and it is easy to integrate with your site. here is an example of the Hypochlorite Dilution Calculator Source Code.

Downloads and Links

About Online software

  • Online software is easy to use and does not need installation to run
  • Your software and your code will be safe since they will not be distributed to the final user
  • Your software will be easy to integrate with your site

Introduction

  • Sodium hypochlorite solution is widely used in-home care, swimming water disinfecting, poultry production plants & many more.
  • The most know trade name of it is Clorox.
  • Many small plants buy the concentrated solutions in bulk for diluting and packing.
  • This calculator is for providing online calculations of the diluting.

Using this calculator:

  • Decide what concentration you want to produce example 4%w/w
  • Get the concentration of the stock solution you have.
  • Go to the online page for the calculation Online calculator
  • Enter your values then press calculates
  • the online calculator will use AJAX so the page will display the result without reloading it.
  • All the input data will be sent to a PHP script that will make the calculation.

Point of Interest:

This article discusses the following technique:

  • How to build simple online software that could be run on free hosting.
  • Simple implantation of ajax
  • Testing your software on your PC

Hosting requirements:

  • Free or paid hosting account
  • No database is needed for this software
  • The hosting should support PHP.

Why PHP?

  • It is the most server-side script available on the webserver even on free plans
  • It is run on both Windows and Unix server

Testing your code locally

To test your code locally you will need a web server with PHP support.

List of some easy-to-use servers with PHP support by default.

  • Uniform Server Zero: Uniform Zero takes a fresh look at Apache, MySQL, PHP, and Perl portability. It has a power full control panel
  • XAMPP: It is a very common Apache-based web server with PHP support
  • Server2Go: Portable Apache-based web server with PHP support. It could run on CD
  • AppServ
  • USBWebserver
  • WampServer

List of some easy-to-use servers that you should add PHP support by additional software.

  • Small HTTP server lightweight, very powerful, multi-functional webserver.
  • IIS: IIS is built in windows to adding PHP support just download PHP for IIs and run it

How does this application work?

  • This application depends on a table of concentration in different units and the density of the sodium hypochlorite solution. It contains also required excess sodium hydroxide for each concentration.
  • To calculate the quantity of water needed to dilute 8%w/w solution to be 4% solution as an example.
  • We search in the table for the input and output chlorine weight percentage and the density.
  • If we do not found the concentration we will calculate it depending on the nearest concentration
PHP
//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;
}

Hear is a part of the table

Cl %W/V Cl% Chlorometric
Degrees
°Cl
NaOCl %W/V NaOCl% Required
excess of
NaOH
%W/W
Specific Gravity
change when adding
1% NaOH
Specific Gravity
With required
excess of NaOH
Specific Gravity
Without 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

Storing table data in the array:

  • There are 3 data fields and the other fields are calculated fields.
  • I store the data fields in an array
    PHP
     $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"> 
  • I define fields constants
    $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);
  • Then I store the calculated fields in the array for easy searching.
  • I wrote an Html page that will be the interface of the application

Using AJAX to call PHP data

Once the user fills the form and press calculate button it will call the AjaxRefresh function.

This function will do the following

  • Store all form data in string
  • Create XMLHttpRequest object
  • Create a function that will handle the AJAX response
  • Open XMLHttpRequest object and send the data
JavaScript
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();
}

The PHP script will send the Html elements ids with their new values

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)