Click to See Complete Forum and Search --> : Help in for loop....
dannyjoy
02-26-2006, 11:46 AM
How come I cant do this? Please help....
for ($m=0; $m < $option; $m++)
{
for($i=$m; $i<$d; $i=($m + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
}
The problem lies with the for loop saying $i=$m. How come I cant do this?
NogDog
02-26-2006, 12:09 PM
What is the code supposed to do? What is it not doing, or what error is being generated, which causes you to say it's not working and that the problem is with the $i=$m ?
dannyjoy
02-26-2006, 12:25 PM
i've tried it without the $i = $m and it works....the $i = $m is to make it loop for another specified number of times.
when i put $i = $m, the loop doesnt end
dannyjoy
02-26-2006, 01:13 PM
actually it was only
for($i=0; $i<$d; $i=($i + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
and this works....but because I want to make this whole loop to loop for another specified number of times , that's why I put in the other for loop....
NogDog
02-26-2006, 01:15 PM
You've got 7 variables and 2 arrays in the mix here. Without knowing the values of each, how can we analyze what's going on? This following works fine, but I had to initialize many of the variables and the arrays:
<?php
$option = 5;
$value = array (1,2,3,4,5,6,7,8);
$value1 = array (1,2,3,4,5,6,7,8);
$k = 0;
$match = "0";
$d = 5;
for ($m=0; $m < $option; $m++)
{
for($i=$m; $i<$d; $i=($m + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
}
?>
Mester Pediz
02-26-2006, 01:22 PM
You really need to post the whole script!
dannyjoy
02-26-2006, 03:29 PM
$criteria = 5;
$option = 4;
$d = ($criteria * $option);
$match = 0;
$k=0;
$value = array(1,2,3,4,2,3,4,1,2,3,4,3,2,3,4,1,3,3,2,1)
$value1 = array(1,2,3,2,3)
for ($m=0; $m < $option; $m++)
{
for($i = $m; $i < $d; $i = ($m + $option))
{
if ($value[$i] == $value1[$k])
{
$match = $match + 1;
$k++;
}
else
{
$k++;
}
}
echo $match;
}
I'm supposed to compare only certain values of array value with the value of array value1 and if they are equal, increase the match...which is just a counter to keep count...
It still doesnt work....
NogDog
02-26-2006, 04:29 PM
This is an infinite loop:
for($i = $m; $i < $d; $i = ($m + $option))
I think maybe what you want is:
for($i = $m; $i < $d; $i += ($m + $option)) // += instead of =