mitwess Posted December 22, 2009 Share Posted December 22, 2009 so i have some code with a while loop which was working fine $l=0; $lx=f.$l; $j=1; while ( $j<500 ) { $query = "SELECT id, fb_user_id, friend_id, f0 FROM sim_rp WHERE id ='".$j."' "; $rs = mysql_query($query); $row = mysql_fetch_array($rs, MYSQL_ASSOC); $id1 = $row['fb_user_id']; $id2 = $row['friend_id']; $query = "SELECT $lx from users where fb_user_id ='".$id1."' "; $rs = mysql_query($query); if (mysql_num_rows($rs) > 0) { $row1 = mysql_fetch_array($rs, MYSQL_NUM); $compare1[$j] = $row1[0]; } $query = "SELECT $lx from users where fb_user_id ='".$id2."' "; $rs = mysql_query($query); if (mysql_num_rows($rs) > 0) { $row2 = mysql_fetch_array($rs, MYSQL_NUM); $compare2[$j] = $row2[0]; } ++$j; } //To find the correlation of the two arrays, simply call the //function Correlation that takes two arrays: $correlation = Correlation($compare1, $compare2); $correlation = round($correlation,3); //Displaying the calculated Correlation: //print $correlation; //The functions that work behind the scene to calculate the //correlation function Correlation($arr1, $arr2) { $correlation = 0; $k = SumProductMeanDeviation($arr1, $arr2); $ssmd1 = SumSquareMeanDeviation($arr1); $ssmd2 = SumSquareMeanDeviation($arr2); $product = $ssmd1 * $ssmd2; $res = sqrt($product); $correlation = $k / $res; return $correlation; } function SumProductMeanDeviation($arr1, $arr2) { $sum = 0; $num = count($arr1); for($i=0; $i<$num; $i++) { $sum = $sum + ProductMeanDeviation($arr1, $arr2, $i); } return $sum; } function ProductMeanDeviation($arr1, $arr2, $item) { return (MeanDeviation($arr1, $item) * MeanDeviation($arr2, $item)); } function SumSquareMeanDeviation($arr) { $sum = 0; $num = count($arr); for($i=0; $i<$num; $i++) { $sum = $sum + SquareMeanDeviation($arr, $i); } return $sum; } function SquareMeanDeviation($arr, $item) { return MeanDeviation($arr, $item) * MeanDeviation($arr, $item); } function SumMeanDeviation($arr) { $sum = 0; $num = count($arr); for($i=0; $i<$num; $i++) { $sum = $sum + MeanDeviation($arr, $i); } return $sum; } function MeanDeviation($arr, $item) { $average = Average($arr); return $arr[$item] - $average; } function Average($arr) { $sum = Sum($arr); $num = count($arr); return $sum/$num; } function Sum($arr) { return array_sum($arr); } but when i try to add an additional while loop around the whole thing, it doesn't work. any idea on why? or a work around when i add the following to the beginning of the above code (after $l =0;) while ( $l<50 ) { and this to the end of the above code (because i want to iterate the value of $l) ++l; } the script stops working. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/ Share on other sites More sharing options...
ChemicalBliss Posted December 22, 2009 Share Posted December 22, 2009 ++l; should be $l++; -CB- Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-982211 Share on other sites More sharing options...
sasa Posted December 22, 2009 Share Posted December 22, 2009 close loop before declaration of functions Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-982228 Share on other sites More sharing options...
mitwess Posted December 22, 2009 Author Share Posted December 22, 2009 the missing $ was a transcription error. can i not have a function within a loop? i really need it to be within the loop for what i'm trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-982238 Share on other sites More sharing options...
ChemicalBliss Posted December 22, 2009 Share Posted December 22, 2009 No you cannot redeclare functions (so the while loop would fail anyway). But, there is a slightly complicated work-around: you can do this: <?php $function_prefix = "func_"; $i = 0; $funcname1 = create_function('$arg1,$arg2','return $arg1." - ".$arg2;'); echo($funcname1("hello","world")."<br />"); $funcname2 = create_function('$arg1,$arg2','return $arg1." - ".$arg2;'); echo($funcname2("hello","world")); ?> Good Luck -CB- Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-982253 Share on other sites More sharing options...
teynon Posted December 22, 2009 Share Posted December 22, 2009 I dont know why you would ever need to declare a function inside of a loop. You should rethink your logic there. Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-982260 Share on other sites More sharing options...
mitwess Posted December 23, 2009 Author Share Posted December 23, 2009 well i have two sets of 20 database fields, each with @2000 records i wanted to compare each pair (loop one) for each field (loop two) loop one is two related records being grabbed from one of the 20 database fields, one at a time, 2000 times. loop two causes everything to happen again for the next iterative db field (++$l) the function to determine correlation needs to happen after loop one, when two sets of 2000 records have been loaded into an array for the correlation function, and then the process needs to restart (++$l) with the analysis if the next db field obviously i need to be doing it differently since my current design doesn't work, that's why i posted the question. i can't figure out another way to do it at present. Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-982812 Share on other sites More sharing options...
ChemicalBliss Posted December 23, 2009 Share Posted December 23, 2009 Maybe if you explain what your data is, and exactly your planning to do with the data, we can provide the logic you need . Then you can code it . -CB- Quote Link to comment https://forums.phpfreaks.com/topic/186002-while-loop-within-a-loop-problem/#findComment-983048 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.