Loop incrementing $i problem when using $i++ in the loop

I have an image and want to draw six equal rectangles across it side by side. Starting at the top left 0,0 and full height.

This sort of works but has a major problem; if I write $i++ anywhere it uses that instead of $i through the rest of the code!
This means it misses out rectangles - only creats 0 2 4 6

$result = '';
$value='';
$size = getimagesize('600438_l.jpg');
	$column = $size[0]/6;

$i = 0;
	 while( $i < 7) {
	
	ECHO $i.'<br>';
	$j = $i++;
	
	$result = ($column*$i).',0 '.($column*$j).','.$size[1].'<br>';
	
	$value = $value.$result;
	
	echo $value.'<br>';
	
	            $i++;
				}

0
590.5,0 0,3543

2
590.5,0 0,3543
1771.5,0 1181,3543

4
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543

6
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543

This works better but includes data twice:

$result = '';
$value='';
$size = getimagesize('600438_l.jpg');
	$column = $size[0]/6;

$i = 0;
$j = 0;
	 while( $i < 7) {
	
	echo $i.'<br>';
	$j = $i+$i;
	
	$result = ($column*$i).',0 '.($column*$j).','.$size[1].'<br>';
	
	$value = $value.$result;
	
	echo $value.'<br>';
	
	            $i++;
				}

0
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543

1
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543
590.5,0 1181,3543

2
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543
590.5,0 1181,3543
1181,0 2362,3543

3
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543
590.5,0 1181,3543
1181,0 2362,3543
1771.5,0 3543,3543

4
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543
590.5,0 1181,3543
1181,0 2362,3543
1771.5,0 3543,3543
2362,0 4724,3543

5
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543
590.5,0 1181,3543
1181,0 2362,3543
1771.5,0 3543,3543
2362,0 4724,3543
2952.5,0 5905,3543

6
590.5,0 0,3543
1771.5,0 1181,3543
2952.5,0 2362,3543
4133.5,0 3543,3543
0,0 0,3543
590.5,0 1181,3543
1181,0 2362,3543
1771.5,0 3543,3543
2362,0 4724,3543
2952.5,0 5905,3543
3543,0 7086,3543

Please can somebody point out where I am going wrong.

I have not tested the code, but from looking at your code, you can change this:

$j = $i++;
	
	$result = ($column*$i).',0 '.($column*$j).','.$size[1].'&lt;br&gt;';

to

$result = ($column*$i).',0 '.($column*++$i).','.$size[1].'&lt;br&gt;';

or

$result = ($column*$i++).',0 '.($column*$i).','.$size[1].'&lt;br&gt;';

The first one increase the $i variable before using it instead of $j, the second increase the $i after it was used in the first location.

Note. Remember to remove the ++$i at the bottom, as you dont need it there anymore.

Thank you @TheRedDevil first indications look good:

0
0,0 590.5,3543

1
0,0 590.5,3543
590.5,0 1181,3543

2
0,0 590.5,3543
590.5,0 1181,3543
1181,0 1771.5,3543

3
0,0 590.5,3543
590.5,0 1181,3543
1181,0 1771.5,3543
1771.5,0 2362,3543

4
0,0 590.5,3543
590.5,0 1181,3543
1181,0 1771.5,3543
1771.5,0 2362,3543
2362,0 2952.5,3543

5
0,0 590.5,3543
590.5,0 1181,3543
1181,0 1771.5,3543
1771.5,0 2362,3543
2362,0 2952.5,3543
2952.5,0 3543,3543

6
0,0 590.5,3543
590.5,0 1181,3543
1181,0 1771.5,3543
1771.5,0 2362,3543
2362,0 2952.5,3543
2952.5,0 3543,3543
3543,0 4133.5,3543

Seems like this whole thing could be waaaaaay simpler.

$columnWidth = $size[0] / 6;
$columnHeight = $size[1];

for ($nColumn = 0; $nColumn < 6; ++$nColumn) {
    $columnStart = $columnWidth * $nColumn;
    $columnEnd = $columnStart + $columnWidth;

    echo "$nColumn<br>\n";
    echo "$columnStart,0 $columnEnd,$columnHeight<br><br>\n\n";
}
1 Like

Thank you for the alternative method @Jeff_Mott it gives the correct output as well.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.