...

View Full Version : for loop integer variable always reads the same



ajetrumpet
03-03-2010, 10:41 AM
all,

I have this small loop that explodes a delimited text file:
$log = fread($handle, filesize($logfile));
fclose ($handle);

$log = explode(PHP_EOL . PHP_EOL, trim($log));
$numitems = count($log);
$cutoff = floor($numitems / 5);
$itemcounter = 0;

//FIRST TABLE HERE
echo '<div style="width:250px; position:absolute; left:0px"><table border="0" cols="1">';

for ($i = 0; $i = $cutoff * 1; $i++) {
echo '<tr>';
echo '<td align="center">' . $log[$i] . '</td>';
echo '</tr>';

if ($itemcounter == $cutoff * 1) {
break; }

$itemcounter = $itemcounter + 1;

}

echo '</table></div>';i is always reading as cutoff * 1. it doesn't start at 0 like it should. i'm sure the syntax is wrong, and i am not astute at PHP, so if someone could point out what i need to change here to get $i to start at 0 that would be great! thanks!

SKDevelopment
03-03-2010, 11:13 AM
for ($i = 0; $i = $cutoff * 1; $i++) {

should be


for ($i = 0; $i == $cutoff * 1; $i++)

or


for ($i = 0; $i <= $cutoff * 1; $i++)

You used assignment "=" instead of comparison "==" or "===".

[Paul Ferrie ]
03-03-2010, 11:14 AM
You loop should be something like

for ($i = 0; $i < $cutoff * 1; $i++) {

The second param of a for loop is an if statement of sorts so you can't have =.

In the loop you should always be checking for less than< or greater than >

Hope it helps

ajetrumpet
03-03-2010, 12:41 PM
thanks for the help you too. got it fixed!

[Paul Ferrie ]
03-03-2010, 12:46 PM
Nice one:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum