I have a simple HTML page as a wrapper, then a PHP function to read information from a file and separate it into div tags (think separating comments or forum posts). It reads from a .txt flat file that is delimited between each section. This delimiter is supposed to increment a variable, then create a new div tag based on the value i (). It works for number 1 (the initialiser) and then it increments once, but it fails to increment more than once.
So my question is essentially what am I doing wrong? Am I modifying my variables incorrectly, or am I outputting HTML wrong?
You can assume the file is being opened and closed properly.
while (!feof($file)){
$currentPost = 1; # this is the counter (i)
$charA = fgetc($file); # check for delimiterA
$charB = fgetc($file); # check for delimiterB
$line = fgets($file); # read THE REST OF THE LINE
if ($charA == "$" && $charB == "!") { # this delimiter separates posts.
$currentPost = $currentPost + 1;
echo "</div> <div class=\"col$currentPost\">"; # close last div, make new div
} else if ($charA == "%" && $charB == "&") { # content is finished, close div
echo "</div>";
} else {
echo "$charA$charB$line<br>";
}
}
Initialize $currentPost = 1;
before the loop, not inside it.
The way your doing it now resets the $currentPost
to 1
every time the loop runs.
each time the while get executed the $currentPost = 1;
is reexecuted this is what you are doing wrong , you need to get the first initialization out of the while