i like to sum all row values from the $orderfeed_query. But when i echo the $sum, i get just the sum from the last loop.
how can can i add the sum of all following loop on the $sum variable? I dont know how many loops a order have.
$checkorder = mysql_query("SELECT * FROM orders WHERE `email` = '$email' ") or die(mysql_error());
while ($row = mysql_fetch_assoc($checkorder)) {
$orderid = $row["orderid"];
$check_order = $row["check_order"];
if($check_order[0] == 1){
$orderfeed_query = mysql_query("SELECT * FROM orderfeed WHERE `orderid` = '$orderid' AND `product` = '1'") or die(mysql_error());
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];
$sum = $signaturewiz+$flurstueckwiz+$uploadwiz+$exsignaturewiz+$ibanwiz;
echo $sum;
}
}
}
}
This code
$sum = $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;
overwrites sum each time. You must add to sum not to overwrite
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz + $exsignaturewiz + $ibanwiz;
And declare $sum = 0; before main loop
Several people have pointed out that $sum should be outside the loop, and that is indeed correct. However, MySQL can do this all for you:
$orderfeed_query = mysql_query("SELECT SUM(signaturewiz + flurstueckwiz + uploadwiz + exsignaturewiz + ibanwiz) FROM orderfeed WHERE orderid = '$orderid' AND product = '1'") or die(mysql_error());
if ($row = mysql_fetch_row($orderfeed_query)) {
echo $row[0];
}
Move the echo of $sum outside the loop
Use += rather than + to accumulate a total over multiple iterations
Initialize your variable before using += as if $sum has an undefined value it can mess up the count when using +=
$sum = 0; // init variable
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = $row["signaturewiz"];
$flurstueckwiz = $row["flurstueckwiz"];
$uploadwiz = $row["uploadwiz"];
$exsignaturewiz = $row["exsignaturewiz"];
$ibanwiz = $row["ibanwiz"];
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz +
$exsignaturewiz + $ibanwiz;
}
echo $sum;
ADDITIONAL INFO:
You see 12 and not 3 so the data from your table I assume is text and not numeric so do this to convert text numbers to integers
$sum = 0; // init variable
while ($row = mysql_fetch_assoc($orderfeed_query)) {
$signaturewiz = (int)$row["signaturewiz"];
$flurstueckwiz = (int)$row["flurstueckwiz"];
$uploadwiz = (int)$row["uploadwiz"];
$exsignaturewiz = (int)$row["exsignaturewiz"];
$ibanwiz = (int)$row["ibanwiz"];
$sum += $signaturewiz + $flurstueckwiz + $uploadwiz +
$exsignaturewiz + $ibanwiz;
}
echo $sum;
I got problem with this code. I've tried so many possible solution but still no update in phpmyadmin. The code run successfully through the footer since it say "Successfully Updated" but still no changes
<?php
if ($_POST){
$con = mysql_connect("localhost", "root", "pass123");
if (!$con){die('Could not connect:' . mysql_error());}
mysql_select_db("testing", $con) or die('Error:' . mysql_error());
$entry = $_GET['entry'];
$branch_id = $_POST['branch_id'];
$outletname = $_POST['outletname'];
$date = $_POST['date'];
$q11a = $_POST['q11a'];
$q12a = $_POST['q12a'];
$q13a = $_POST['q13a'];
$q14a = $_POST['q14a'];
$q15a = $_POST['q15a'];
$q16a = $_POST['q16a'];
$q17a = $_POST['q17a'];
$q18a = $_POST['q18a'];
$q19a = $_POST['q19a'];
$q20a = $_POST['q20a'];
$q21a = $_POST['q21a'];
$q22a = $_POST['q22a'];
$q23a = $_POST['q23a'];
$q24a = $_POST['q24a'];
$q25a = $_POST['q25a'];
$q26a = $_POST['q26a'];
$q27a = $_POST['q27a'];
$q28a = $_POST['q28a'];
$q29a = $_POST['q29a'];
$q30a = $_POST['q30a'];
$q31a = $_POST['q31a'];
$q32a = $_POST['q32a'];
$q33a = $_POST['q33a'];
$q34a = $_POST['q34a'];
$q35a = $_POST['q35a'];
$q36a = $_POST['q36a'];
$q37a = $_POST['q37a'];
$q38a = $_POST['q38a'];
$q39a = $_POST['q39a'];
$q40a = $_POST['q40a'];
$q41a = $_POST['q41a'];
$q42a = $_POST['q42a'];
$q43a = $_POST['q43a'];
$q44a = $_POST['q44a'];
$q45a = $_POST['q45a'];
$q46a = $_POST['q46a'];
$q47a = $_POST['q47a'];
$q48a = $_POST['q48a'];
$q49a = $_POST['q49a'];
$q50a = $_POST['q50a'];
$q51a = $_POST['q51a'];
$q52a = $_POST['q52a'];
$q53a = $_POST['q53a'];
$q54a = $_POST['q54a'];
$q55a = $_POST['q55a'];
$q56a = $_POST['q56a'];
$q57a = $_POST['q57a'];
$q58a = $_POST['q58a'];
$q59a = $_POST['q59a'];
$q60a = $_POST['q60a'];
$q61a = $_POST['q61a'];
$q62a = $_POST['q62a'];
$q63a = $_POST['q63a'];
$q64a = $_POST['q64a'];
$q65a = $_POST['q65a'];
$q66a = $_POST['q66a'];
$q67a = $_POST['q67a'];
$q68a = $_POST['q68a'];
$q69a = $_POST['q69a'];
$q70a = $_POST['q70a'];
$q71a = $_POST['q71a'];
$q72a = $_POST['q72a'];
$q73a = $_POST['q73a'];
$q74a = $_POST['q74a'];
$q75a = $_POST['q75a'];
$q76a = $_POST['q76a'];
$q78a = $_POST['q78a'];
$q78a1 = $_POST['q78a1'];
$q79a = $_POST['q79a'];
$q79a1 = $_POST['q79a1'];
$q80a = $_POST['q80a'];
$q81a = $_POST['q81a'];
$q82a = $_POST['q82a'];
$q83a = $_POST['q83a'];
$q84a = $_POST['q84a'];
$q85a = $_POST['q85a'];
$timein = $_POST['timein'];
$HOR = $_POST['HOR'];
$MODa = $_POST['MODa'];
$KL = $_POST['KL'];
$ateambox = $_POST['ateambox'];
$box35 = $_POST['box35'];
$box36 = $_POST['box36'];
$box41 = $_POST['box41'];
$box50 = $_POST['box50'];
$box53 = $_POST['box53'];
$box54 = $_POST['box54'];
$box55 = $_POST['box55'];
$box61a = $_POST['box61a'];
$box61aa = $_POST['box61aa'];
$box61b = $_POST['box61b'];
$box61bb = $_POST['box61bb'];
$box61c = $_POST['box61c'];
$box61cc =$_POST['box61cc'];
$box61d = $_POST['box61d'];
$box61dd = $_POST['box61dd'];
$box61e = $_POST['box61e'];
$box61ee = $_POST['box61ee'];
$box73a = $_POST['box73a'];
$box73b = $_POST['box73b'];
$box73c = $_POST['box73c'];
$box74a = $_POST['box74a'];
$box74b = $_POST['box74b'];
$box74c = $_POST['box74c'];
$box74d = $_POST['box74d'];
$box74e = $_POST['box74e'];
$box74f = $_POST['box74f'];
$box74g = $_POST['box74g'];
$box74h = $_POST['box74h'];
$box74i = $_POST['box74i'];
$box74j = $_POST['box74j'];
$box75a = $_POST['box75a'];
$box75b = $_POST['box75b'];
$box75c = $_POST['box75c'];
$box75d = $_POST['box75d'];
$box75e = $_POST['box75e'];
$box75f = $_POST['box75f'];
$box75g = $_POST['box75g'];
$box75h = $_POST['box75h'];
$box75i = $_POST['box75i'];
$box75j = $_POST['box75j'];
$box75k = $_POST['box75k'];
$box75l = $_POST['box75l'];
$box75m = $_POST['box75m'];
$box75n = $_POST['box75n'];
$box75o = $_POST['box75o'];
$box75p = $_POST['box75p'];
$box75q = $_POST['box75q'];
$box75r = $_POST['box75r'];
$box78a = $_POST['box78a'];
$box78b = $_POST['box78b'];
$box78c = $_POST['box78c'];
$box78d = $_POST['box78d'];
$box78e = $_POST['box78e'];
$box78f = $_POST['box78f'];
$box78g = $_POST['box78g'];
$box78h = $_POST['box78h'];
$box78i = $_POST['box78i'];
$box78j = $_POST['box78j'];
$box78k = $_POST['box78k'];
$box78l = $_POST['box78l'];
$box78m = $_POST['box78m'];
$box79a = $_POST['box79a'];
$box79b = $_POST['box79b'];
$box79c = $_POST['box79c'];
$box79d = $_POST['box79d'];
$box91a = $_POST['box91a'];
$box91b = $_POST['box91b'];
$box91c = $_POST['box91c'];
$box92a = $_POST['box92a'];
$box92b = $_POST['box92b'];
if ($q11a == -1){
$naarrival = $naarrival + 1;
}
if ($q12a == -1){
$naarrival = $naarrival + 1;
}
if ($q14a == -1){
$naarrival = $naarrival + 1;
}
if ($q15a == -1){
$naarrival = $naarrival + 1;
}
if ($q26a == -1){
$nadining = $nadining + 1;
}
if ($q31a == -1){
$nadining = $nadining + 1;
}
if ($q32a == -1){
$nadining = $nadining + 1;
}
if ($q44a == -1){
$nadining = $nadining + 1;
}
if ($q45a == -1){
$nadining = $nadining + 1;
}
if ($q78a == -1){
$naproduct = $naproduct + 1;
}
if ($q81a == -1){
$naproduct = $naproduct + 1;
}
if ($q83a == -1){
$napayment = $napayment + 1;
}
$arrival_na1 = $naarrival;
$arrival_ppna1 = 14 - $arrival_na1;
$totalarrival = $q11a + $q12a + $q13a + $q14a + $q15a + $q16a + $q17a + $q18a;
$arrival_score1 = $totalarrival / $arrival_ppna1 *100;
$dining_na1 = $nadining;
$dining_ppna1 = 31 - $dining_na1;
$totaldining = $q19a + $q20a + $q21a + $q22a + $q23a + $q24a + $q25a + $q26a + $q27a + $q28a + $q29a + $q30a + $q31a + $q32a + $q33a + $q34a + $q35a + $q36a + $q37a + $q38a + $q39a + $q40a + $q41a + $q42a + $q43a + $q44a + $q45a + $q46a + $q47a + $q48a + $q49a;
$dining_score1 = $totaldining / $dining_ppna1 *100;
$ordering_na1 = $naordering;
$ordering_ppna1 = 28 - $ordering_na1;
$totalordering = $q50a + $q51a + $q52a + $q53a + $q54a + $q55a + $q56a + $q57a;
$ordering_score1 = $totalordering / $ordering_ppna1 *100;
$product_na1 = $naproduct;
$product_ppna1 = 47 - $product_na1;
$totalproduct = $q58a + $q59a + $q60a + $q61a + $q62a + $q63a + $q64a + $q65a + $q66a + $q67a + $q68a + $q69a + $q70a + $q71a + $q72a + $q73a + $q74a + $q75a + $q76a + $q78a + $q78a1 + $q79a + $q79a1 + $q80a + $q81a;
$product_score1 = $totalproduct / $product_ppna1 *100;
$payment_na1 = $napayment;
$payment_ppna1 = 6 - $payment_na1;
$totalpayment = $q82a + $q83a + $q84a + $q85a;
$payment_score1 = $totalpayment / $payment_ppna1 *100;
$totalattentiveness1 = ($q16a + $q18a + $q51a + $q52a + $q55a + $q57a + $q85a) / 19 * 100;
$totalaccuracy1 = ($q50a + $q54a + $q56a) / 8 * 100;
$totalspeed1 = $q53a / 8 * 100;
$totalfriendliness1 = ($q17a + $q84a) / 6 * 100;
$totalpresentation1 = ($q63a + $q64a + $q67a + $q72a) / 5 * 100;
$totaltaste1 = ($q59a + $q61a + $q70a + $q71a + $q79a + $q80a) / 14 * 100;
$totalquality1 = ($q58a + $q60a + $q62a + $q65a + $q66a + $q68a + $q69a + $q73a + $q74a + $q75a + $q76a + $q78a + $q78a1 + $q79a1 + $q81a) / 28 * 100;
$total1 = $arrival_na1 + $dining_na1 + $ordering_na1 + $product_na1 + $payment_na1;
$total2 = $arrival_ppna1 + $dining_ppna1 + $ordering_ppna1 + $product_ppna1 + $payment_ppna1;
$total3 = $totalarrival + $totaldining + $totalordering + $totalproduct + $totalpayment;
$percentage = ($total3 / $total2) * 100;
if ($percentage >= 86) {
$grade = "A";
} else if ($percentage >= 70) {
$grade = "B";
} else if ($percentage >= 55) {
$grade = "C";
} else {
$grade = "D";
}
mysql_select_db("testing", $con) or die('Error:' .mysql_error());
$insert_query = "UPDATE a_team SET
date='$date',
q11a='$q11a',
q12a='$q12a',
q13a='$q13a',
q14a='$q14a',
q15a='$q15a',
q16a='$q16a',
q17a='$q17a',
q18a='$q18a',
q19a='$q19a',
q20a='$q20a',
q21a='$q21a',
q22a='$q22a',
q23a='$q23a',
q24a='$q24a',
q25a='$q25a',
q26a='$q26a',
q27a='$q27a',
q28a='$q28a',
q29a='$q29a',
q30a='$q30a',
q31a='$q31a',
q32a='$q32a',
q33a='$q33a',
q34a='$q34a',
q35a='$q35a',
q36a='$q36a',
q37a='$q37a',
q38a='$q38a',
q39a='$q39a',
q40a='$q40a',
q41a='$q41a',
q42a='$q42a',
q43a='$q43a',
q44a='$q44a',
q45a='$q45a',
q46a='$q46a',
q47a='$q47a',
q48a='$q48a',
q49a='$q49a',
q50a='$q50a',
q51a='$q51a',
q52a='$q52a',
q53a='$q53a',
q54a='$q54a',
q55a='$q55a',
q56a='$q56a',
q57a='$q57a',
q58a='$q58a',
q59a='$q59a',
q60a='$q60a',
q61a='$q61a',
q62a='$q62a',
q63a='$q63a',
q64a='$q64a',
q65a='$q65a',
q66a='$q66a',
q67a='$q67a',
q68a='$q68a',
q69a='$q69a',
q70a='$q70a',
q71a='$q71a',
q72a='$q72a',
q73a='$q73a',
q74a='$q74a',
q75a='$q75a',
q76a='$q76a',
q78a='$q78a',
q78a1='$q78a1',
q79a='$q79a',
q79a1='$q79a1',
q80a='$q80a',
q81a='$q81a',
q82a='$q82a',
q83a='$q83a',
q84a='$q84a',
q85a='$q85a',
totalarrival='$totalarrival',
arrival_score1='$arrival_score1',
totaldining='$totaldining',
dining_score1='$dining_score1',
totalordering='$totalordering',
ordering_score1='$ordering_score1',
totalproduct='$totalproduct',
product_score1='$product_score1',
totalpayment='$totalpayment',
payment_score1='$payment_score1',
totalattentiveness1='$totalattentiveness1',
totalaccuracy1='$totalaccuracy1',
totalspeed1='$totalspeed1',
totalfriendliness1='$totalfriendliness1',
totalpresentation1='$totalpresentation1',
totaltaste1='$totaltaste1',
totalquality1='$totalquality1',
timein='$timein',
HOR='$HOR',
MODa='$MODa',
KL='$KL',
ateambox='$ateambox',
box35='$box35',
box36='$box36',
box41='$box41',
box50='$box50',
box53='$box53',
box54='$box54',
box55='$box55',
box61a='$box61a',
box61aa='$box61aa',
box61b='$box61b',
box61bb='$box61bb',
box61c='$box61c',
box61cc='$box61cc',
box61d='$box61d',
box61dd='$box61dd',
box61e='$box61e',
box61ee='$box61ee',
box73a='$box73a',
box73b='$box73b',
box73c='$box73c',
box74a='$box74a',
box74b='$box74b',
box74c='$box74c',
box74d='$box74d',
box74e='$box74e',
box74f='$box74f',
box74g='$box74g',
box74h='$box74h',
box74i='$box74i',
box74j='$box74j',
box75a='$box75a',
box75b='$box75b',
box75c='$box75c',
box75d='$box75d',
box75e='$box75e',
box75f='$box75f',
box75g='$box75g',
box75h='$box75h',
box75i='$box75i',
box75j='$box75j',
box75k='$box75k',
box75l='$box75l',
box75m='$box75m',
box75n='$box75n',
box75o='$box75o',
box75p='$box75p',
box75q='$box75q',
box75r='$box75r',
box78a='$box78a',
box78b='$box78b',
box78c='$box78c',
box78d='$box78d',
box78e='$box78e',
box78f='$box78f',
box78g='$box78g',
box78h='$box78h',
box78i='$box78i',
box78j='$box78j',
box78k='$box78k',
box78l='$box78l',
box78m='$box78m',
box79a='$box79a',
box79b='$box79b',
box79c='$box79c',
box79d='$box79d',
box91a='$box91a',
box91b='$box91b',
box91c='$box91c',
box92a='$box92a',
box92b='$box92b',
arrival_na1='$arrival_na1',
arrival_ppna1='$arrival_ppna1',
dining_na1='$dining_na1',
dining_ppna1='$dining_ppna1',
ordering_na1='$ordering_na1',
ordering_ppna1='$ordering_ppna1',
product_na1='$product_na1',
product_ppna1='$product_ppna1',
payment_na1='$payment_na1',
payment_ppna1='$payment_ppna1',
total1='$total1',
total2='$total2',
total3='$total3',
percentage='$percentage',
grade='$grade' WHERE entry='$entry'";
mysql_query($insert_query) or die('Error:' . mysql_error());
mysql_close($con);
}
?>
Check whether you insert query give the correct query or not
echo $insert_qury="update query ";
and copy the output of insert query and test manually to past on sql in table it it update correctly then you query is correct and your error may be on "mysql_query"
You have put everything inside single quote...
Try this:
$insert_query = "UPDATE a_team SET
date='".$date."',
q11a='".$q11a."',
q12a='".$q12a."',..... WHERE entry='".$entry."'";
What I need is putting this array in a loop, but I can't get it to work because week is 1 - 9 and the key is 0 - 8. So i getting a error with a undefined offset i know why the does that but I don't know how to do this properly.
Before people ask me why not just change week1 to week0 and start counting from there. I can't because I did a calculating that is based on 1 - 52 and it will mess up my calculating if I start on 0 - 51
$totaal_vruchtzetting_week[10] = $totaal["week1"][0] + // = 0.46
$totaal["week2"][1] + // = 2.87
$totaal["week3"][2] + // = 4.97
$totaal["week4"][3] + // = 4.35
$totaal["week5"][4] + // = 3.02
$totaal["week6"][5] + // = 2.03
$totaal["week7"][6] + // = 1.41
$totaal["week8"][7] + // = 1.12
$totaal["week9"][8]; // = 1.13
// Should be total 21,36
Edit:
This is my loop I got until now but it gives me the wrong answer plus 2 errors
for($week = 1; $week < 9; $week++)
{
for($sw = 0; $sw <= 8; $sw++)
{
$totaal_vruchtzetting_week[10] += $totaal["week".$week][$sw];
}
}
echo $totaal_vruchtzetting_week[10]; // Outputs 170.89
$i=1;
$totaal_vruchtzetting_week[10]=0;
foreach($totaal as $total)
{
$totaal_vruchtzetting_week[10]+=$total["week$i"][$i-1];
$i++;
}
echo $totaal_vruchtzetting_week[10];
You should sum with this this loop
$i = 1;
$result = 0;
for ($i = 1; $i <= 9; $i++) {
if (isset($totaal['week' . $i]) && isset($totaal['week' . $i][$i - 1])) {
$result += floatval($totaal['week' . $i][$i - 1]);
}
}
$totaal_vruchtzetting_week[10] = $result;
$sum = 0;
foreach(array_values($totaal) as $index=>$item)
$sum += reset($item);
echo $sum; // 21.36
$totaal_vruchtzetting_week[10] = $sum;
Demo
Not sure if I got your question, but did you consider foreach? It iterates over every array regardless of the keys.
I am trying to implement the levenshtein algorithm with a little addon. I want to prioritize values that have consecutive matching letters. I've tried implementing my own form of it using the code below:
function levenshtein_rating($string1, $string2) {
$GLOBALS['lvn_memo'] = array();
return lev($string1, 0, strlen($string1), $string2, 0, strlen($string2));
}
function lev($s1, $s1x, $s1l, $s2, $s2x, $s2l, $cons = 0) {
$key = $s1x . "," . $s1l . "," . $s2x . "," . $s2l;
if (isset($GLOBALS['lvn_memo'][$key])) return $GLOBALS['lvn_memo'][$key];
if ($s1l == 0) return $s2l;
if ($s2l == 0) return $s1l;
$cost = 0;
if ($s1[$s1x] != $s2[$s2x]) $cost = 1;
else $cons -= 0.1;
$dist = min(
(lev($s1, $s1x + 1, $s1l - 1, $s2, $s2x, $s2l, $cons) + 1),
(lev($s1, $s1x, $s1l, $s2, $s2x + 1, $s2l - 1, $cons) + 1),
(lev($s1, $s1x + 1, $s1l - 1, $s2, $s2x + 1, $s2l - 1, $cons) + $cost)
);
$GLOBALS['lvn_memo'][$key] = $dist + $cons;
return $dist + $cons;
}
You should note the $cons -= 0.1; is the part where I am adding a value to prioritize consecutive values. This formula will be checking against a large database of strings. (As high as 20,000 - 50,000) I've done a benchmark test with PHP's built in levenshtein
Message Time Change Memory
PHP N/A 9300128
End PHP 1ms 9300864
End Mine 20ms 9310736
Array
(
[0] => 3
[1] => 3
[2] => 0
)
Array
(
[0] => 2.5
[1] => 1.9
[2] => -1.5
)
Benchmark Test Code:
$string1 = "kitten";
$string2 = "sitter";
$string3 = "sitting";
$log = new Logger("PHP");
$distances = array();
$distances[] = levenshtein($string1, $string3);
$distances[] = levenshtein($string2, $string3);
$distances[] = levenshtein($string3, $string3);
$log->log("End PHP");
$distances2 = array();
$distances2[] = levenshtein_rating($string1, $string3);
$distances2[] = levenshtein_rating($string2, $string3);
$distances2[] = levenshtein_rating($string3, $string3);
$log->log("End Mine");
echo $log->status();
echo "<pre>" . print_r($distances, true) . "</pre>";
echo "<pre>" . print_r($distances2, true) . "</pre>";
I recognize that PHP's built in function will probably always be faster than mine by nature. But I am wondering if there is a way to speed mine up?
So the question: Is there a way to speed this up? My alternative here is to run levenshtein and then search through the highest X results of that and prioritize them additionally.
Based on Leigh's comment, copying PHP's built in form of Levenhstein lowered the time down to 3ms. (EDIT: Posted the version with consecutive character deductions. This may need tweaked, by appears to work.)
function levenshtein_rating($s1, $s2, $cons = 0, $cost_ins = 1, $cost_rep = 1, $cost_del = 1) {
$s1l = strlen($s1);
$s2l = strlen($s2);
if ($s1l == 0) return $s2l;
if ($s2l == 0) return $s1l;
$p1 = array();
$p2 = array();
for ($i2 = 0; $i2 <= $s2l; ++$i2) {
$p1[$i2] = $i2 * $cost_ins;
}
$cons = 0;
$cons_count = 0;
$cln = 0;
$tbl = $s1;
$lst = false;
for ($i1 = 0; $i1 < $s1l; ++$i1) {
$p2[0] = $p1[0] + $cost_del;
$srch = true;
for($i2 = 0; $i2 < $s2l; ++ $i2) {
$c0 = $p1[$i2] + (($s1[$i1] == $s2[$i2]) ? 0 : $cost_rep);
if ($srch && $s2[$i2] == $tbl[$i1]) {
$tbl[$i1] = "\0";
$srch = false;
$cln += ($cln == 0) ? 1 : $cln * 1;
}
$c1 = $p1[$i2 + 1] + $cost_del;
if ($c1 < $c0) $c0 = $c1;
$c2 = $p2[$i2] + $cost_ins;
if ($c2 < $c0) $c0 = $c2;
$p2[$i2 + 1] = $c0;
}
if (!$srch && $lst) {
$cons_count += $cln;
$cln = 0;
}
$lst = $srch;
$tmp = $p1;
$p1 = $p2;
$p2 = $tmp;
}
$cons_count += $cln;
$cons = -1 * ($cons_count * 0.1);
return $p1[$s2l] + $cons;
}
I think the major slowdown in your function is the fact that it's recursive.
As I've said in my comments, PHP function calls are notoriously heavy work for the engine.
PHP itself implements levenshtein as a loop, keeping a running total of the cost incurred for inserts, replacements and deletes.
I'm sure if you converted your code to a loop as well you'd see some massive performance increases.
I don't know exactly what your code is doing, but I have ported the native C code to PHP to give you a starting point.
define('LEVENSHTEIN_MAX_LENGTH', 12);
function lev2($s1, $s2, $cost_ins = 1, $cost_rep = 1, $cost_del = 1)
{
$l1 = strlen($s1);
$l2 = strlen($s2);
if ($l1 == 0) {
return $l2 * $cost_ins;
}
if ($l2 == 0) {
return $l1 * $cost_del;
}
if (($l1 > LEVENSHTEIN_MAX_LENGTH) || ($l2 > LEVENSHTEIN_MAX_LENGTH)) {
return -1;
}
$p1 = array();
$p2 = array();
for ($i2 = 0; $i2 <= $l2; $i2++) {
$p1[$i2] = $i2 * $cost_ins;
}
for ($i1 = 0; $i1 < $l1; $i1++) {
$p2[0] = $p1[0] + $cost_del;
for ($i2 = 0; $i2 < $l2; $i2++) {
$c0 = $p1[$i2] + (($s1[$i1] == $s2[$i2]) ? 0 : $cost_rep);
$c1 = $p1[$i2 + 1] + $cost_del;
if ($c1 < $c0) {
$c0 = $c1;
}
$c2 = $p2[$i2] + $cost_ins;
if ($c2 < $c0) {
$c0 = $c2;
}
$p2[$i2 + 1] = $c0;
}
$tmp = $p1;
$p1 = $p2;
$p2 = $tmp;
}
return $p1[$l2];
}
I did a quick benchmark comparing yours, mine, and PHPs internal functions, 100,000 iterations each, time is in seconds.
float(12.954766988754)
float(2.4660499095917)
float(0.14857912063599)
Obviously it hasn't got your tweaks in it yet, but I'm sure they wont slow it down that much.
If you really need more of a speed boost, once you have worked out how to change this function, it should be easy enough to port your changes back into C, make a copy of PHPs function definitions, and implement your own native C version of your modified function.
There's lots of tutorials out there on how to make PHP extensions, so you shouldn't have that much difficulty if you decide to go down that route.
Edit:
Was looking at ways to improve it further, I noticed
$c0 = $p1[$i2] + (($s1[$i1] == $s2[$i2]) ? 0 : $cost_rep);
$c1 = $p1[$i2 + 1] + $cost_del;
if ($c1 < $c0) {
$c0 = $c1;
}
$c2 = $p2[$i2] + $cost_ins;
if ($c2 < $c0) {
$c0 = $c2;
}
Is the same as
$c0 = min(
$p1[$i2 + 1] + $cost_del,
$p1[$i2] + (($s1[$i1] == $s2[$i2]) ? 0 : $cost_rep),
$c2 = $p2[$i2] + $cost_ins
);
Which I think directly relates to the min block in your code. However, this slows down the code quite significantly. (I guess its the overhead of the extra function call)
Benchmarks with the min() block as the second timing.
float(2.484846830368)
float(3.6055288314819)
You were right about the second $cost_ins not belonging - copy/paste fail on my part.
I am trying to get the accumulative swing index for an aapl stock chart. I am using this calculation for reference.
http://www.barchart.com/education/std_studies.php?what=int_swing&hideheader=true#study
This is what I have written so far. This should return 252.09 but I cannot get it to work.
$asi[0] = -78.75
$ht = 584; // High today
$lt = 574.25; // low
$ct = 584.00; // close
$ot = 578; // open
$hy = 574; // High yesterday
$ly = 565.61;
$cy = 569.05;
$oy = 571.67;
$k = max(($hy-$ct),($ly-$ct));
$abc = array(($ht-$cy), ($lt-$cy), ($ht-$lt));
$max = max($abc);
$r = 0;
if($max == $abc[0]){
$r = ($ht-$cy)-.5*($lt-$cy)+.25*($cy-$oy);
}elseif($max == $abc[1]){
$r = ($lt-$cy)-.5*($ht-$cy)+.25*($cy-$oy);
}elseif($max == $abc[2]){
$r = ($ht-$lt)+.25*($cy-$oy);
}else{
echo "Error in welles accumulative swing index";
exit;
}
$l = 3 //period;
$val = 50 * (($cy - $ct) + .5 *($cy - $oy) + .25*($ct-$ot)) / $r * $k / $l;
$asi[] = $asi[$i-1] + $val;
Any help would be greatly appreciated.
I have tried to implement this index newly symbol-by-symbol and have get different result (swing: -248.7032967033 ).
May be your control value wrong?
That is my code:
class Swing
{
public function calculate($high_price, $low_price, $close_price, $open_price, $t)
{
// (Ct-1 - Ct)
$summand0 = ($close_price[$t-1] - $close_price[$t]);
// 0.5(Ct-1 - Ot-1)
$summand1 = 0.5 * ($close_price[$t-1] - $open_price[$t-1]);
// 0.25(Ct - Ot)
$summand2 = 0.25 * ($close_price[$t] - $open_price[$t]);
$limit_move_default = 3.0;
$r = $this->get_r_value($high_price, $low_price, $close_price, $open_price, $t);
$k = $this->get_k_value($high_price, $low_price, $close_price, $t);
$factor0 = 50.0 * ($summand0 + $summand1 + $summand2) / $r;
$factor1 = $k / $limit_move_default;
// SWING = 50 * ((Ct-1 - Ct)+ 0.5(Ct-1 - Ot-1)+ 0.25(Ct - Ot))/ R * K / M
return $factor0 * $factor1;
}
public function get_k_value($high_price, $low_price, $close_price, $t)
{
// K= MAX(| Ht-Ct-1|, | Lt-Ct-1|)
return max(
abs($high_price[$t] - $close_price[$t-1]),
abs($low_price[$t] - $close_price[$t-1]));
}
public function get_r_value($high_price, $low_price, $close_price, $open_price, $t)
{
// A. |Ht-Ct-1|
$a = abs($high_price[$t] - $close_price[$t-1]);
// B. |Lt-Ct-1|
$b = abs($low_price[$t] - $close_price[$t-1]);
// C. |Ht-Lt|
$c = abs($high_price[$t] - $low_price[$t]);
$max_value = max($a, $b, $c);
$d = abs($high_price[$t] - $low_price[$t]);
if($a == $max_value)
// R= (| Ht-Ct-1|)-.5(| Lt-Ct-1|)+.25(| Ct-1-Ot-1|)
return $a - 0.5 * $b + 0.25 * $d;
if($b == $max_value)
// R= (| Lt-Ct-1|)-.5(| Ht-Ct-1|)+.25(| Ct-1-Ot-1|)
return $b - 0.5 * $a + 0.25 * $d;
if($c == $max_value)
// R= (| Ht-Lt|)+.25(| Ct-1-Ot-1|)
return $c + 0.25 * $d;
}
};
$swing = new Swing();
$high_price = array(574.0, 584.0);
$low_price = array(565.61, 574.25);
$close_price = array(569.05, 584.0);
$open_price = array(571.67, 578.0);
$value = $swing->calculate($high_price, $low_price, $close_price, $open_price, 1);
echo("swing: $value \n");
$d looks wrong.
It should be abs($close_price[$t-1] - $open_price[$t-1]);