How to rotate images on every load using simple code in PHP

Posted By: KP On November 23, 2010 Under Other, Programming Categories


Do you wish to rotate a banner or image on every load of website ? Some simple PHP techniques and codes will help you. The changed image may or may not be of same size

Trick 1: No Database use needed.

In this trick we will number all images to be rotated sequentially. e.g. 1.jpg,2.jpg,3.jpg …10.jpg

On page load we will select random number from our range i.e. 1 to 10

So the image code will be

<img src=”images/<?php echo rand(1,10); ?>”.jpg>

Where images is your image folder.

Trick 2: Use of database.

This trick requires a MYSQL database. In this we will store image file name in a table named images. This method allows to use multiple extension images.

id image
1 abcd.jpg
2 efgh.jpg

<?php

$sql=”select * from images where id order by rand()”;

$imagename=mysql_fetch_assoc(mysql_query($sql));

echo “<img src='”images/”.$imagename[‘image’].”‘>”;

?>

Using any of above code will display new image on every page load. Other methods are also there, comment if you have better ideas.



Share with Friends ( Sharing is Caring! )


Subscribe For E-mail Notification Of Article Updates

Enter your email address:

NOTICE:
DO NOT FORGET TO CLICK ON THE VERIFICATION LINK AFTER SUBSCRIBING,THE VERIFICATION MAIL WILL BE SENT TO YOU FROM "FeedBurner Email Subscriptions".
( What is this? Why I need to do this?)

Related Posts

Post a Comment

OR