...

View Full Version : Showing a different picture everyday.



DR.Wong
08-23-2006, 09:03 PM
Hi! On my site, I have a funny picture on my homepage... now this was all fine and dandy for the first week or so, but then I realised that it may be fruitful to change it once in a while.

My good buddy has now hooked me up with 480 funny pics!

If I change the pic everyday, that is like a year and a half!

So, if I were to enter the names of all of these images into a database, is there a piece of code that would like increment a variable every 24hours.

So it would call the 'id' field of my table?

Get what I am trying to do?

Thanks a lot!:thumbsup:

mic2100
08-23-2006, 10:32 PM
u need to create a varaible that know waht day of the year it is so if u pass into you select statement then it will bring out a diff pic each day




$day = date('z') + 1; //this is done because the variable starts at 0 and ur table increment starts at 1

$query = "SELECT pic FROM pictures WHERE id = ".$day

Len Whistler
08-24-2006, 03:33 AM
Another option is to randomly select an image to be displayed every time the page is loaded using PHP. With over 400 images this may be the way to go and you could add or delete images without any code changes.

DR.Wong
08-24-2006, 03:29 PM
Thanks for the replies. I did think of doing the whole random thing, but my buddy was kind of upset when I suggested it. We want to make it so that you would want to come back the next day to see the next funny pic.

I think I will go with the day of the year thing.:)

Now comes the task of entering all of these images into the database!:(

I dont suppose you guys know how to enter all the files in a directory into a talble do you? haha.. sorry, I know I am asking a lot.

Have a look at my site, it is just for some friends and I to keep in touch when we leave school in the next few months. We want to make a little community of students in our year throughout South Africa.http://www.braddanick.com/

Thanks a lot!:thumbsup:

Fumigator
08-24-2006, 04:25 PM
Is your friend upset about a random pic because the picture would change more often than once a day? What if the picture is random, but only changes once a day? That would be simple, and wouldn't require you entering a lot of data into a database.

You'll need a table that stores a change_date and a file_name. Then you'll need a PHP function at the top of the page that checks to see if the change_date is today's date-- if it is, then just display the picture in file_name. If it isn't, then perform a random function on the picture directory to come up with a new picture, then update the table and set change_date to the current date, and the new picture in file_name.

The first person to load the page for the day will run through the code to pick a new picture; everyone else that day who loads the page will just see the selected picture.

Fumigator
08-24-2006, 04:27 PM
Thinking about it, you could use this method and not even do random-- just sort the files in the directory and loop through them until you find the last picture used, then select the next one.

Kid Charming
08-24-2006, 04:45 PM
Another option that'll get you through a year is to name your picture files by date, and then just format a date() call to generate the file that should be pulled up that day. Save you from having to deal with a cron or extra back and forth with a db. Of course, you'll have to remember to update filenames before the year's out.

DR.Wong
08-24-2006, 05:57 PM
Fumigator:
Yeah, that would work.. my friend just didnt want a user to see all the pics in one day by simply refreshing the page. Also, we want to make it so that a user never see's a picture twice on the homepage, they can view all the pics shown so far in our random gallery (by simply assigning a value of 1 or 0 to a column called 'viewed' and only displaying if it has been seen). I think your method solves this problem, but it looks like the script will be jumping all over the place. The solution above is a bit simpler.

Kid Charming:
That would also work quite well. It is very similar to the solution posted earlier, but if I do it your way, I think I will be doing more work by renaming every file!!! :)
It may be the same amount of work as entering every picture into the database, but I think doing it this way will stop me from making mistakes when renaming the files. This is why I ask if anyone knows how to place the names of every file into a table so there can be no human error.

Thanks tons for the great replies!
Anyone know about that filename into table thing?

Thanks a lot!:thumbsup:

DR.Wong
08-24-2006, 05:59 PM
by the way.. how do you guys rate my site so far?

Its just a hobby site, I am trying to keep it as easy to use and relaxing to the eye as possible!

Any constructive critisism would be great!

Nightfire
08-25-2006, 01:03 AM
Another option that'll get you through a year is to name your picture files by date, and then just format a date() call to generate the file that should be pulled up that day. Save you from having to deal with a cron or extra back and forth with a db. Of course, you'll have to remember to update filenames before the year's out.

Would be a real PITA having to work out what to put in filenames for 18 months at a time though

Nightfire
08-25-2006, 01:06 AM
Anyone know about that filename into table thing?

Thanks a lot!:thumbsup:

Just put the location of the file into the db, ie 'images/img1.jpg' just like you would any normal string. I'm guessing you know how to insert stuff in to the db?

Fumigator
08-25-2006, 01:12 AM
Well if you insist on loading up a database :), here's a way to do it automatically:



$filenameArray = glob("img/*.jpg"); //change this to your path and file extension
foreach ($filenameArray as $fileName) {
$query = "INSERT ...."; //your insert query
$result = mysql_query($query);
if (!$result) {
// error processing
}
}

Kid Charming
08-25-2006, 01:35 AM
Would be a real PITA having to work out what to put in filenames for 18 months at a time though

Not really. I would think something like this should do it:



$startdir = '/path/to/images/'

$images_array = scandir($startdir);

$counter = 0;

foreach( $images_array as $item )
{
if( $item != '.' && $item != '..' )
{
rename( $startdir . $item, '/path/to/new/images/' . date('whateverformatyouwant',strtotime('+' . $counter . ' day')) . '.jpg');
}
$counter++;
}

DR.Wong
08-31-2006, 05:22 PM
Thanks guys, the code is going to work like a charm. I have all the image names in my table and now I can generate the random image.

Before I implement the code though , I want to make thumbnails of the images so I can make a gallery showing all of the images that have been displayed.

I see it was really easy to place all the names into the table, so does anyone know of a tutorial or snippet of code that can automatically make thumbs of all the images and append something like 'tmb' to the end of each image name?

So like the thumb of 'image.jpg' would be named 'imagetmb.jpg'

Or should I just user a resizer on all the images and upload them again?

Thanks a lot guys!

DR.Wong
09-03-2006, 08:34 PM
Hey guys, I tried the code that uses the day of the year and I am getting this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

The thing is that line 1 is the include statement for another page and the error does not go away when I take that code away.

Here is the code I am using :


<?php
$randpic = date('z') + 1;
$username="myname";
$password="mypass";
$database="mydb";
mysql_connect('72.41.255.207:3306',$username,$password);
@mysql_select_db($database) or die( "Problem selecting database");
$query="SELECT * FROM rotatefunny WHERE id=$ranpic";
$result=mysql_query($query) or die(mysql_error());


while ($row = mysql_fetch_assoc($result)) {


$imgpath=$row["imgname"];
}

?>

I then echo $imgpath to get the pic

Know what is wrong?

Fumigator
09-03-2006, 09:17 PM
What does "echo $query;" display?

DR.Wong
09-10-2006, 07:57 PM
Hi. Sorry for the really really late reply.

I get the same error :

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Fumigator
09-10-2006, 08:04 PM
You didn't answer the question. When you stick "echo $query" in there, what do you see?

GJay
09-10-2006, 09:34 PM
$randpic = date('z') + 1;
^
...
$query="SELECT * FROM rotatefunny WHERE id=$ranpic";

missing a d

DR.Wong
09-11-2006, 07:59 PM
Thanks GJay, that works great! Thanks to all who posted!!

Have a great one!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum