Reply
How to build a PHPscript that allows you to update Images automatically?
Old 02-27-2008, 10:37 PM How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
I'd like to know how to set up a gallery of images that updates once every 24 hours, and when you click on the picture it will take you to a specific link.

Can anyone tell me where to look to get this script, or how to do it?


thank you,
Lashtal
Lashtal is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 02-28-2008, 01:50 AM Re: How to build a PHPscript that allows you to update Images automatically?
mtishetsky's Avatar
King Spam Talker

Posts: 1,120
Name: Mike
Location: Mataro, Spain
What do you mean with 'updates every 24 hours'? What is an 'update' in your case?
__________________
Free Mobile Phone Themes

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 02-28-2008, 02:12 PM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
I mean, the ability to have an image automatically switch to another image (already stored in a particular folder) once every 24 hours.

Say I got this Image right here...

((IMAGE OF TREES AT MY FAVORITE PARK))

How do I build a PHPscript that allows me to have that image automatically change itself to another one once every 24 hours?
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 02-28-2008, 02:13 PM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
I'm trying to make my gallery auto-updating. And: On A Timer.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 02-28-2008, 03:29 PM Re: How to build a PHPscript that allows you to update Images automatically?
Gilligan's Avatar
Dead Like Me

Posts: 1,618
Name: Stefan
Location: London, UK
I am very new to php, so this is a first

How many images? If its more than seven then i'm afraid theres a lot more copying and pasting skip to code 2

Code 1


Just use the time() function and an elseif statement(s). To update it every 24 hours, you could use days.

PHP Code:
<?php
$day
=date("D");

if (
$day=="Mon")
  echo 
'<img src="1.gif" />'
elseif (
$day=="Tue")
  echo 
'<img src="2.gif" />'
elseif (
$day=="Wed")
  echo 
'<img src="3.gif" />'
elseif (
$day=="Thu")
  echo 
'<img src="4.gif" />'
elseif (
$day=="Fri")
  echo 
'<img src="5.gif" />'
elseif (
$day=="Sat")
  echo 
'<img src="6.gif" />'
elseif (
$day=="Sun")
  echo 
'<img src="7.gif" />'
  
?>
Code 2

now this one can only be for 366 images

for the sake of an example, i'll only use 10.

PHP Code:
<?php
$day
=date("WD");

if (
$day=="01Mon")
  echo 
'<img src="1.gif" />'
elseif (
$day=="01Tue")
  echo 
'<img src="2.gif" />'
elseif (
$day=="01Wed")
  echo 
'<img src="3.gif" />'
elseif (
$day=="01Thu")
  echo 
'<img src="4.gif" />'
elseif (
$day=="01Fri")
  echo 
'<img src="5.gif" />'
elseif (
$day=="01Sat")
  echo 
'<img src="6.gif" />'
elseif (
$day=="01Sun")
  echo 
'<img src="7.gif" />'
elseif (
$day=="02Mon")
  echo 
'<img src="8.gif" />'
elseif (
$day=="02Tue")
  echo 
'<img src="9.gif" />'
elseif (
$day=="02Wed")
  echo 
'<img src="10.gif" />'
  
?>
Bare in mind that the above will only work for the first two weeks of jan

Last edited by Gilligan : 02-28-2008 at 03:33 PM.
Gilligan is offline
Reply With Quote
View Public Profile
 
Old 02-28-2008, 04:39 PM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
How do I make the code select images At Random?

and, how do I get it alternate between an Infinite Number of images; instead of just 366?

thank you very much for your help thus far. I look forward to learning even more, I just can't find what i'm looking for on Google, so I thought i'd ask the masters.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 02-28-2008, 04:52 PM Re: How to build a PHPscript that allows you to update Images automatically?
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: Jerod Lycett
Location: /home/arenlor
Better is this:
PHP Code:
<?php $max =10;//Set this to how many images you have.
$d=date("z");
$query "SELECT * FROM `updated`";
$result mysql_query($query);
$l mysql_fetch_array($result);
if(
$d $l[0] || ($d == && $l[0] == 365)){
    
$img rand(1,$max);//If you want to start images with 0 use 0 not 1
    
$query "UPDATE TABLE `updated` SET updated = $d && image = $img where updated= $l[0]";
    
$result $query($query);
    echo 
'<img src="images/'.$img.'.jpg" alt="Daily Image">';
}
else{echo 
'<img src="images/'.$l[1].'.jpg" alt="Daily Image">';}?>
What happens is $max is set to your number of images, which are kept in the folder images which is inside the folder that the script is in. It then gets the date, and the date last updated from a mysql table which has only two columns and one row in it, the first column is `updated` and it holds the day of the year that it is, the second contains the daily image. If it's a new day since the script last executed it'll then choose a random image eg (images/1.jpg or images/2.jpg) and display, and it'll update the table with the new updated date and the new image, if it's not a new day it'll skip all that and just output the daily image.
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 02-28-2008, 07:31 PM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
Alright.
Let me start off by saying that i'm using Windows Vista, and i'm trying to send this Script to my webhost at GoDaddy.com

I went to MySQL, and I tried to Import the following Script:

