PHP: Random banner function?

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

PHP: Random banner function?

Post by ayu »

Hello, i was trying to do a random banner function for my site, but since i am not good at all with PHP yet, i failed =(

this is the banner code

Code: Select all

<td width="223"><img src="themes/Nice_Blue/images/logo.png" width="435" height="99" vspace="5" hspace="5"></td>
i tried doing this

Code: Select all

<?
$alllogos = array
      ("themes/nice_blue/images/logos/logo1.png", "themes/nice_blue/images/logos/logo2.png", "themes/nice_blue/images/logos/logo3.png");
    $totallogos = (count($alllogos));
    $nmbr = (rand(0, ($totallogos)));
?>


  <tr> 
    
    <td width="223"><img src="<? echo$nmbr?>" width="435" height="99" vspace="5" hspace="5"></td>

But it failed ^^ greatly, could anyone give me a hand? =P
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

nvm solved it
"The best place to hide a tree, is in a forest"

User avatar
bad_brain
Site Owner
Site Owner
Posts: 11636
Joined: 06 Apr 2005, 16:00
19
Location: In your eye floaters.
Contact:

Post by bad_brain »

would be nice to post your solution so others can learn from it too... :wink:

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

I guess it's bcz he echoe the nb he got from his random instead of his array of logo :roll:
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

I Googled and found this =)

Code: Select all

<?php

$folder = '';

$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; 
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
    foreach($exts as $ext) { 
        if (preg_match('/\.'.$ext.'$/i', $file, $test)) { 
            $files[] = $file; 
            ++$i;
            }
        }
    }
closedir($handle); 
mt_srand((double)microtime()*1000000); 
$rand = mt_rand(0, $i); 

header('Location: '.$folder.$files[$rand]); 
?>
"The best place to hide a tree, is in a forest"

Post Reply