Page 1 of 1
A database-esque text file?
Posted: Tue Nov 25, 2008 11:07 am
by pillageTHENburn
Hello, I have been thoroughly enjoying the posts and information on this forum lately. Now I have run into a problem that I can not seem to solve on my own.
The situation (in very broad terms): I need to make a page that is as dynamic as possible that looks at a folder of images (screen shots from a documentary) and then displays said images (in rows of about 3) with an incrementing number for each image (1, 2, 3... etc. etc.) AND a name (and sometimes title) under each picture. The name and title can be edited directly from this page (click "edit" under any image and a textbox appears, edit name, click submit, the name changes).
Now before everyone tells me that the only way to do this is with a database I have some questions/thoughts...
First off I have succeeded in getting a form to post to a text file and print the content of said text file on the same page (so if my list had only one name I'd be in business). At this point I assumed that I could write a new name/title combination each on a new line and then read and edit said names/titles based on their line number (the order of the images). This proved more difficult than I had imagined, and I am now not even sure it can be done. My second thought was to have each image/name automatically create it's own text file, then there'd be only one name/title combination per text file.
The second part I tried to tackle is the dynamic creation of a page based on files in a folder (or even based on a text file with a list of names?). I thought this would be easy with some sort of auto incrementing function that repeats a chunk of code until it runs out of images (or names)... Again I may be getting in over my head on this one.
Here is an example of what I am trying to accomplish, the problem is that every time I make one of these pages I have to manually add the divs for each image by hand. I finally added a little php so that I only have to list the names once at the top of the file (define a bunch of variables) and the pages uses those names to display the image and create the names and titles:
http://76.162.62.12/screenshots/colorad ... lorado.php (the number in the parentheses are tape numbers)
I know this is a very in-depth question and I've probably been a little vague in parts, I didn't want to ramble too much and I'm pretty new to this whole thing so I figured it might be better if someone who knows what is going on had some input I'd have a better (re)starting point...
Thank you for your time and thank you in advance for your help!!
-Logan
Re: A database-esque text file?
Posted: Tue Nov 25, 2008 12:27 pm
by pickle
Welcome!
I think you're making this more complex than it needs to be. My guess is you're feeling a bit overwhelmed. Let me re-describe what I think your problem is to make sure we're on the same page:
- You've got a folder of images that you want to display on a page, in a 3 x ? grid.
- You also want to be able to change the filename and title associated with each image.
glob() is your best bet for getting the file list. I wouldn't worry about your text file yet. Once you get your list of files from
glob(), you can just loop through the array, outputting a <div>, <form>, and <img> for each file found.
As for storing the title, I too would suggest a database rather than a text file. The reasons being are that databases take care of file locking (so 2 users don't overwrite data), as well as removing the possibility of you parsing your file wrong & completely buggering up the structure. Have you ever heard of SQLite? It's a file-based database system. They're much easier to set up & handle than a full MySQL installation. You could even have a different SQLite database in each image folder you have - kind of the best of both worlds.
P.S. - I love your username.
Re: A database-esque text file?
Posted: Tue Nov 25, 2008 1:45 pm
by pillageTHENburn
Wow, I don't know how I get into these ruts of complication. You are correct:
"- You've got a folder of images that you want to display on a page, in a 3 x ? grid.
- You also want to be able to change the filename and title associated with each image."
I do not know much (read: anything) about the glob() function, but now I'm about to go do some more learning!!! That sounds like exactly what I need... I'm not sure exactly how I would go about looping and outputting however it sounds like something I'll find out when I look into that function.
I have never heard of SQLite but am not intrigued even further! I dabbled a teeny tiny little itty bitty bit with MySQL about 2 years ago and promptly forgot everything I knew as soon as I got what I was making working... but it sounds like it might be similar so perhaps those memories are still kicking around up there. I will also look up SQLite, that sounds really cool!
I won't ask silly questions until I look into those two things, (I'll assume that the integration of a dynamic page with a static (?) database will all be evident as I learn)
Thank you so very much for the pointers and observations! I'll go muck around with code and hope I don't hurt myself too badly! I'll post back what I figure out!
Advice and input is still more than welcome! Thanks again!
-Logan
p.s. thanks for the username compliment... a reminder to myself that the order of things is crucial.
Re: A database-esque text file?
Posted: Tue Nov 25, 2008 7:40 pm
by pillageTHENburn
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Ok, so the glob() worked like a dream. Took me a little bit to get it figured out but I think I got it working.
So now I get a page that is being created based on a folder of images, awesome. The part I will now tackle is the names and numbers. To see how it has gone so far you can go here:
http://76.162.62.12/screenshots/glob.php
Here's what I used (I did NOT write all of this, I merely edited it to fit my needs (thanks goes to NuclearPixel.com)).
Code: Select all
function repeatscreenshot($dir){
if(!$dir){$dir = '.';}
foreach(glob("$dir/*") as $item){$sort[]= end(explode('/',$item));}
$killit = array('screenshots_colorado.php');
$killcounter = 0;
foreach($sort as $sorteditem){
foreach($killit as $killcheck){
if(strtolower($sorteditem) == strtolower($killcheck))
{unset($sort[$killcounter]);}
}$killcounter++;}
if($sort){natsort($sort);}
foreach($sort as $item){$return[]= $item;}
if(!$return){return array();}
return $return;
}
Then I execute it with this
Code: Select all
$folder = 'colorado';
foreach(repeatscreenshot($folder) as $item)
{
echo '<fieldset style="width:240px; text-align:center;">
<legend style="margin:0px 5px 0px 5px;">'.$num.'
</legend>
<div class="item"><img src="'.$folder.'/'.$item.'" width="220" height="157"></div>
<div class="item">'.$item.'</div>
</fieldset>';
}
So that is as far as I got today... I know most of you could have done it in your sleep, but I learned a lot. I am off to tackle SQLite!
-Logan
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Re: A database-esque text file?
Posted: Wed Nov 26, 2008 3:25 am
by panic!
Why all this cowboy stuff and not a database?
Re: A database-esque text file?
Posted: Wed Nov 26, 2008 10:13 am
by pickle
I'll be honest, I don't really know what the heck is going on in that repeatscreenshot() function, but it's way more complex than anything I've written for these purposes. I'd highly recommend torching it & writing your own.
Basically you just want to use glob() to get an array of images from a folder. You can basically replace all of that function with 1 call to glob(). There are other considerations you might want to include, but by no means do you need those three foreach() loops. Heck, the last foreach() loop just makes $return a copy of $sort, but in a horribly inefficient manner.
Re: A database-esque text file?
Posted: Wed Nov 26, 2008 10:47 am
by pillageTHENburn
Pickle:
I was a little afraid that it might be more than I needed... I don't know enough to build something from scratch like that so I figured that since it worked I'd leave it well enough alone. I think part of what it does that I like is gives me an option of a "kill list" which is basically an array of file names that are in the folder I am using that I do not want to show up in my list... Though it is probably not entirely necessary.
I will do what you say here and try actually starting from scratch, that type of thing scares me but I suppose baptism by fire can be useful! Trial and error seems to be my friend at this point.
Panic!:
No no, I've conceded that a database is probably going to be the best option. I'm just a little scared of databases and don't need anything fancy so I thought I might be able to avoid it with text files... Now that I am aware of SQLite I think I may have found, as Pickle mentioned, a happy medium.
I started looking into it yesterday and got a little lost/overwhelmed, I'm sure that with a little more effort I'll get it figured out. I couldn't figure out if I needed to add any files to the directory where I will be running my php pages... From what I read on the SQLite website it didn't appear that I needed to, but everything I was finding was command line stuff and I don't know how that relates to me hosting something on my server (not locally).
If anyone has any quick answers to that stuff that would be awesome, otherwise I will continue to read and I'm sure I'll get it sorted soon enough.
Thank you again for all the help and advice!!
-Logan
p.s. Sorry about the code thing, I think I knew that, I just wasn't thinking....
Re: A database-esque text file?
Posted: Wed Nov 26, 2008 11:09 am
by pickle
If you're having trouble with the function, feel free to ask. Usually when I'm dealing with a complex function, I work backwards. Start by stating what I want the result of the function to be. Then I ask myself "ok, what needs to happen for that result to occur" & list off some major steps. Then I ask myself "how do those steps happen". Eventually the code gets written.
Honestly though, your function is pretty simple:
Code: Select all
function repeatScreenshot($dir = '.'){
return glob($dir.'/{*.jpg,*.JPG,*.jpeg,*.JPEG}',GLOB_BRACE);
}
If you don't want files to be shown, just don't end them any of the extensions listed there.
Re: A database-esque text file?
Posted: Wed Nov 26, 2008 11:46 am
by pillageTHENburn
Thank you for your patience... I never want to be the guy who posts on a forum expecting people to just give him the answer without any work on his part, that's why I've been trying to figure it out and learn it myself as well.
So I started with a blank page and wrote this:
Code: Select all
foreach (glob("colorado/*.jpg") as $filename) {
echo "$filename";
}
This does pretty much exactly what I expected it to do, which is print a list of the file names that are in said directory. It also prints "colorado" in front of each one because I have that listed there...
You suggested this as a starting point:
Code: Select all
function repeatScreenshot($dir = '.'){
return glob($dir.'/{*.jpg,*.JPG,*.jpeg,*.JPEG}',GLOB_BRACE);
}
I have not tried it yet but to me that looks like a much better (more efficient) way to get only the file type(s) I want. How do I go about actually using this function then? Please excuse my ignorance but I assume that I need to use foreach() somehow in order to loop through to get each file name and then use echo to print my surrounding code and the file name (sort of like the first clunky thing I tried only less... clunky)?
I'm sorry I really am trying to learn this as fast as I can and not have other people do the work for me. Thank you again for your generous help!
-Logan