Hey, I'm just starting out with php and I've managed to pull images from a directory, and I would like to have captions added to each file that is being pulled. Very basic, I have: <?php $images = glob("directory/filename.jpg"); foreach($images as $image) { echo '<div class="imgholder"><img src="'.$image.'"><div class="mask">'.$caption.'</div></div>'; } ?> Now, I suppose I should use a an array, that checks $images for its filename, and associate specific filename with an caption to be displayed in the var $captions. How can I do this very simple? PHP beginner, Ziando
No. What you should do is store filenames, extension and caption in a database, and pull the information from the database (basically you create a db-call and check the table with the pictures), and when you get the filenames, you pull the matching file from whereever they're stored.
I decided to create a file, that listed the caption seperated with an "," followed by the url. Example: My image title,directory/pic001.jpg Then i had it search for the "," and then post the both parts in an echo. <?php $f = fopen("imagedirectory/captions.txt", "r"); while (!feof($f)) { $captions = explode(",",fgets($f)); echo '<div class="imageholder"><img src="'. $captions[1].'"><div class="mask">'.utf8_encode($captions[0]).'</div></div>'; } fclose($f); ?> Had to use the encode, since i use ÅÄÖ in my captions. Don't know if I used it properly. But then again, I am new at this.
That's a perfectly fine solution, in that it works. But it's not a workable solution - in that it needs constant manual updates etc. whenever you upload a new picture, for instance (dunno if that is a real problem, but it should at least be considered). Also, there's no need for the UTF-8 encode, if the website itself is coded correctly, and the txt-file saved as UTF-8.
Why is that? update your script to support json, usign php you can write to files you know... http://php.net/manual/en/function.file-put-contents.php step 1 : encode array to json + write to file step 2 : read from file & decode json to array Not that big of a deal...
Unless he has some sort of upload-function which allows him to add to the original file, whenever he uploads a new file, he'll have to add to that text-file manually (or, by all means, via a form) - granted, the same goes of course for storing the information in a database, but at least then it's possible to retrieve certain bits when needed (search, the ability to easily update, etc). Find a specific value in a text-file like he was talking about, and update the caption, for instance, is a pain in the ass, and will (more often than not) involve rewriting the whole file every time. If the file grows in size as well, you're gonna end up with other issues. So no, it's not a good solution. It's a workable, limited solution for small file-sets and not much change.
true, but a session is a file as well, not often i see people care about the size it gets. I would opt for a db , but maybe his host doesn't support it ... if he doesn't want to use a database... a file is the next best thing...
If his host doesn't support or provide a database, he needs a new host. If he doesn't want to use a database... well, sure, but then again, why create a more complex way of doing something when there are better options out there. However, all this is just guesswork, and not really relevant - there are "best practices" and then there are "it works" - I know what I would chose.