Esempio n. 1
0
function applyFormula($formula, $values)
{
    $rawFormula = $formula;
    $ids = idsPerFormula($formula);
    $i = 0;
    foreach ($ids as $id) {
        if ($values[$i] == "") {
            return null;
        }
        $formula = preg_replace("/\\[#id{$id}\\]/", " " . $values[$i++] . " ", $formula);
    }
    $result = eval("return {$formula};") or die("<b style=color:red>ERROR IN FORMULA '{$rawFormula}'</b>");
    return $result;
}
Esempio n. 2
0
<?php 
/* NEW CALCULATION INSERT TO DB */
//check input
if (!isset($_POST['name'], $_POST['formula'], $_POST['unit'])) {
    die;
}
//process input
$name = mysql_real_escape_string($_POST['name']);
$form = mysql_real_escape_string($_POST['formula']);
$unit = mysql_real_escape_string($_POST['unit']);
//check for duplicates in name and formula
$duplicates = current(mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM calculations WHERE name='{$name}'")));
if ($duplicates > 0) {
    die("ERROR! This calculation name already exists!");
}
$duplicates = current(mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM calculations WHERE formula='{$form}'")));
if ($duplicates > 0) {
    die("ERROR! This calculation formula already exists!");
}
//check if formula is valid
include "calculations_library.php";
$ids = idsPerFormula($form);
$result = applyFormula($form, $ids);
//if it's incorrect, this function will die()
//all is ok! insert calculation query
$sql = "INSERT INTO calculations (name,formula,unit) VALUES ('{$name}','{$form}','{$unit}')";
mysql_query($sql) or die(mysql_error());
//success & go to new device
echo "<b>New Calculation '{$name}' Inserted Correctly!</b> ";
$id = current(mysql_fetch_array(mysql_query("SELECT MAX(id) FROM calculations")));
echo "<a href=calculation.php?id={$id}>VIEW</a>";
Esempio n. 3
0
		#newCalc:hover {background:lightgreen;transition:all 0.5s}
	</style>
	<b>+ Create New Calculation</b>
</div>

<!--CREATED CALCULATIONS-->
<div style=margin-top:2em>
<table cellpadding=10>
	<tr><th>Calculation Name<th>Formula<th>Devices involved<th>Unit<th>Options</tr>
<?php 
include 'calculations_library.php';
//to use idsPerFormula()
$sql = "SELECT * FROM calculations";
$res = mysql_query($sql) or die(mysql_error());
echo "<b>" . mysql_num_rows($res) . " calculations found</b>";
while ($row = mysql_fetch_array($res)) {
    $id = $row['id'];
    $name = $row['name'];
    $unit = $row['unit'];
    $formula = $row['formula'];
    //get devices ids array from formula
    $devicesInvolved = count(idsPerFormula($formula));
    echo "<tr>\n\t\t\t\t<td><a href=calculation.php?id={$id}>{$name}</a>\n\t\t\t\t<td>{$formula}\n\t\t\t\t<td align=center>{$devicesInvolved}\n\t\t\t\t<td>{$unit}\n\t\t\t\t<td><button onclick=\"if(confirm('Are you sure that you want to delete calculation id {$id}?'))\n\t\t\t\t\t\t\t\t\t{window.location='deleteCalculation.php?id={$id}'}\"\n\t\t\t\t\t\t\tstyle='background:red'>\n\t\t\t\t\t\t\tDelete Calculation</button>";
}
if (mysql_num_rows($res) == 0) {
    echo "<tr style=color:#666><td colspan=5>~No calculations created yet";
}
?>
</table>
</div>