Array not incrementing?

Hello everyone,

This past weekend has been decent when it comes to me learning PHP. However, I’m having a problem that I can’t figure out. For some reason, it appears that an array I have isn’t incrementing.

The array contains elements pulled in by a variable in a loop - I’m guessing that’s where the problem is, except that I AM getting the elements, but no incrementing. Here’s the code I’ve got with some notations:

[php]

<?php if ($handle = opendir('./event-titles')) { while (false !== ($fileName = readdir($handle))) { if ($fileName != "." && $fileName != "..") { $fileContent = $fileName; $openedFile=fopen("event-titles/".$fileContent,"r"); while(!feof($openedFile)) { $fileContent = fgets($openedFile); } $eventStuff = array($fileContent); foreach ($eventStuff as $key => $val) { echo $eventStuff."
";//Returns "Array". echo $key."
";//Returns "0" (zero), does not increment. echo $val."
";//Correctly returns each element in array. } fclose($openedFile); } } closedir($handle); } ?>

[/php]

I’ve gone through PHP.net and a bunch of other sites, but this one’s got me confused. It appears that I’m getting values into the array, since they’re being echoed out successfully, but I’m not sure what’s keeping the array from incrementing. (I need to increment so I can sort - I think.)

Dave

Well, your $fileContent is one line of data. then, you force it into an array name $eventStuff.
So, you would only get the one value out of that array. You are attempting to parse thru an
array that has never had keys set up. They would always be zero. Normally, you would parse
the lines data, in this case $fileContent and break it up into a $key and $value.

First, this section of your code:
while(!feof($openedFile))
{
$fileContent = fgets($openedFile);
}
Will load lines of data into $fileContent. If the lines are placed as-is into the variable.
Note that this is an array now made up of lines of the text file. (One entry per array line!)

The next code:
$eventStuff = array($fileContent);
Creates an array made from an array. (Again, no indexing or $key’s entered!)

So, now you try to use this data and separate it into a $key and $value pair without any $key’s ever being set up in either of the two arrays you see above. That is your problem. There is a way around this problem. You can use just a for instead of using the foreach. Just use the $fileContent and a for that parses thru the entire array and use a counter for the indexing instead of setting key’s. Did that make sense?
Something like this might work for you:
[php]
while ( !feof($openedFile) )
$fileContent = fgets($openedFile); //brackets not needed, only one line in while clause…

      for($i = 0; $i < count($fileContent); ++$i)
      {
        echo $i . "<br>";
        echo $fileContent[$i] . "<br/>";//Correctly returns each element in array.
      }
      fclose($openedFile);

[/php]
Something like that should work. I have not tested it as I did not know what is in your text file.
You can debug this using this line, which will print the contents of the array:
print_r($fileContent); //which will show you the array’s content…
If it is really a simple array such as “a”, “b”, “c”, it will show just those items.
If it is an associative array with $keys and $vals, it will show them “0”=>“a”, “1”=>“b”,“2”=>“c” …
Arrays will print showing sub-arrays also. So, if you printed your $eventStuff in your current code, it would show two arrays without any keys… (I think!) So…

Hope that helps! Good luck!

Hi ErnieAlex,

Thanks for the quick reply!

Earlier, I was trying to increment through using $i, etc. but it wasn’t giving me anything. I tried it this time and it’s still coming up blank. I also tried replacing [php]$i < count($fileContent)[/php] with [php]$i < count($openedFile)[/php] since I’m cycling through the files rather than cycling through the contents of the files (each file only has one line of content).

So this is what I’ve got, but it’s generating no output…

[php]

<?php if ($handle = opendir('./event-titles')) { while (false !== ($fileName = readdir($handle))) { if ($fileName != "." && $fileName != "..") { $openedFile=fopen("event-titles/".$fileName,"r"); while(!feof($openedFile)) $fileContent = fgets($openedFile); for($1 = 0; $i < count($fileContent) ++$i); { echo $i . "
"; echo $fileContent[$i] . "
"; } } } } ?>

[/php]

To be honest, I have a feeling there may be something funky going on with the server (I’ve tried suggestions from other people that also didn’t work). The server environment is weird for one thing - this is all running under a Movable Type blogging installation, but none of it is actually using Movable Type. All the files are uploaded as assets. And I have no access to a database. This is not a fun situation. ;D

Well, this quote:

(each file only has one line of content).

Makes a HUGE difference! You code is set up to read lines of each file not just one line!
The code should parse thru all the files in the folder/directory, so you should be getting some output.
(If the files exist!) First, look at the “event-titles/” folder on the server and make sure there are txt files in it.
Next, remove all the $i section of code. You do not need that if there is only one line per file.
Try this instead… Not sure about the Moveable-Type server… Let us know if it works for you!
[php]

<?php // Create fileContent array of all event titles... $fileContent = array(); if ($handle = opendir('./event-titles')) { while (false !== ($fileName = readdir($handle))) { if ($fileName != "." && $fileName != "..") { $openedFile=fopen("event-titles/".$fileName,"r"); $fileContent[] = file_get_contents($openedFile); } } } // Now display the event titles... foreach($fileContent as $key=>$title) { echo $key . "
"; echo $title . "
"; } ?>

[/php]
Well, that is formatted a bit more standard. It pulls all the filenames from the directory, loads each file into the content variable and then displays it. It does not check for there being no files in the folder…
Let us know if that works better for you…

Wow, VERY CLOSE! ;D

It’s actually incrementing now, which I haven’t been able to do since I began this simple little task Thursday (or was it Wednesday?). One minor problem…

With this line - [php]echo $title . “
”;[/php] - I’m getting no output from the line. When I change [php]echo $title . “
”;[/php] to [php]echo $fileContent . “
”;[/php], I get “Array” as the output.

As for the incrementing problem, I know now what I was doing wrong, but I’m not sure I know WHY it was wrong. ;D I was incrementing based on [php]$openedFile[/php] (the actual file names in the directory) rather than [php]$fileContent[/php]. I guess I’m not clear on why we’re counting the contents of the file versus the files themselves. It obviously works though.

Any idea about the empty output of the file contents? I did verify that there are files in the directory, and that each file does actually have content.

Addition: I also just now tried [php]foreach($fileContent as $key=>$fileContent)[/php] with [php]echo $fileContent . “
”;[/php], and the output is still empty. I keep bouncing between this reply form and my code, trying to see if I can figure out the problem before I click “post.”

I don’t know if it matters, but the current content of each file is in this test format - 20120210000—February.

Dave

Try this:

Right after creating the content just before the FOREACH put these lines:

print_r($fileContent);
die();

This will print the contents of the $fileContent array for debugging. This way we can see exactly what is inside it to see if I coded it’s creation incorrectly. Post back a few lines of the printout… Thanks!

PS: Normally an array is something like:
( somekey=>somevalue,
somekey2=>somevalue2,
somekey3=>somevalue3) So seeing the output of the array will help sort it out.

GOT IT!!!

I switched [php]file_get_contents[/php] to [php]fgets[/php]. Now I’m getting all the keys displaying as well as the associated content. Now it’s just a matter of sorting them (part two of my tribulations, but I’m close on that one).

ErnieAlex, I REALLY appreciate your help! You’ve pulled Excalibur from the stone, and the PHP kingdom is yours!

Dave

(By the way, I don’t know if I’m overdoing it with the PHP tags. Better safe than sorry I guess.)

No, tags are good! And, GREAT! Sorry, I was tired when I typed that new version out for you.

So, sorting is a no-brainer! Use the SORT function. Easy… LOL…

Just use this command after it is created before it is printed.
(Dump the echo of the keys if not needed! That was for debugging…)

[php]
asort($fileContent);
[/php]
That easy!!! Here is the link that explains it. You can sort in many other ways, but, that is an ascending sort based on “value” not “key”!
http://php.net/manual/en/array.sorting.php

Nothing I do is every right the first time. Or the second. Or the third… etc. :smiley:

Anyway, it’s working! I put the sort right before the foreach, and for the first time, I’m getting the data AND it’s sorted. It kind of feels like it must have felt the first time the scientists in Switzerland first turned on the Large Hadron Collider. ;D

I REALLY appreciate your help. This has had me stumped, and others online too, although it may be that they were misunderstanding what I was trying to do. If you saw the gyrations I’m going through to get some of these features working, you’d kick me in the rear. But of the people here at work, I’m the only one who will know… :smiley:

Great! I will mark the topic solved! Always a great feeling to solve the programming puzzle!

CYA in the bitstream!

Sponsor our Newsletter | Privacy Policy | Terms of Service