Jump to content

while loop within a loop problem?


mitwess

Recommended Posts

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?

 

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.