Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Showing a different picture everyday.

    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!
    -DR.Wong

    Wheres the food at?

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    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

    PHP Code:

    $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 

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    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.
    Leonard Whistler

  • #4
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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!
    -DR.Wong

    Wheres the food at?

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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!
    -DR.Wong

    Wheres the food at?

  • #9
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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!
    -DR.Wong

    Wheres the food at?

  • #10
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Quote Originally Posted by Kid Charming
    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

  • #11
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Quote Originally Posted by DR.Wong
    Anyone know about that filename into table thing?

    Thanks a lot!
    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?

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Well if you insist on loading up a database , here's a way to do it automatically:

    PHP Code:
    $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
        
    }


  • #13
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire
    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:

    PHP Code:
    $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++;


  • #14
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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

    Wheres the food at?

  • #15
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    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 :

    Code:
    <?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?
    -DR.Wong

    Wheres the food at?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •