OK, what I want is to have an image on my websites front page. That image should change monthly. I would prefer to have the image not show up for at least 12 months (there are about 30 images to choose from right now with more coming).
I am thinking a combo of PHP and possibly javascript??? Not sure and don’t want to restrict any idea.
I have searched for something close, but, well, to quote a cliche “no cigar”.
Any help would be great.
Also, it has to have a caption (not the image file name).
I thought about having a couple of text files and maybe that is the best way.
However, my file would look like this:
Filename1.txt = file name 1
Filename2.txt = file name 2
Filename3.txt = file name 3
Filename4.txt = file name 4
Filename5.txt = file name 5
Filename6.txt = file name 6
Filename7.txt = file name 7
Filename8.txt = file name 8
Filename9.txt = file name 9
So, how would I read them into a dynamic array (since the pictures could grow/shrink over time)?
I found the explode() command, but what does that gain me?
Here is an example I have been looking at but don’t really understand.
while (!feof($f_handle1)) {
// get one line of text
$line_of_text = fgets($f_handle1);
// break it down because the file is [filename] = [caption] format
$imgs[] = explode('=', $line_of_text);
}
Not sure if $imgs will be dynamically increased and how - yet.
I have been working on something and want to run it past those out there with vastly more knowledge than me.
Here is my PHP script
<?php
$f_handle1 = fopen("MasterFile.txt", "r") ;
$f_handle2 = fopen("UsedFile.txt", "rb") ;
$counter = 0;
$found = false;
$imgs = array();
$t_imgs = array();
$used_imgs = array();
$t_used_imgs = array();
// read the main file and put into an array for processing.
while (!feof($f_handle1)) {
// get one line of text
$line_of_text = fgets($f_handle1);
// break it down because the file is [data] = [caption] format
$t_imgs[] = explode('=', $line_of_text);
$imgs[$counter]["url"] = $t_imgs[0];
$imgs[$counter]["caption"] = $t_imgs[1];
$counter++;
}
fclose($f_handle1); // close the file since we have what we need.
// now shuffle up the array by first element (keeping them together hopefully)
shuffle($imgs);
// read the used images array
$counter = 0;
while (!feof($f_handle2)) {
$line_of_text = fgets($f_handle2);
$t_used_imgs[] = explode('=', $line_of_text);
$used_imgs[$counter]["url"] = $t_used_imgs[0];
$used_imgs[$counter]["caption"] = $t_used_imgs[1];
}
$counter = 0;
while (!$found) {
// see if the first element of the main array is in the used array
if inarray($imgs[$counter]["url"], $used_imgs) {
$counter++;
}
else {
$found = True;
}
}
?>
Will this work? I have not had a chance to put it on the website to test it - our server is down right now and am doing this in notepad.
P.S. I know it is not complete yet - just want the feel from you.
OK, if I was to do that, the table would have 3 columns (id, url, caption), linked to another table that would contain 3 values (id, id_other_table, month_num).
then I could SELECT all records, joined to the second table by the id=>id_other_table, that did NOT have links, correct???
Now how can I make sure I pick a random record in the query results?
Then, I have to take the current month number in the linked (second) table and replace the id_other_table with the current record id I am using.
Make sense??? Not to me, but that is why I am here…
Image pool is going to be around 30 or more. So, the image may or may not appear once a year or maybe once every 3 years - just not sure how to make that happen, but …
Image = the path and filename of the image
Description = You text description
Status = three way value 1= available, 2 = current image; 3 = already used.
You display the image that has Status=2 everyday
1st of the month you run a script that takes the Status=2 row and set it to Status=3. Then randomly select a row where Status=1, and set that row to Status=2.
One question - at the end of using all the images, they then become all Status = 3, which then would “lock out” them for use, ie, there would be no Status = 1.
Have to think about how/when to trigger the status change for an already used to available. Might have to put a 4th field - dateused??? Then could change status from used to available if the dateused was over some time - like 18 months or so.
you could check that condition on the monthly run, if no rows are status 1, ie except for the current status 2 they are all status 3, then you change all the 3’s to 1’s, and the current 2 to a 3 (so it doesnt get chosen next month).
I just need to understand how to do a “monthly run” of the check and change the monthly image. Not sure how to do this - does it run by itself? I am worried about multiple people accessing the page and if that triggers a change of the image (status change described above).
if you have access to CRON, use it to run the script at 1am on the 1st of each month.
If not you’ll need to make a check on your index page of date/time, and if its the first time that the script has run in a month use that to start the script to change the image.
So, how would I know if I have access to CRON? And how would I know what CRON is? I am not a server side person but am learning more about it.
Or, how can I make the check on the index page if this is going to be basically static with just the PHP script doing the check of the database? Not really understanding this part.
No … but good point. IF there were that condition (let’s say a wintery picture), how would one go about making sure it did not appear in July for instance.
That was kind of my point really. I think someone further up mentioned ‘images by month’, maybe if you thought in terms of images by season (4) and applied some of the other solutions it might avoid this kind of jarring image (trees with no leaves in July and so on).