Wednesday, August 18, 2010

how to add odd/even loop in array

1. ount the loop number


for($i=0;$i<$blah;$i++)
if($i&1){
// ODD
}else{
// EVEN
}



2. odd loop itteration


$oddLoop = false;
foreach ($findposts as $findpost):
//.....
if($oddLoop=!$oddLoop){
// code for odd loop numbers
}else{
// code for even loop numbers
}


3. Odd ID number

if ( ( $findpost->ID ) != $id ) {
if($findpost->ID & 1){
// ODD
}else{
//EVEN
}



The three ways are


1. Modulo
for ($i = 0; $i < 10; $i++)
{
 
if ($i % 2 == 0)
 
{
    echo
"even";
 
}
 
else
 
{
    echo
"odd";
 
}
}


2. Flipping boolean value

$even = true;
for ($i = 0; $i < 10; $i++)
{
 
if ($even)
 
{
    echo
"even";
 
}
 
else
 
{
    echo
"odd";
 
}

  $even
= !$even;
}


3. mentioned boolean operator

for ($i = 0; $i < 10; $i++)
{
 
if ($i & 1 == 0)
 
{
    echo
"even";
 
}
 
else
 
{
    echo
"odd";
 
}
}