[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 11-20-2007, 09:42 AM   #1 (permalink)
NamePros Member
 
Join Date: Aug 2007
Posts: 75
50.00 NP$ (Donate)

travinb is an unknown quantity at this point


Javascript Question

Hi,

I wanted to know how to make an image rotator (with links) through javascript..is it possible to make one?

Basically every time the page is refreshed I want a new image with its link to come in the others place..

In the hope that I am not asking for too much..and looking for some really helpful tips...

thanks..
__________________
The Unneglectable Blog| FinanceJargon.com |NasCarSchedule.Info
|financeadvisor.co.in| budgethotelsbeijing.com | researchelp.com
travinb is offline  
Old 11-20-2007, 11:12 AM   #2 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough


I don't know much about JS, but it is possible in PHP and actually better.

PHP Code:
<?php

$imageList
= array("pic1", "pic2", "pic3", "pic4");

echo
'<img src="'.$imageList[array_rand($imageList)].'">';

?>
This takes a random picture from the array. Just change the filenames under pic1, pic2, pic3, pic4.



PHP Code:
<?php

$extAllowed
= array("jpg", "gif", "png", "JPG", "GIF", "PNG");

$imageDir = 'img/';

$dirConn = opendir($imageDir);
while (
$file = readdir($dirConn)) {
    
$ext = explode(".", $file);
    if (
in_array($ext[1], $extAllowed)) {
        
$imageList[] = $file;
    }
}
closedir($imageDir);

echo
'<img src="'.$imageDir.'/'.$imageList[array_rand($imageList)].'">';

?>
This one takes a random file from a directory. Just change the $imageDir variable to the folder that has your pictures.


Of course, these files are all shown randomly and not in any order. If you needed to do this in order, you would need to have some kind of counter in either a file or database keeping track of what picture was shown last.
Palyriot is offline  
Old 11-21-2007, 06:32 AM   #3 (permalink)
NamePros Member
 
Join Date: Aug 2007
Posts: 75
50.00 NP$ (Donate)

travinb is an unknown quantity at this point


Thanks Derek...
I wouldn't need a counter and random images are just fine...
My only issue is will I be able to hyperlink the images?
Is there someway that can be added to the picture..

Thanks for the help.. this is really cool though perhaps not what I am looking for...

or else is there anyway I can rotate php files instead of images.. I can then include the images with their links to within those files.. as in ad1.php, ad2.php instead of pic1 pic2...
__________________
The Unneglectable Blog| FinanceJargon.com |NasCarSchedule.Info
|financeadvisor.co.in| budgethotelsbeijing.com | researchelp.com
travinb is offline  
Old 11-21-2007, 08:37 AM   #4 (permalink)
Joe
Senior Member
 
Join Date: Oct 2005
Location: Kent ~ U.K.
Posts: 3,246
88.85 NP$ (Donate)

Joe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud ofJoe has much to be proud of

Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
PHP Code:
<?php

$extAllowed
= array("jpg", "gif", "png", "JPG", "GIF", "PNG"); // this is the list of recognised file extensions (without the .)

$imageDir = 'img/'; // the folder in which all the images are

$dirConn = opendir($imageDir);
while (
$file = readdir($dirConn)) {
     
$ext = explode(".", $file);
    if (
in_array($ext[1], $extAllowed)) { // checking if the loaded file's extension is in the list of allowed extensions
        
$imageList[] = $file;
    }
}
closedir($imageDir);

echo
'<img src="'.$imageDir.'/'.$imageList[array_rand($imageList)].'">'; // display the image on the page.

?>
That is it commented slightly to help you understand.

To rotate php files, merely change
PHP Code:
$extAllowed = array("jpg", "gif", "png", "JPG", "GIF", "PNG");
AND
echo
'<img src="'.$imageDir.'/'.$imageList[array_rand($imageList)].'">';
to
PHP Code:
$extAllowed = array("PHP");
AND
include
$imageList[array_rand($imageList)];
If this is the case, you might want to rename some of the variables appropriately.

Joe

Last edited by TheArbiter; 11-21-2007 at 09:10 AM. Reason: typo
Joe is offline  
Old 11-21-2007, 11:32 AM   #5 (permalink)
Danltn.com
 
Daniel's Avatar
 
Join Date: May 2007
Location: Danltn.com / Nottingham, UK
Posts: 1,201
13.51 NP$ (Donate)

Daniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond reputeDaniel has a reputation beyond repute

Ethan Allen Fund Ethan Allen Fund
PHP Code:
<?php

/**
* Danltn | http://danltn.com
* No warranty is given to code used
*/

$imgs = array(
"pic1.gif" => "http://google.com",
"pic2.gif" => "http://msn.com",
"pic3.gif" => "http://example.com",
);

$image = array_rand($imgs);
$url = $imgs[$image];

echo
"<a href='$url'><img src='$image' border='0'></a>";

?>
Daniel is offline  
Old 11-21-2007, 08:55 PM   #6 (permalink)
NamePros Member
 
Join Date: Aug 2007
Posts: 75
50.00 NP$ (Donate)

travinb is an unknown quantity at this point


Thanks guys..this is great
will surely work with these now.. and will get back to you on this..
__________________
The Unneglectable Blog| FinanceJargon.com |NasCarSchedule.Info
|financeadvisor.co.in| budgethotelsbeijing.com | researchelp.com
travinb is offline  
Old 11-21-2007, 10:52 PM   #7 (permalink)
NamePros Regular
 
Palyriot's Avatar
 
Join Date: Jul 2004
Location: Seattle, Wa
Posts: 596
76.25 NP$ (Donate)

Palyriot is a jewel in the roughPalyriot is a jewel in the roughPalyriot is a jewel in the rough


No problem travinb. You got it perfect Danltn. Thanks for the assist.
Palyriot is offline  
Old 11-21-2007, 11:01 PM   #8 (permalink)
NamePros Legend
 
weblord's Avatar
 
Join Date: Dec 2005
Location: Philippines - www.Nabaza.com
Posts: 19,840
21,700.43 NP$ (Donate)

weblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatnessweblord Has achieved greatness

Autism Protect Our Planet
since you want javascript
http://free-javascript.blogspot.com/...e-rotator.html

Quote:
Originally Posted by travinb
Hi,

I wanted to know how to make an image rotator (with links) through javascript..is it possible to make one?

Basically every time the page is refreshed I want a new image with its link to come in the others place..

In the hope that I am not asking for too much..and looking for some really helpful tips...

thanks..
weblord is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 08:33 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85