"<?php $max =10;//Set this to how many images you have.
$d=date("z");
$query = "SELECT * FROM `updated`";
$result = mysql_query($query);
$l = mysql_fetch_array($result);
if($d > $l[0] || ($d == 0 && $l[0] == 365)){
$img = rand(1,$max);//If you want to start images with 0 use 0 not 1
$query = "UPDATE TABLE `updated` SET updated = $d && image = $img where updated= $l[0]";
$result = $query($query);
echo '<img src="images/'.$img.'.jpg" alt="Daily Image">';
}
else{echo '<img src="images/'.$l[1].'.jpg" alt="Daily Image">';}?>"

But I received an error message, Error #1064
saying that it cannot read specfically the punctuation "<?php"

So I am very confused...

Last edited by Lashtal : 02-28-2008 at 10:14 PM.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 02-28-2008, 10:36 PM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
Here is the error message I get...

ERROR
There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unknown Punctuation String @ 1
STR: <?
SQL: <?php
$max =10;

SQL query:

MySQL said:
#1064 - 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 '<?php
$max =10' at line 1

Last edited by Lashtal : 02-28-2008 at 10:40 PM.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 02-28-2008, 11:01 PM Re: How to build a PHPscript that allows you to update Images automatically?
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: Jerod Lycett
Location: /home/arenlor
That is PHP not just MySQL both $query are SQL statements.
Code:
CREATE TABLE updated(updated int(11),image int(11);
INSERT INTO `updated` values(0,1);
Run that and it'll create the table them populate it with an entry. Put the PHP into a page and edit anything you need to (such as $max or where your images are held) and make sure you have the images made and then run that page, it should output a random image. Also you'll need to connect to MySQL before hand so after the <?php but before $query put these lines in and customize them:
PHP Code:
$link mysql_connect('localhost''mysql_user''mysql_password') or die("Could not connect: ".mysql_error());
$db_select mysql_select_db('db_name',$link) or die("Could not select: ".mysql_error()); 
Add this line right before the ?>
PHP Code:
mysql_close($link); 
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 02-29-2008, 04:48 AM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
How do I assign specific links to each image with this code?

and

How can I modify it to display .gif images as well?
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 02-29-2008, 11:53 AM Re: How to build a PHPscript that allows you to update Images automatically?
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: Jerod Lycett
Location: /home/arenlor
If no one else grabs this I'll get to it tonight.
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 03-02-2008, 05:22 PM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
Please?
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 03-04-2008, 11:24 PM Re: How to build a PHPscript that allows you to update Images automatically?
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: Jerod Lycett
Location: /home/arenlor
Sorry it took so long to get around to this, had a very insane past few days. The way this works is we're going to create a second table that is the following:
Code:
CREATE TABLE images(image int(11) primary key auto_increment, name varchar(40) unique, link text);
This will make a new table that has three columns in it, the first column is named image and is the number of the image as used in the PHP code I gave you the first time around. The second column is the name of the image, blah.gif or null.jpg, the image names all must be unique. The third column is the link for the image.
Instead of the PHP used above we will be using a new script:
PHP Code:
<?php $link mysql_connect('localhost''mysql_user''mysql_password') or die("Could not connect: ".mysql_error());
$db_select mysql_select_db('db_name',$link) or die("Could not select: ".mysql_error());
$max =10;//Set this to how many images you have.
$d=date("z");
$query "SELECT * FROM `updated`";
$result mysql_query($query);
$l mysql_fetch_array($result);
if(
$d $l[0] || ($d == && $l[0] == 365)){
    
$img rand(1,$max);
    
$query "UPDATE TABLE `updated` SET updated = $d && image = $img where updated= $l[0]";
    
$result mysql_query($query);
    
$query "SELECT * FROM `images` where image=$img";
    
$result mysql_query($query);
    
$array mysql_fetch_array($result);
    echo 
'<a href="'.$array[2].'"><img src="images/'.$array[1].'" alt="Daily Image"></a>';
}
else{
$query "SELECT * FROM updated, images WHERE updated.image = images.image";
    
$result mysql_query($query);
    
$array mysql_fetch_array($result);
    echo 
'<a href="'.$array[4].'"><img src="images/'.$array[3].'" alt="Daily Image"></a>';}
mysql_close($link);?>
Make sure to run this SQL command too if you haven't already:
Code:
CREATE TABLE updated(updated int(11),image int(11));
INSERT INTO `updated` values(0,1);
All you have to do to insert a new picture is run this SQL changing IMAGE_NAME for the image's name and LINK_LOCATION to the link (including http://) DO NOT REMOVE THE QUOTES!!:
Code:
INSERT INTO `images` VALUES(NULL,'IMAGE_NAME','LINK_LOCATION');
An example of this would be:
Code:
INSERT INTO `images` VALUES(NULL,'blah.png','http://example.com');
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 03-05-2008, 05:36 AM Re: How to build a PHPscript that allows you to update Images automatically?
Lashtal's Avatar
Ultra Talker

Posts: 325
Name: Lashtal
So I managed to successfully create the tables.

I was even able to successfully add more images to the desired 'images' table.

But I am confused about what to do with the PHPscript itself.

Do I insert it into wordpad and save it as ".php", then upload it to my 'images' table?

Or do I just insert it into my webpage?


This is my first time executing a PHPscript, so please remember that you're speaking to a newbie here

And thank you so far, for a lot of your help!

Last edited by Lashtal : 03-06-2008 at 04:00 AM.
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 03-06-2008, 10:24 PM Re: How to build a PHPscript that allows you to update Images automatically?
Arenlor's Avatar
Ultra Talker

Posts: 463
Name: J