Since this bit of code is very similar to the previous example,
we're just going to cover the pieces that are different.
We've got a new variable called $text which
is the name of the text file containing our descriptions. Let
me break off here and show you what the contents of the text
PICT0259.JPG,Our Halloween Pumpkin
PICT0271.JPG,Converting our garage to a halloween cave
PICT0282.JPG,My little ghoulish helpers
PICT0524.JPG,Ash meets Cinderella on her 7th B-day
PICT0633.JPG,Jayden's first snowday
One very important piece to remember is that you need to have
an empty line as the last line in the text file. If you don't then
the description for the last image won't show up. This is because
our loop condition is !feof and since we're
pulling in a whole line at a time from the text file, not having
the blank line at the end means your last entry has the end-of-file
marker at the end and therefore satisfies the condition and will
not pull that line into the array. Another thing to keep note of
is that there isn't a space on either side of the comma. This is
important. Put a space between it and the filename and you break
the script. Put it between the comma and the description and
you'll have a space in front of the description under the picture.
That last paragraph covered quite a bit of what is happening, but
let's go over it step by step. Our while condition
is testing to see if the end-of-file is TRUE. If so, it stops.
Otherwise it does the below.
Since our text file is delimited with a comma seperating the
image name and the description, we're going to explode the
line off that comma to seperate it into an array.
The problem we have to solve now is that the filename and the
description at this point are two seperate elements in the array.
To fix this we're going to reassign the filename as the KEY
and VALUE which is the description text. If you plan
on using commas in your description, then you need to use a
different delimiter (maybe a semi-colon).
Once this reassignment is done, here's what our array looks like:
[PICT0168.JPG] => Balloon Fiesta!
[PICT0259.JPG] => Our Halloween Pumpkin
[PICT0271.JPG] => Converting our garage to a halloween cave
[PICT0282.JPG] => My little ghoulish helpers
[PICT0524.JPG] => Ash meets Cinderella on her 7th B-day
[PICT0633.JPG] => Jayden's first snowday
Next we get the next line, and unset the $temp
array so it's empty for the next iteration.
The last thing we do is add a bit of code to the line which outputs
our td in the table to output a linebreak, and
ouput the description text.
Now if you were paying attention you'll notice I didn't mention
two lines in the above code. The reason being is that these
normally wouldn't be there in your final version. The line
$allfiles .= $file . ',<br />'; creates a
variable containing all the filenames ending in a comma, seperated
by a newline, which we output at the end with an echo.
This is just a helper for you to get a list of filenames to put
in your description text file so all you have to do is type the
description after it.
Well, that's how you do it. Pretty simple huh?