Tuesday, May 31, 2011

How do you make pictures change (with PHP) every time you refresh?

Example:


http://timeinapicture.com/images/rotate.php


Like that! Everytime you refresh, another picture appears.





What is the PHP code?


What do I DO with the PHP code?


I'm all new to this.





Thanks.How do you make pictures change (with PHP) every time you refresh?
Edit: Actually i just realized, that the following would be easier:





-----StartCode-----


%26lt;?php


$IMG_S = '10'; // The maximum images in the Directory


echo '%26lt;img src=';images/filename_' . rand(1,$IMG_S) . '.jpg'; alt=';An Image';%26gt;';


?%26gt;


-----CodeEnd-----





No need to use arrays, unless ofcause, you want to avoid the same image to be shown 2 times. But would it even matter that much?





The extra code required, take up unnecessary server resources, for a case, which almost never happens, (given you have enough images in the directory).











Anyway, if you would like the script to ';count'; the images first, you would simply do something like the below.





-----StartCode-----


$files = scandir(';images';); // The directory is images


$i = 0; // counter starts at 0


$num = count($files);


while ($i %26lt; $num) {


++$i;


}


$i = $i -2; // removes ';.'; and ';..'; from the results





$IMG_S = $i; // The maximum images in the Directory


echo '%26lt;img src=';images/filename_' . rand(1,$IMG_S) . '.jpg'; alt=';An Image';%26gt;';


-----CodeEnd-----





Obviously you wouldn't want the counting to happen, each time the user hits update. Because this would take up resources unnessarily when there ain't uploaded more images since their last update/visit.





the file format in this script is, ';filename_x.jpg';, using an array we would be able to show images, with all file extensions.





---StartCode---


%26lt;?php


$files = scandir(';images';); // The directory is images





$i = 0; // counter starts at 0


$num = count($files);


while ($i %26lt; $num) {


++$i;


}


$i = $i -1; // excludes ';0'; from the results, since it ain't vaild.





echo '%26lt;img src=';images/' . $files[rand(2, $i)] . ''; alt=';An Image';%26gt;';


?%26gt;


---CodeEnd---








---About PHP---


You would want to put the PHP code inside a .php file, and upload this to your server, with your ftp program, see ( http://www.smartftp.com/ )





You would also need to make shure, that your server, or host is running php, (most hosts have support for php these days).








If you need a good host, i can recommend one.com at http://www.one.com they offer cheap hosting, and got support for php.





See the following:


(scandir)


http://www.php.net/manual/en/function.sc鈥?/a>


(echo) - i recommend this if you are totally new to php


http://php.net/echoHow do you make pictures change (with PHP) every time you refresh?
You can do this. Wherever you want the image in your page, put this instead of a normal %26lt;img%26gt; tag:





%26lt;img src=';%26lt;?php


$images = array


(


';image1.jpg';,


';image2.jpg';,


';image3.jpg';


);


$random = rand(0, count($images));


print($images[$random]);


?%26gt;';%26gt;





And so on, using your image names. Make sure that each different filename is in quotes and there's a comma after all of them except the last.





You can e-mail me at phoenix_ultimatum@yahoo.com if you can't get it to work.

No comments:

Post a Comment